programming to design a vending machine in c++ language

my program works as far as asking the user to enter the money and keeping count of the money enter. but once enough money is entered to purchase an item the program doesnt respond to ask the user to enter a selection. can anyoe assist me please?

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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <iostream>
#include <iomanip>                                             
using namespace std;


int main()                                                      
{
char money;
double totalm = 0;
double value = 0, valued = 0, valuen = 0, valueq = 0, valueb = 0;
int c1=0, c2=0, c3=0, c4=0, c5=0, c6=0;
int C1=0, C2=0, C3=0, C4=0, C5=0, C6=0;
int item;

	cout<<"======Welcome to TD'S Vending======"<<endl;
	cout<<"    Depost Money                   "<<endl;
	cout<<"    Enter n or N for Nickel        "<<endl;
	cout<<"    Enter d or D for Dime          "<<endl;
	cout<<"    Enter q or Q for Quarter       "<<endl;
	cout<<"    Enter b or B for Dollar Bill   "<<endl;
	cout<<"    Enter e to end the deposit     "<<endl;
	cin>>money;

	while(money!='e' && money!='E')
	{
		if(money=='n' || money=='N')
		{
			double valuen=0.05;
			totalm+=valuen;
			cout<<"nickel is deposited, the total money deposited is "<<totalm<<endl;
			cin>>money;
		}
		if(money=='d' || money=='D')
		{
			double valued=0.10;
			totalm+=valued;
			cout<<"dime is deposited, the total money deposited is "<<totalm<<endl;
			cin>>money;
		}
		if(money=='q' || money=='Q')
		{
			double valueq=0.25;
			totalm+=valueq;
			cout<<"quarter is deposited, the total money deposited is "<<totalm<<endl;
			cin>>money;
		}
		if(money=='b' || money=='B')
		{
			double valueb=1.00;
			totalm+=valueb;
			cout<<"dollar is deposited, the total money deposited is "<<totalm<<endl;
			cin>>money;
		}
		else
		{
			cout<<"invalid entry, please try again"<<endl;
			cin>>money;
		}
	}

	cout<<"=========Welcome to TD'S Vending========="<<endl;
	cout<<"    Snickers     Goose     Condom        "<<endl;
	cout<<"     $0.85       $2.00     $1.50         "<<endl;
	cout<<"       C1          C2        C3          "<<endl<<endl;
	cout<<"     Chips       Dutch      Mint         "<<endl;
	cout<<"     $0.50       $1.00     $0.25         "<<endl;
	cout<<"       C4          C5        C6          "<<endl;
	cout<<"========================================="<<endl;
	cout<<"Please enter item number"<<endl;
	cin>>item;

	if (item==c1||C1)
	{
		double S=0.85;
		float change=0;
		if(totalm>=S)
		{
			cout<<"Snickers is purchased"<<endl;
			change=totalm-S;
			cout<<"Money deposited is"<<totalm<<"Your change is"<<change<<endl;
			cout<<"Thank You"<<endl;
		}
		else
		{
		}

		if (item==c2||C2)
	{
		float G=2.00;
		float change=0;
		if(totalm>=G)
		{
			cout<<"Goose is purchased"<<endl;
			change=totalm-G;
			cout<<"Money deposited is"<<totalm<<"Your change is"<<change<<endl;
			cout<<"Thank You"<<endl;
		}
		else
		{
		}

			if (item==c3||C3)
	{
		float CO=1.50;
		float change=0;
		if(totalm>=CO)
		{
			cout<<"Condom is purchased"<<endl;
			change=totalm-CO;
			cout<<"Money deposited is"<<totalm<<"Your change is"<<change<<endl;
			cout<<"Thank You"<<endl;
		}
		else
		{
		}

			if (item==c4||C4)
	{
		float CH=0.50;
		float change=0;
		if(totalm>=CH)
		{
			cout<<"Chips is purchased"<<endl;
			change=totalm-CH;
			cout<<"Money deposited is"<<totalm<<"Your change is"<<change<<endl;
			cout<<"Thank You"<<endl;
		}
		else
		{
		}

			if (item==c5||C5)
	{
		float D=1.00;
		float change=0;
		if(totalm>=D)
		{
			cout<<"Dutch is purchased"<<endl;
			change=totalm-D;
			cout<<"Money deposited is"<<totalm<<"Your change is"<<change<<endl;
			cout<<"Thank You"<<endl;
		}
		else
		{
		}

			if (item==c6||C6)
	{
		float M=0.25;
		float change=0;
		if(totalm>=M)
		{
			cout<<"Mint is purchased"<<endl;
			change=totalm-G;
			cout<<"Money deposited is"<<totalm<<"Your change is"<<change<<endl;
			cout<<"Thank You"<<endl;
		}
		else
		{
		}
			}
			}

		return 0;
			}
			}
		}
	}

}
I tested it and it seems to work quite well for me.
Maybe I'm misunderstanding your problem?
after i enter "e" to end the depositing of the money, the program then asks the user to enter a selection. i enter a selection and the program ends and it not suppose to do that. im not sure how to get the program to run thru the rest of the code. im not sure what i have done wrong in the last codes of if else statements after the selection is entered by the user.
Oh, I see, I thought you meant it wasn't getting to the part where it asks for the item.
Just from looking over it I'm thinking it's a problem with your brackets. I'll give it a real look-over though.
thank you i appreciate your help
!
I'm an idiot.
The "Item" is an "int" rather than a string.
Try it with a string and it should be fine.
Also, your program would be a lot easier to read if you used switch-case instead of if statements. It's really easy to learn and looks very professional. :D
you know i was told switch statments were harder because you have to add the break that was by my own teacher. makes me think she didnt think i could learn it
shud i change it to a string within the if else statment too?
For the purpose of a class assignment it's probably a lot easier to just leave it as an int, and change the choices from "C1" and "C2" to just "1" and "2" in the if statements.
oh ok and if i dnt i need to change the if statements to a switch statements to use the "string"?
I'm sorry, I'm being really hasty in my responses, I've missed something else that was actually what was causing all your problems. At the top you've defined "C1" and all those to all equal 0. So the only way to get your program to work properly is to choose 0 as the item number. But then you buy everything. :P

