I need help reformatting my if-else statements

Write your question here.

I've made a program that accepts cent input between 5 and 100, and returns change in quarters/dimes/nickels. When I input 85, it should give back 3 quarters and a dime.

Only the if statement executes. I thought I properly nested my if/else loops but it doesn't seem like it. Can someone tell me how to re-format this code?
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;

int main()
{
	int purchaseAmount;
	int quarters =25;
	int dimes =10;
	int nickels =5;

	cout << "Simple Vending Program for VKK (Regular Version)"<<endl;
	cout << "Enter a purchase amount [5-100 ]: ";
	cin >> purchaseAmount;
		if ( purchaseAmount % 25 == 0)
		{
			purchaseAmount = purchaseAmount / quarters;	
			quarters = purchaseAmount;
			if (purchaseAmount % 10 == 0)
			{
				purchaseAmount = purchaseAmount / dimes;
				dimes = purchaseAmount;
				if (purchaseAmount % 5 == 0)
				{
					purchaseAmount = purchaseAmount / nickels;
					nickels = purchaseAmount;
				}
			}
			cout << quarters << dimes << nickels;
		}
    
		else 
	 {
		 cout << "Unable to process and invalid purchase amount of " << purchaseAmount << " cents"<<endl;
	 }
	return 0;
}
You have a logic problem here. Program just does not do what you want.
What you have now: your if statement executes only if amount is evenly divisible by 25, so you can only enter 0, 25, 50, 75 100 etc.
Then you are dividing your amount by 25 losing all modulo information about it

In fact you do not even need any if statements here.

Example of calculating change:
1
2
3
4
5
6
7
quarters = amount / 25;
amount %= 25;
dimes = amount / 10;
amount %= 10;
nickels = amount / 5;
amount %=5;
pennies = amount;

I need to use the if/else, for a project

i re-formated some of the code using what you wrote.

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
#include <iostream>

using namespace std;

int main()
{
	int purchaseAmount;
	int quarters = 25;
	int dimes = 10;
	int nickels = 5;

	cout << "Simple Vending Program for VKK (Regular Version)" << endl;
	cout << "Enter a purchase amount [5-100 ]: ";
	cin >> purchaseAmount;
		
		if (purchaseAmount % 25 == 0)
	{
		quarters = purchaseAmount / 25;
		purchaseAmount %= 25;
		cout << quarters << dimes << nickels;
	}
		else if (purchaseAmount % 10 == 0)
		{
			dimes = purchaseAmount / 10;
			purchaseAmount %= 10;
			cout << quarters << dimes << nickels;
		}
		else if (purchaseAmount % 5 == 0)
		{
			nickels = purchaseAmount / 5;
			purchaseAmount %= 5;
			cout << quarters << dimes << nickels;
		}
		
	else
		cout << "Unable to process an invalid purchase amount of " << purchaseAmount << " cents." << endl;
	
	return 0;
}
Still wrong logic: you will only give quarter if whole sum can be given as quarters. You do not need conditions there, make one just before calculating change to check if amount is valid.
sorry about that. you were right, I didn't need all of those conditions.

i used the code you supplied without all of those conditions and it worked! I guess when I looked over your code I was confused and didn't understand it fully.

thanks
Topic archived. No new replies allowed.