Vending machine project

So for my college course I've been told to make a vending machine project, I've got the money side of things to work but I can only get it to output the option for crisps whatever input I use, can somebody help me out?

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
#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;
	int c = 1, m = 2, p = 3, k = 4, w = 5;
	int C = 1, M = 2, P = 3, K = 4, W = 5;
	char item;
	cout << "Products" << endl;
	cout << "" << endl;
	cout << "C= Crisps		40p" << endl;
	cout << "M= Milk Chocolate Bar	60p" << endl;
	cout << "P= Plain Chocolate Bar	60p" << endl;
	cout << "K= Cola			30p" << endl;
	cout << "W= Water		30p" << endl;
	cout << "	" << endl;
	cout << "Please insert credit. '1'=10p, '2'=20p and '5'=50p" << endl;
	cout << "After you have inserted the money enter 'E' to end the deposit" << endl;
	cout << "	" << endl;
	cin >> money;

	while (money != 'e' && money != 'E')
	{
		if (money == '1')
		{
		double valueq = 0.10;
		totalm += valueq;
		cout << "10p, the total money deposited is " << setprecision(2) << fixed << char(156) << totalm << endl;
		cin >> money;
		}
		else {
			if (money == '2')
			{
			double valueq = 0.20;
			totalm += valueq;
			cout << "20p, the total money deposited is " << setprecision(2) << fixed << char(156) << totalm << endl;
				cin >> money;
			}
			else {
				if (money == '5')
				{
				double valueq = 0.50;
				totalm += valueq;
				cout << "50p, the total money deposited is " << setprecision(2) << fixed << char(156) << totalm << endl;
				cin >> money;
				}
				else
				{
				cout << "Invalid entry, please try again" << endl;
				cin >> money;
				}
			}
		}
	}

	cout << "Please make your selection" << endl;
	cin >> item;

	if (item == c || C)
	{
	double Z = 0.40;
	float change = 0;
	if (totalm >= Z)
		{
		cout << "Crisps have been purchased" << endl;
		change = totalm - Z;
		cout << "Money deposited is " << setprecision(2) << fixed << char(156) << totalm << endl;
		cout << "Your change is " << setprecision(2) << fixed << char(156) << change << endl;
		cout << "Thank You" << endl;
		cout << "	" << endl;
		}
	}
	else if (item == m || M)
	{
	float Y = 0.60;
	float change = 0;
	if (totalm >= Y)
		{
		cout << "Milk Chocolate has been purchased" << endl;
		change = totalm - Y;
		cout << "Money deposited is " << setprecision(2) << fixed << char(156) << totalm << endl;
		cout << "Your change is " << setprecision(2) << fixed << char(156) << change << endl;
		cout << "Thank You" << endl;
		cout << "	" << endl;
		}
	}

	else if (item == p || P)
	{
	float X = 0.60;
	float change = 0;
	if (totalm >= X)
		{
		cout << "Plain Chocolate has been purchased" << endl;
		change = totalm - X;
		cout << "Money deposited is " << setprecision(2) << fixed << char(156) << totalm << endl;
		cout << "Your change is " << setprecision(2) << fixed << char(156) << change << endl;
		cout << "Thank You" << endl;
		cout << "	" << endl;
		}
	}

	else if (item == k || K)
	{
	float V = 0.30;
	float change = 0;
	if (totalm >= V)
		{
		cout << "Cola has been purchased" << endl;
		change = totalm - V;
		cout << "Money deposited is " << setprecision(2) << fixed << char(156) << totalm << endl;
		cout << "Your change is " << setprecision(2) << fixed << char(156) << change << endl;
		cout << "Thank You" << endl;
		cout << "	" << endl;
		}
	}
	
	else if (item == w || W)
	{
	float U = 0.30;
	float change = 0;
	if (totalm >= U)
		{
		cout << "Water has been purchased" << endl;
		change = totalm - U;
		cout << "Money deposited is " << setprecision(2) << fixed << char(156) << totalm << endl;
		cout << "Your change is " << setprecision(2) << fixed << char(156) << change << endl;
		cout << "Thank You" << endl;
		cout << "	" << endl;
		}
	}
	else
	{
	cout << "Invalid selection, please try again" << endl;
	}

	system("pause");
	exit(0);
	}
Last edited on
You need to fix your tests for item to this:

1
2
3

if( item == w || item ==W )


What you have says:

