Vending Machine

Can someone help me to create a vending machine program? Im just studying C++ for almost 3 months. We started from cout and finish in looping. It is possible for me to create that kind of program? If not, can you guys suggest me another program? Thanks!
Last edited on
You started from cout and finished in looping? As in, for-loops and such? You can easily learn that in a few days, what did you do for the rest of the class?

Also, this is not a homework site, show us something you have been working on and we'll be glad to help out.
Sorry TarikNeaj. Yes. We do different activities. Also I'm not an IT or CS student. Computer Programming is a minor subject on our course. This is what i'm doing. A vending machine. But I cannot run it. The error says, the variable amount is used without initialization. Hopefully you can help me. Thanks!

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <iostream>
#include <conio.h>
using namespace std;

int main()
{

	char a,b,c,d,e;
	int amount,current,change,choice;
	
	cout<<"*************************************"<<endl;
	cout<<"*--------- Vending Machine ---------*"<<endl;
	cout<<"*************************************";

	cout<<endl<<endl;
	
	while (amount !=0)
	{
	cout<<"Insert 5php coin"<<endl;
	cout<<"\nInsert 10php coin"<<endl;
	cout<<"\nAmount: ";
	cin>>amount;
	
	cout<<endl;
	cout<<"Current amount: "<<current<<".00php";
	
	current = current + amount;

		if (current == 1 && current <= 8)
		{
			cout<<endl;
			cout<<"Current amount: "<<current<<".00php";

			cout<<"a. Orange Juice --------------------- 8.00php"<<endl;
			cout<<"b. Apple Juice ---------------------- 8.00php"<<endl;
			cout<<"c. Mango Juice ---------------------- 8.00php"<<endl;
			cout<<"d. Pineapple Juice ------------------ 9.00php"<<endl;
			cout<<"e. Milo Juice ---------------------- 10.00php"<<endl;
		}
		if (current > 8 && current < 10)
		{
			cout<<endl;
			cout<<"Current amount: "<<current<<".00php";

			cout<<"a. Orange Juice --------------------- 8.00php"<<endl;
			cout<<"b. Apple Juice ---------------------- 8.00php"<<endl;
			cout<<"c. Mango Juice ---------------------- 8.00php"<<endl;
		}
		if (current > 10 && current < 12)
		{
			cout<<endl;
			cout<<"Current amount: "<<current<<".00php";

			cout<<"d. Pineapple Juice ----------------- 10.00php"<<endl;
			cout<<"e. Milo Juice ---------------------- 10.00php"<<endl;
		}
		cout<<endl;
		cout<<"Enter your choice: ";
		cin>>choice;

		change = current - amount;

		switch (choice)
		{
		case 'a':
			{
				cout<<"You've selected Apple Juice."<<endl;
				cout<<"Your change is: "<<change;
				break;
			}
		case 'b':
			{
				cout<<"You've selected Orange Juice."<<endl;
				cout<<"Your change is: "<<change;
				break;
			}
		case 'c':
			{
				cout<<"You've selected Mango Juice."<<endl;
				cout<<"Your change is: "<<change;
				break;
			}
		case 'd':
			{
				cout<<"You've selected Pineapple Juice."<<endl;
				cout<<"Your change is: "<<change;
				break;
			}
		case 'e':
			{
				cout<<"You've selected Milo Juice."<<endl;
				cout<<"Your change is: "<<change;
				break;
			}
		default:
			cout<<"Invalid Input!";
		}

		cout<<"\nThank you!";
	}


getch();
return 0;
}
Last edited on
You are trying to use amount and current without either of them starting with a number.
Instead of having amount in the while loop why not a boolean like isRunning. Then if you want to stop the while loop just give the user an option that sets isRunning to false, ending the while loop. Or you could check at the end of the while loop if amount == 0 then isRunning = false.

1
2
3
4
5
6
7
8
9
10
11
int amount = 0, current = 0;
bool isRunning = true;

while(isRunning)
{
       //do stuff
      
       if(amount <= 0)
          isRunning = false;
}


