struct problems
Mar 18, 2014 at 4:34am UTC
I'm supposed to be writting a program to calculate change using structs and functions. I cannot get this down. when I compile it i get this as the answer:
-858993460-858993460-858993460-85899346045
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#include <iostream>
#include <string>
using namespace std;
struct change
{
int quarters, dimes, nickels, pennies;
int cents;
};
change ComputeChange(int cents){
change c;
c.quarters = cents / 25;
c.cents = cents % 25;
c.dimes = cents / 10;
c.cents = cents % 10;
c.nickels = cents / 5;
c.cents = cents % 05;
c.pennies = cents / 1;
c.cents = cents % 01;
return c;
}
void PrintChange(change c){
cout << c.quarters;
cout << c.dimes;
cout << c.nickels;
cout << c.pennies;
cout << c.cents;
return ;
}
int main(int argc, const char * argv[]){
change FirstGiven;
change SecondGiven;
FirstGiven.cents = 45;
SecondGiven.cents = 55;
ComputeChange(FirstGiven.cents);
PrintChange(FirstGiven);
cin.get();
return 0;
}
Mar 18, 2014 at 4:46am UTC
You aren't doing anything with what you are returning from ComputeChange. On line 11 you say you are returning a change object but then in main you are ignoring it.
Mar 18, 2014 at 5:23am UTC
ok so in the main would i put something like:
int main(int argc, const char* argv[]){
change c;
ComputeChange(c.cents);
PrintChange(c);
cin.get();
return0;
}
//because after this i still get the same numbers when i compile. I apologize for my ignorance.
Mar 18, 2014 at 6:07am UTC
1 2 3 4 5 6 7
int main()
{
int cents = 0;
std::cin >> cents;
change c = ComputeChange(cents); //notice this line here
PrintChange(c);
}
Mar 18, 2014 at 6:14am UTC
In your first example ComputeChange returns c but you never pass it to PrintChange.
PrintChange(ComputeChange(FirstGiven.cents));
Mar 18, 2014 at 3:58pm UTC
thanks alot that really helped. I also had the calculation wrong for the change but i figured that out.
Topic archived. No new replies allowed.