if item is equal to k or if K is non-zero!
Thanks for the reply, I've just tried that and now whatever input I use I get the "Invalid selection, please try again" output
Let's see your new version.
Sure thing, it's:
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
#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;
	int c = 1, m = 2, p = 3, k = 4, w = 5;
	int C = 1, M = 2, P = 3, K = 4, W = 5;
	char item;
	cout << "Products" << endl;
	cout << "" << endl;
	cout << "C= Crisps		40p" << endl;
	cout << "M= Milk Chocolate Bar	60p" << endl;
	cout << "P= Plain Chocolate Bar	60p" << endl;
	cout << "K= Cola			30p" << endl;
	cout << "W= Water		30p" << endl;
	cout << "	" << endl;
	cout << "Please insert credit. '1'=10p, '2'=20p and '5'=50p" << endl;
	cout << "After you have inserted the money enter 'E' to end the deposit" << endl;
	cout << "	" << endl;
	cin >> money;
	while (money != 'e' && money != 'E')
	{
		if (money == '1')
		{
			double valueq = 0.10;
			totalm += valueq;
			cout << "10p, the total money deposited is " << setprecision(2) << fixed << char(156) << totalm << endl;
			cin >> money;
		}
		else {
			if (money == '2')
			{
				double valueq = 0.20;
				totalm += valueq;
				cout << "20p, the total money deposited is " << setprecision(2) << fixed << char(156) << totalm << endl;
				cin >> money;
			}
			else {
				if (money == '5')
				{
					double valueq = 0.50;
					totalm += valueq;
					cout << "50p, the total money deposited is " << setprecision(2) << fixed << char(156) << totalm << endl;
					cin >> money;
				}
				else
				{
					cout << "Invalid entry, please try again" << endl;
					cin >> money;
				}
			}
		}
	}

	cout << "Please make your selection" << endl;
	cin >> item;

	if (item ==c || item ==C)
	{
		double Z = 0.40;
		float change = 0;
		if (totalm >= Z)
		{
			cout << "Crisps have been purchased" << endl;
			change = totalm - Z;
			cout << "Money deposited is " << setprecision(2) << fixed << char(156) << totalm << endl;
			cout << "Your change is " << setprecision(2) << fixed << char(156) << change << endl;
			cout << "Thank You" << endl;
			cout << "	" << endl;
		}
	}
	else if (item ==m || item ==M)
	{
		float Y = 0.60;
		float change = 0;
		if (totalm >= Y)
		{
			cout << "Milk Chocolate has been purchased" << endl;
			change = totalm - Y;
			cout << "Money deposited is " << setprecision(2) << fixed << char(156) << totalm << endl;
			cout << "Your change is " << setprecision(2) << fixed << char(156) << change << endl;
			cout << "Thank You" << endl;
			cout << "	" << endl;
		}
	}
	else if (item ==p || item ==P)
	{
		float X = 0.60;
		float change = 0;
		if (totalm >= X)
		{
			cout << "Plain Chocolate has been purchased" << endl;
			change = totalm - X;
			cout << "Money deposited is " << setprecision(2) << fixed << char(156) << totalm << endl;
			cout << "Your change is " << setprecision(2) << fixed << char(156) << change << endl;
			cout << "Thank You" << endl;
			cout << "	" << endl;
		}
	}
	else if (item ==k || item ==K)
	{
		float V = 0.30;
		float change = 0;
		if (totalm >= V)
		{
			cout << "Cola has been purchased" << endl;
			change = totalm - V;
			cout << "Money deposited is " << setprecision(2) << fixed << char(156) << totalm << endl;
			cout << "Your change is " << setprecision(2) << fixed << char(156) << change << endl;
			cout << "Thank You" << endl;
			cout << "	" << endl;
		}
	}
	else if (item ==w || item ==W)
	{
		float U = 0.30;
		float change = 0;
		if (totalm >= U)
		{
			cout << "Water has been purchased" << endl;
			change = totalm - U;
			cout << "Money deposited is " << setprecision(2) << fixed << char(156) << totalm << endl;
			cout << "Your change is " << setprecision(2) << fixed << char(156) << change << endl;
			cout << "Thank You" << endl;
			cout << "	" << endl;
		}
	}
	else
	{
			cout << "Invalid selction, please try again" << endl;
	}

		system("pause");
		exit(0);
	}
Last edited on
What is it that you want your user to input for, say, crisps? The letter 'C'? Or the number '1'? Because your help text suggests that it's the letter, and you read the input as a character, but at lines 64, 78, etc, you're comparing the integer value of that character to the values 1, 2, etc.
Preferably the letter format, so C for Crisps etc but if it's easier to do it the other way then I'm happy to change it. (As you can probably tell I'm new to this....)
You forgot to enclose your character comparisons. Should be if(input == 'W' || input == 'w') because the way you have it you are comparing to the variable w and W which you have initialized with the value 5 (which in ascii is a non-printable character iirc).
Wow that was stupid of me... Thank you! I would have been there for days and made no progress otherwise
Topic archived. No new replies allowed.