You can use amount if you want, but you need to get the user's amount before the while loop starts, because right now it's not even initialized and if you do initialized it, it would be equal to 0, making it not even run the while loop to start with.

1
2
3
4
5
6
7
8
9
int amount = 0, current = 0;

cin >> amount;

while(amount > 0)
{
       //do stuff
}


Last edited on
Thanks sir toast9 it try to use the other. Sorry. Im just a newbie and didn't know about the bool. Our instructor didn't explain that well. So if I use the other method, the codes will look like this?

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <conio.h>
using namespace std;

int main()
{

	char a,b,c,d,e;
	int choice;
        int amount = 0, current = 0;
        bool isRunning = true;

	cout<<"*************************************"<<endl;
	cout<<"*--------- Vending Machine ---------*"<<endl;
	cout<<"*************************************";

	cout<<endl<<endl;
	
	while (isRunning)
	{
	cout<<"Insert 5php coin"<<endl;
	cout<<"\nInsert 10php coin"<<endl;
	cout<<"\nAmount: ";
	cin>>amount;
	
	cout<<endl;
	cout<<"Current amount: "<<current<<".00php";
	
	current = current + amount;

		if (current == 1 && current <= 8)
		{
			cout<<endl;
			cout<<"Current amount: "<<current<<".00php";

			cout<<"a. Orange Juice --------------------- 8.00php"<<endl;
			cout<<"b. Apple Juice ---------------------- 8.00php"<<endl;
			cout<<"c. Mango Juice ---------------------- 8.00php"<<endl;
			cout<<"d. Pineapple Juice ------------------ 9.00php"<<endl;
			cout<<"e. Milo Juice ---------------------- 10.00php"<<endl;
		}
		if (current > 8 && current < 10)
		{
			cout<<endl;
			cout<<"Current amount: "<<current<<".00php";

			cout<<"a. Orange Juice --------------------- 8.00php"<<endl;
			cout<<"b. Apple Juice ---------------------- 8.00php"<<endl;
			cout<<"c. Mango Juice ---------------------- 8.00php"<<endl;
		}
		if (current > 10 && current < 12)
		{
			cout<<endl;
			cout<<"Current amount: "<<current<<".00php";

			cout<<"d. Pineapple Juice ----------------- 10.00php"<<endl;
			cout<<"e. Milo Juice ---------------------- 10.00php"<<endl;
		}
                if (current <= 0)
		{
		       isRunning = false;
		}

		cout<<endl;
		cout<<"Enter your choice: ";
		cin>>choice;

		change = current - amount;

		switch (choice)
		{
		case 'a':
			{
				cout<<"You've selected Apple Juice."<<endl;
				cout<<"Your change is: "<<change;
				break;
			}
		case 'b':
			{
				cout<<"You've selected Orange Juice."<<endl;
				cout<<"Your change is: "<<change;
				break;
			}
		case 'c':
			{
				cout<<"You've selected Mango Juice."<<endl;
				cout<<"Your change is: "<<change;
				break;
			}
		case 'd':
			{
				cout<<"You've selected Pineapple Juice."<<endl;
				cout<<"Your change is: "<<change;
				break;
			}
		case 'e':
			{
				cout<<"You've selected Milo Juice."<<endl;
				cout<<"Your change is: "<<change;
				break;
			}
		default:
			cout<<"Invalid Input!";
		}

		cout<<"\nThank you!";
	}


getch();
return 0;
}
Look at this condition:

if (current == 1 && current <= 8)
This is the same as writing:

if (current == 1)

Do you see why?

What happens if current is between 2 and 8 inclusive?

What happens if current is 10?

What happens if current is 12 or greater?

Last edited on
So I need to use || rather than &&?

if (current == 1 || current <= 8)

I chose to restrict the value until 12 because of the items.
This:

if (current == 1 || current <= 8)

is the same as writing:

if (current <= 8)

I'd ask you if if you see why, but since you ignored almost every question I asked in my previous post, it feels pointless.

Last edited on
I'm so sorry.

If the current is between 2 and 8, It will still display the value.

