issue with an else statement

Here is the problem: Here is the problem text: You have invented a vending machine capable of deep frying twinkies. Write a program to simulate the vending machine. It costs $ 3.50 to buy a deep- fried twinkie, and the machine only takes coins in denominations of a dollar, quarter, dime, or nickel. Write code to simulate a person putting money into the vending machine by repeatedly prompting the user for the next coin to be inserted. Output the total entered so far when each coin is inserted. When $ 3.50 or more is added, the program should output Enjoy your deep- fried twinkie along with any change that should be returned.
Hi there, So far everything is working fine except one minor thing. The prompt "you have to enter this amount more" still appears even if the machine owes money.
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
54
55
56
57
58
59
60
61
62
63
64
65
66
1 #include <iostream>
2 using namespace std;
3 
4 void getUserAmt();
5 
6 double amount, remain, total;
7 
8 int main () {
9 
10         total = 3.50;
11         // call function
12 
13         while (total > 0)
14         {
15                 getUserAmt();
16                 total = total - amount;
17                 cout << "you have to enter this amount more " << total << endl;
18         }
 19 
20         if (total == 0)
21         {
22         cout << "Item paid in full!" << endl;
23         }
 24 
 25         else (amount > total);
26         {
27                 cout << "your change is " <<  total  << "thanks" <<  endl;
28         }
29 
30         return 0;
31 }
32 
33 void getUserAmt()
34 {
35         char choice;
36 
37         cout << "the cost is $3.5" << endl;
38         cout << "enter dollar,quarter,dime, nickel - using corresponding letters(D.Q.d.N)" <<endl;
39         cin >> choice;
40 
41         cout << "you've entered choice" << choice << endl;
42 
43         if (choice == 'D')
44         {
45                 amount = 1.00;
46         }
47         else if (choice == 'Q')
48         {
49                 amount = .25;
50         }
51         else if (choice == 'd')
52         {
53                 amount = .10;
54         }
55         else if (choice == 'N')

  56         {
  57                 amount = .05;
  58         }
  59         else
  60         {
  61                 amount = 0;
  62         }
  63 }
  64 
  65 


Thank you!
Ok, you have multiple problems first the one you asked about. The problem with it outputting "you have to enter this amount more" is because your while statement will output that as long as your total is greater than 1. I will let you figure out how to fix that.

Second, don't use global variables! Unless there constant, don't use them. Pass variables to the function and have it return them or use reference variables.

OK here is what I changed your code to. I tried to keep it as close to yours as I could but I did end up haveing to change multiple things. Take a look and hopefully, it helps you solve your problem.

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <iomanip>
using namespace std;

//funtion prototype
double getUserAmt();



int main() {

	double entered, totalLeft=3.50; //<-- moved variable in main

	cout << fixed << setprecision(2);

	while (totalLeft > 0)
	{
		entered = getUserAmt();
		totalLeft -= entered;

		if (totalLeft > 0)
		{
			cout << "You still owe: " << totalLeft << endl;
		}
		cout << "\n";
	}


	if (totalLeft == 0)
	{
		cout << "Item paid in full!" << endl;
	}

	else
	{
		totalLeft *= -1;
		cout << "Your change is $" << totalLeft << endl;
	}
	cout << "Good Bye" << endl;

	return 0;

}

double getUserAmt()
{
	char choice;
	double entered;

	cout << "The cost is $3.50" << endl;
	cout << "Enter dollar,quarter,dime, nickel - using corresponding letters(D.Q.d.N)" << endl;
	cin >> choice;

	cout << "You have entered: " << choice << endl;

	if (choice == 'D')
	{
		entered = 1.00;
	}
	else if (choice == 'Q')
	{
		entered = .25;
	}
	else if (choice == 'd')
	{
		entered = .10;
	}
	else if (choice == 'N')

	{
		entered = .05;
	}
	else
	{
		entered = 0;
	}
	return entered;
}
Last edited on
Many Thanks!
Topic archived. No new replies allowed.