Help with calling functions in int main()

Hi guys,
I hope someone can help me with this code that I am stuck in at.

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

int main()
{
	init(money);

return 0;	
}  


I am confused about a couple of stuff.

1. In line 12, I do not understand what is the purpose of the "&". Why is it there?

2. In line 33, Inside int main(), I know that I am calling the void init(money& x) function, the wrong way. But I do not know what the right way is :(
I tried void init(money x), void init(money& x), void init(x.money), init(money x), init(money& x), init(x.money) and other variations but all are wrong. What is the correct way?
Last edited on
1. In line 12, I do not understand what is the purpose of the "&". Why is it there?

A "&" (reference variable) is an alias for another variable. Any changes made to the reference variable are actually performed on the variable for which it is an alias. By using a reference variable as a parameter, a function may change a variable that is defined in another function.
Reference variables are defined like regular variables, except you place an ampersand (&)
in front of the name. For example, the following function definition makes the parameter
refVar a reference variable:
1
2
3
4
void doubleNum(int &refVar)
{
refVar *= 2;
}


This function doubles refVar by multiplying it by 2. Since refVar is a reference variable,
this action is actually performed on the variable that was passed to the function as an argument.

2. In line 33, Inside int main(), I know that I am calling the void init(money& x) function, the wrong way.


You have to declare the structure money. Then, you can call the function void init(money& x)

For example:
1
2
3
4
5
6
7
int main()
{
	money m; // m is a money structure.
	init(m); // Call the function void init(money& x).

	return 0;
}

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?


The condition at line 21 makes the logic in the if statement run if the cents value is greater (but not equal to) 99. That makes sense to me: if cents ended up being 100 or more, the logic you have would increment the dollar number and re-assign the cents number correctly, e.g. if dollars had 1 and cents had 101, it would change that to dollars 2 and cents 1.

If the cents number is less than 10, I think you need to figure out a way to put in a leading 0 when displaying it. Otherwise it looks like $2.1 instead of $2.01.

1
2
Enter dollars and cents: 1 101
Total money you have is: $2.1

Last edited on
Yes, I think I have to figure that out. But for now, my issue of the program not rounding up, for example, $1.99 to $2.00, still remains an unknown to me :(
I guess I don't quite understand why $1.99 should round up to $2.00. Is it just 99 cents that should be bumped up to a full dollar? Should $1.98 stay $1.98 or should that also be rounded up to the nearest dollar?
Yes, $1.98 should remain as $1.98.

What lines 25 and 26 are supposed to do is to round up the dollar to the next value, ONLY if the cents are 99. Not 98, not 97 or else. Just in case cents were 99.

Some examples:

$1.99 --> $2.00
$100.99 --> $101.00
$x.99 --> $(x+1).00

Do you see what I mean?
Assuming the balance is nonnegative:
1
2
3
4
if ((x.cents % 100) == 99) { // e.g., x.cents is 99, 199, 299, etc.
  x.dollars += (x.cents / 100) + 1; 
  x.cents = 0;
}
Thank you all so much, guys! Now it works as I want to.

Here is the final code, just for reference sake:

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
#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 % 100) == 99) 
	{
	x.dollars += (x.cents / 100) + 1; 
	x.cents = 0;
	}
		
	cout << "Total money you have is: " << "$" << x.dollars << "." << x.cents << endl;
		
	}

int main()
{
	money x;	
	init(x);
	

return 0;	
}

Topic archived. No new replies allowed.