C ++私有和公共功能

时间:2014-11-03 17:36:37

标签: c++

我需要在下面的程序中添加一个减法值,但是当我到达添加部分时我会收到错误,如何在添加'季度,镍币,硬币和硬币之后仍然显示它。

当我到达添加部分时,为什么我得到8?请帮帮我。

#include <iostream>
#include <iomanip>
using namespace std;

class Purse
{
private:
    int quarters;
    int dimes;
    int nickels;
    int pennies;

public:
    void display_purse(int, int, int, int); 
    int insert_money(int, int, int, int);
    int remove_money(int,int,int,int);  
};

int main ()
{
    int qua, dim, nic, pen;
    int quaa, dimm, nicc, penn;
    int quaaa, dimmm, niccc, pennn;

    cout << "Enter the number of Quarters :" << endl;
    cin >> qua;

    cout << "Enter the number of Dimes :" << endl; 
    cin >> dim;

    cout << "Enter the number of Nickels :" << endl; 
    cin >> nic;

    cout << "Enter the number of Pennies :" << endl; 
    cin >> pen;


    Purse purse1;
    purse1.display_purse(qua, dim, nic, pen);

    cout << endl << "Current content of the purse:  " << qua << " quarters " << dim          <<      "    dimes " << nic << " nickels " << pen << " pennies " << endl << endl;

    cout << "or " << qua/4 + dim/10 + nic/20 + pen/100 << " dollars" << endl << endl;

    //starts adding

    cout << "Enter the number of Quarters to add : " << endl;
    cin >> quaa;

    cout << "Enter the number of Dimes to add : " << endl;
    cin >> dimm;

    cout << "Enter the number of Nickels to add : " << endl;
    cin >> nicc;

    cout << "Enter the number of Pennies to add : " << endl;
    cin >> penn;

    cout << endl << "The amount after entered is :" << purse1.insert_money(quaa,    dimm,      nicc ,penn ) << endl << endl;

//starts removing

    cout << "Enter the number of Quarters to remove : " << endl;
    cin >> quaaa;

    cout << "Enter the number of Dimes to remove : " << endl;
    cin >> dimmm;

    cout << "Enter the number of Nickels to remove : " << endl;
    cin >> niccc;

    cout << "Enter the number of Pennies to remove : " << endl;
    cin >> pennn;

    cout << endl << "The amount after entered is :" << purse1.remove_money(quaaa, dimmm, niccc, pennn) << endl;

    cout << "or " << quaaa/4 + dimmm/10 + nic/20 + pennn/100 << " dollars" << endl << endl;

    return 0;
}

void Purse::display_purse(int x, int y, int z, int a)
{
    quarters = x;
    dimes = y;
    nickels = z;
    pennies = a;
}

int Purse::insert_money(int quaa, int dimm, int nicc, int penn)
{
    quarters += quaa;
    return quarters;

    dimes += dimm;
    return dimes;

    nickels += nicc;
    return nickels;

    pennies += penn;
    return pennies;
}

int Purse::remove_money(int quaaa, int dimmm, int niccc, int pennn)
{
    quarters -= quaaa;
    return quarters;

    dimes -= dimmm;
    return dimes;

    nickels -= niccc;
    return nickels;

    pennies -= pennn;
    return pennies;
}

1 个答案:

答案 0 :(得分:2)

你正在划分整数。例如,9/4 == 2评估为true,而9/4 == 1.25评估为false

解决问题的简单方法是将每个较小硬币的数量除以浮点数:

cout << "or " << quaaa/4.0 + dimmm/10.0 + nic/20.0 + pennn/100.0 << " dollars" << endl << endl;

此外,您的功能实际上并未完成其代码。一旦你到达函数中的return语句就会退出。例如:

int Purse::insert_money(int quaa, int dimm, int nicc, int penn)
{
    quarters += quaa;
    return quarters;  //Everything After this never executes

    dimes += dimm;
    return dimes;

    nickels += nicc;
    return nickels;

    pennies += penn;
    return pennies;
}

应改为:

void Purse::insert_money(int quaa, int dimm, int nicc, int penn)
{
    quarters += quaa;
    dimes += dimm;
    nickels += nicc;
    pennies += penn;
}

或潜在的:

void Purse::insert_money(int& quaa, int& dimm, int& nicc, int& penn)
{
    quarters += quaa;
    quaa = quarters;
    dimes += dimm;
    dimm = dimes;
    nickels += nicc;
    nicc = nickels;
    pennies += penn;
    penn = pennies;
}

如果您想要退还钱包中每种货币的总金额

最后,display_purse不显示钱包的内容,实际上只是将你的钱包设置为你传入的任何值。

一般来说,您发布的代码会让我相信您并没有真正尝试逐步执行并自行调试,但在我的回答中,您应该在正确的轨道上开始其他评论。