2014-11-03 127 views
0

我需要從以下程序中添加一個減值,但是當我到達添加部分時出現錯誤,如何在添加「宿舍,鎳幣,硬幣和便士」後仍然顯示它。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; 
} 
+0

四個參數「int,int,int,int」是什麼意思?這太可怕了。 – gnasher729 2014-11-03 17:38:31

+3

你需要一些基本的C++課程。在這種情況下,你有多個'return'語句。第一個返回下面的代碼從不執行。 – 2014-11-03 17:38:35

+0

您需要告訴我們您收到的錯誤。 – Philipp 2014-11-03 17:44:15

回答

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; 

此外,你的功能實際上並沒有完成他們的代碼。一旦你在函數中返回一個返回語句,它就會退出。例如:

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,不顯示你的錢包的內容,它實際上只是設置你的p請注意你傳入的任何值。

一般而言,您發佈的代碼會讓我相信您並未實際嘗試單步執行並自行調試它,但在我的回答中您的其他評論應該在右側跟蹤開始。