Saturday, 15 June 2013

C++ Private And Public Function -



C++ Private And Public Function -

i need add together subtract values next programme error when reach adding part, how can still show after adding 'quarters, nickels, dimes , pennies' .

how come 8 when adding part? please help me out.

#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 number of quarters :" << endl; cin >> qua; cout << "enter number of dimes :" << endl; cin >> dim; cout << "enter number of nickels :" << endl; cin >> nic; cout << "enter number of pennies :" << endl; cin >> pen; purse purse1; purse1.display_purse(qua, dim, nic, pen); cout << endl << "current content of 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 number of quarters add together : " << endl; cin >> quaa; cout << "enter number of dimes add together : " << endl; cin >> dimm; cout << "enter number of nickels add together : " << endl; cin >> nicc; cout << "enter number of pennies add together : " << endl; cin >> penn; cout << endl << "the amount after entered :" << purse1.insert_money(quaa, dimm, nicc ,penn ) << endl << endl; //starts removing cout << "enter number of quarters remove : " << endl; cin >> quaaa; cout << "enter number of dimes remove : " << endl; cin >> dimmm; cout << "enter number of nickels remove : " << endl; cin >> niccc; cout << "enter number of pennies remove : " << endl; cin >> pennn; cout << endl << "the amount after entered :" << purse1.remove_money(quaaa, dimmm, niccc, pennn) << endl; cout << "or " << quaaa/4 + dimmm/10 + nic/20 + pennn/100 << " dollars" << endl << endl; homecoming 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; homecoming quarters; dimes += dimm; homecoming dimes; nickels += nicc; homecoming nickels; pennies += penn; homecoming pennies; } int purse::remove_money(int quaaa, int dimmm, int niccc, int pennn) { quarters -= quaaa; homecoming quarters; dimes -= dimmm; homecoming dimes; nickels -= niccc; homecoming nickels; pennies -= pennn; homecoming pennies; }

you're dividing integers. illustration 9/4 == 2 evaluates true while 9/4 == 1.25 evaluate false.

the simple prepare problem split number of each quantity of lesser coins floating point number instead:

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

additionally, functions aren't completing code. homecoming statement in function exits. example:

int purse::insert_money(int quaa, int dimm, int nicc, int penn) { quarters += quaa; homecoming quarters; //everything after never executes dimes += dimm; homecoming dimes; nickels += nicc; homecoming nickels; pennies += penn; homecoming pennies; }

should instead be:

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

or potentially:

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; }

if you'd homecoming total amounts of each type of currency in purse

finally display_purse, doesn't display contents of purse, it's setting purse whatever values pass in.

in general posted code leads me believe haven't attempted step through execution , debug yourself, between reply other comments should on right track started.

c++

No comments:

Post a Comment