If the current is 10, it will display all the items from 8.00php to 10php

If the current is greater than 12, I don't know what will happen.

Im very sorry sir. Please help me.
@Ryanjoshiii. Please always listen to what people have to say and answer their questions, they are asking it for a reason.

I'll just simply explain it.

if (current == 1 || current <= 8)

Is the same as if (current <= 8)

Because numbers. Thats literally it. Number 1 is already less than 8, so if you just used the less than 8 if statement, then the number 1 is already included in it.

current <=8 is all the numbers that is equal to or below 8. 1 is equal to or below 8. Meaning the current == 1 is pointless.

Edit: What exactly are you trying to say with that if statement? Maybe we can help you with that if this is not what you were looking for.
Last edited on
Thank you so much @TarikNeaj. Now I understand. I'm trying to say that the vending machine will display the items if the current amount is exact or more than the amount to pay for each items. And if you still add or insert 1php or 5 php, the machine will continue to display the items that the current amount is capable of buying. I'm so sorry if my codes are not clear.

Is this okay now guys? Or still need revision?

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
		if (current >= 8)
		{
			cout<<endl;
			cout<<"Current amount: "<<current<<".00php";

			cout<<"a. Orange Juice --------------------- 8.00php"<<endl;
			cout<<"b. Apple Juice ---------------------- 8.00php"<<endl;
			cout<<"c. Mango Juice ---------------------- 8.00php"<<endl;
		}
		else if (current >=9)
		{
			cout<<endl;
			cout<<"Current amount: "<<current<<".00php";

			cout<<"a. Orange Juice --------------------- 8.00php"<<endl;
			cout<<"b. Apple Juice ---------------------- 8.00php"<<endl;
			cout<<"c. Mango Juice ---------------------- 8.00php"<<endl;
			cout<<"d. Pineapple Juice ------------------ 9.00php"<<endl;
		}
		else if (current >=10)
		{
			cout<<endl;
			cout<<"Current amount: "<<current<<".00php";

			cout<<"d. Mil Juice  ----------------- 10.00php"<<endl;
			cout<<"e. Milo Juice ---------------------- 10.00php"<<endl;
		}
                else if (current <= 0)
		{
		       isRunning = false;
		}
Last edited on
It looks nice. But one question. Here -

1
2
else if (current >=10)
		


At that if statement, dont you want to display all the 8php and 9php items aswell as the 10php items? Because at the

else if (current >=9) You display both 8 and 9 php.
@TarikNeaj, Thanks bro.

Yes. If the current amount is 8php, it will display the items that cost 8php, then if the current is 9php, it will display both items that cost 8php up to 9php, and if the current amount is 12php or greater than 12php, it will display items that cost 8php up to 10php. Then you will select one. After that, the machine will display the change.

Also, You can, insert 1php coin or 5php coin until you didn't choose any of other items.

I think this will help you understand what I mean. Please see this.

http://prntscr.com/6bqucw

http://prntscr.com/6bqun2
Last edited on
This is another modification I made. But the problem is, even if the current amount is not enough to buy the items, the items still appear. All I want is the items will appear if the current amount is enough or exact to buy the item. Thanks!

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <iostream>
#include <conio.h>
#include <Windows.h>
using namespace std;

