Thank you very much for replying. I think I understand the part about the "reference variable".
And for declaring the struct part, I understand it completely and now it works just fine :)
Now, I probably should have explained what I am trying to do with this code. So, basically, it takes dollars and cents inputs from the user as integers and print them in the regular form $xx.xx
And it has a logic (lines 24 and 25) that round up values like $1.99 to become $2.00. After making the correction you showed me, it works fine at printing out $xx.xx, but it does not round it up. Here is an example:
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
|
#include <iostream>
using namespace std;
struct money
{
int dollars;
int cents;
};
void init(money& x)
{
cout << "Enter dollars and cents: ";
cin >> x.dollars >> x.cents;
if(x.cents > 99)
{
x.dollars += x.cents/100;
x.cents = x.cents%100;
}
cout << "Total money you have is: " << "$" << x.dollars << "." << x.cents << endl;
}
int main()
{
money x;
init(x);
return 0;
}
|
Output:
Enter dollars and cents: 1 99
Total money you have is: $1.99 <----- this should be $2.00, if lines 24 and 25 are the correct logic for incrementing the dollar when the cents are more than 99. But the fact it's not working means that lines 24 and 25 are wrong? Or am I just not putting what's in line 33 in the right place?