So:
int c1=0, c2=0, c3=0, c4=0, c5=0, c6=0;
int C1=0, C2=0, C3=0, C4=0, C5=0, C6=0;

should be
int c1=1, c2=2, c3=3, c4=4, c5=5, c6=6;
int C1=1, C2=2, C3=3, C4=4, C5=5, C6=6;

Then you just have a little syntax issue to work out, but if I told you how to fix that it'd be cheating!

Just organize your code, or surprise your teacher and use a switch statement.
Last edited on
it still doesnt work for me.
this is what i did:

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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <iostream>
#include <iomanip>
#include <cctype>                                             
using namespace std;


int main()                                                    
{
char money;
double totalm = 0;
double value = 0, valued = 0, valuen = 0, valueq = 0, valueb = 0;
int c1=1, c2=2, c3=3, c4=4, c5=5, c6=6;
int C1=1, C2=2, C3=3, C4=4, C5=5, C6=6;
string item;

	cout<<"======Welcome to TD'S Vending======"<<endl;
	cout<<"    Depost Money                   "<<endl;
	cout<<"    Enter n or N for Nickel        "<<endl;
	cout<<"    Enter d or D for Dime          "<<endl;
	cout<<"    Enter q or Q for Quarter       "<<endl;
	cout<<"    Enter b or B for Dollar Bill   "<<endl;
	cout<<"    Enter e to end the deposit     "<<endl;
	cin>>money;

	while(money!='e' && money!='E')
	{
		if(money=='n' || money=='N')
		{
			double valuen=0.05;
			totalm+=valuen;
			cout<<"nickel is deposited, the total money deposited is "<<totalm<<endl;
			cin>>money;
		}
		if(money=='d' || money=='D')
		{
			double valued=0.10;
			totalm+=valued;
			cout<<"dime is deposited, the total money deposited is "<<totalm<<endl;
			cin>>money;
		}
		if(money=='q' || money=='Q')
		{
			double valueq=0.25;
			totalm+=valueq;
			cout<<"quarter is deposited, the total money deposited is "<<totalm<<endl;
			cin>>money;
		}
		if(money=='b' || money=='B')
		{
			double valueb=1.00;
			totalm+=valueb;
			cout<<"dollar is deposited, the total money deposited is "<<totalm<<endl;
			cin>>money;
		}
		else
		{
			cout<<"invalid entry, please try again"<<endl;
			cin>>money;
		}
	}

	cout<<"=========Welcome to TD'S Vending========="<<endl;
	cout<<"    Snickers     Goose     Condom        "<<endl;
	cout<<"     $0.85       $2.00     $1.50         "<<endl;
	cout<<"       C1          C2        C3          "<<endl<<endl;
	cout<<"     Chips       Dutch      Mint         "<<endl;
	cout<<"     $0.50       $1.00     $0.25         "<<endl;
	cout<<"       C4          C5        C6          "<<endl;
	cout<<"========================================="<<endl;
	cout<<"Please enter item number"<<endl;
	string messgae;

	if (item==c1 || item==C1)
	{
		double S=0.85;
		float change=0;
		if(totalm>=S)
		{
			cout<<"Snickers is purchased"<<endl;
			change=totalm-S;
			cout<<"Money deposited is"<<totalm<<"Your change is"<<change<<endl;
			cout<<"Thank You"<<endl;
		}
		else
		{
		}

		if (item==c2 || item==C2)
	{
		float G=2.00;
		float change=0;
		if(totalm>=G)
		{
			cout<<"Goose is purchased"<<endl;
			change=totalm-G;
			cout<<"Money deposited is"<<totalm<<"Your change is"<<change<<endl;
			cout<<"Thank You"<<endl;
		}
		else
		{
		}

			if (item==c3 || item==C3)
	{
		float CO=1.50;
		float change=0;
		if(totalm>=CO)
		{
			cout<<"Condom is purchased"<<endl;
			change=totalm-CO;
			cout<<"Money deposited is"<<totalm<<"Your change is"<<change<<endl;
			cout<<"Thank You"<<endl;
		}
		else
		{
		}

			if (item==c4 || item==C4)
	{
		float CH=0.50;
		float change=0;
		if(totalm>=CH)
		{
			cout<<"Chips is purchased"<<endl;
			change=totalm-CH;
			cout<<"Money deposited is"<<totalm<<"Your change is"<<change<<endl;
			cout<<"Thank You"<<endl;
		}
		else
		{
		}

			if (item==c5 || item==C5)
	{
		float D=1.00;
		float change=0;
		if(totalm>=D)
		{
			cout<<"Dutch is purchased"<<endl;
			change=totalm-D;
			cout<<"Money deposited is"<<totalm<<"Your change is"<<change<<endl;
			cout<<"Thank You"<<endl;
		}
		else
		{
		}

			if (item==c6 || item==C6)
	{
		float M=0.25;
		float change=0;
		if(totalm>=M)
		{
			cout<<"Mint is purchased"<<endl;
			change=totalm-G;
			cout<<"Money deposited is"<<totalm<<"Your change is"<<change<<endl;
			cout<<"Thank You"<<endl;
		}
		else
		{
		}
			}
			}

		return 0;
			}
			}
		}
	}




}
Last edited on
disrecharge what i posted im sleeping and i realized what i was doing wrong
Topic archived. No new replies allowed.