int main()
{
		SetConsoleTitle( TEXT( "Vending Machine" ) );
        int amount = 0, current = 0;
        bool isRunning = true;

	cout<<"*************************************"<<endl;
	cout<<"*--------- Vending Machine ---------*"<<endl;
	cout<<"*************************************";

	cout<<endl<<endl;
	
	do
	{
		cout<<"Current amount: "<<current<<".00php"<<endl;

	    cout<<"\n1.Insert 1php coin";
		cout<<"\n5.Insert 5php coin";
		cout<<"\n10.Insert 10php coin"<<endl;
	
		cout<<"\na. Orange Juice --------------------- 8.00php"<<endl;
		cout<<"b. Apple Juice ---------------------- 8.00php"<<endl;
		cout<<"c. Mango Juice ---------------------- 8.00php"<<endl;
		cout<<"d. Pineapple Juice ------------------ 9.00php"<<endl;
		cout<<"e. Milo Juice ---------------------- 10.00php"<<endl;
		cout<<"f. Milk Juice ---------------------- 10.00php"<<endl;
		cout<<"g. Iced Milo ----------------------- 11.00php"<<endl;
		cout<<"h. Lemon Iced Tea ------------------ 11.00php"<<endl;
		cout<<"i. Hot Choco ----------------------- 12.00php"<<endl;
		cout<<"j. 3 in 1 Coffee ------------------- 12.00php"<<endl;
	    
		cout<<"\nInsert coin: ";
		cin>>amount;
		current = current + amount;
	    
		cout<<endl;
		{
			if (current <= 7)
			{
				cout<<endl;
			}
			else if (current == 8)
			{
				cout<<endl;
				cout<<"Current amount: "<<current<<".00php";
	
				cout<<"\na. Orange Juice --------------------- 8.00php"<<endl;
				cout<<"b. Apple Juice ---------------------- 8.00php"<<endl;
				cout<<"c. Mango Juice ---------------------- 8.00php"<<endl;
			}
			else if (current == 9)
			{
				cout<<endl;
				cout<<"Current amount: "<<current<<".00php";

				cout<<"\na. Orange Juice --------------------- 8.00php"<<endl;
				cout<<"b. Apple Juice ---------------------- 8.00php"<<endl;
				cout<<"c. Mango Juice ---------------------- 8.00php"<<endl;
				cout<<"d. Pineapple Juice ------------------ 9.00php"<<endl;
			}
			else if (current >= 10)
			{
				cout<<"Current amount: "<<current<<".00php";

				cout<<"\na. Orange Juice --------------------- 8.00php"<<endl;
				cout<<"b. Apple Juice ---------------------- 8.00php"<<endl;
				cout<<"c. Mango Juice ---------------------- 8.00php"<<endl;
				cout<<"d. Pineapple Juice ------------------ 9.00php"<<endl;
				cout<<"e. Milo Juice ---------------------- 10.00php"<<endl;
				cout<<"f. Milk Juice ---------------------- 10.00php"<<endl;
				cout<<"g. Iced Milo ----------------------- 11.00php"<<endl;
				cout<<"h. Lemon Iced Tea ------------------ 11.00php"<<endl;
				cout<<"i. Hot Choco ----------------------- 12.00php"<<endl;
				cout<<"j. 3 in 1 Coffee ------------------- 12.00php"<<endl;
			}
			else
			{
				cout<<"Invalid Input!"<<endl;
			}
		}
			if (amount == 0)
			{
				isRunning = false;
				system("pause");
			}
			else if (amount == 2)
			{
			    cout<<"Invalid Input!"<<endl;
			}
			else if (amount == 3)
			{
			    cout<<"Invalid Input!"<<endl;
			}
			else if (amount == 4)
			{
			    cout<<"Invalid Input!"<<endl;
			}
			else if (amount == 6)
			{
			    cout<<"Invalid Input!"<<endl;
			}
			else if (amount == 7)
			{
			    cout<<"Invalid Input!"<<endl;
			}
			else if (amount == 8)
			{
			    cout<<"Invalid Input!"<<endl;
			}
			else if (amount == 9)
			{
			    cout<<"Invalid Input!"<<endl;
			}
	}
	while (isRunning);
getch();
return 0;
}
But the problem is, even if the current amount is not enough to buy the items, the items still appear.

You mean right at the start of each loop iteration? You print them all out in lines 26 - 35.
Last edited on
Yes sir. It always appear. I think its because its on the start of the program. I put if else to limit the display items but it doesnt work.

For example sir, I insert 1php coin. So the current amount is 1php. So it means that no items shall appear. But it always appear. So if the current amount is 10php, it will display all the items that worth 8.00php up to 10.00php.
Last edited on
Look at lines 26 - 35. Those lines run on every iteration of the loop.
Topic archived. No new replies allowed.