C++ Vending Machine Program

Thank you, changing the assignment operator worked but I am still having an issue. I thought something was wrong with my exit condition for my if statements that allow the user to end the program but now after a second entry my program just runs out of control and I don't know why. Below is my 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
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
//Vending Machine Program
#include <iostream>;
using namespace std;

int main()
{
	double product1 = 59.99;
	double product2 = 59.99;
	double product3 = 59.99;
	double product4 = 59.99;
	double customerMoney = 0.00;

	cout << "How much money do you have? ";
	cin >> customerMoney;

	while (customerMoney >= 59.99)
	{
		int customerChoice = 0;
		bool exitProgram = NULL;

		cout << "Which product would you like to buy?\n Enter '1' for Killzone: Shadow Fall ($59.99)\n Enter '2' for Deadrising 3 ($59.99)\n Enter '3' for Call of Duty Ghosts ($59.99)\n Enter '4' for Madden NFL 25 ($59.99)";
		cin >> customerChoice;
		if (customerChoice == 1)
		{
			cout << "You have selected Killzone: Shadow Fall\n";
			customerMoney = customerMoney - product1;
			cout << "You have $" << customerMoney << " left\n";

			cout << "Would you like to exit the program? (Enter '1' for true or '2' for false)";
			cin >> exitProgram;
			if (exitProgram == 1)
			{
				return 0;
			}
			else if (exitProgram == 2)
			{
				break;
			}
		}
		else if (customerChoice == 2)
		{
			cout << "You have selected Deadrising 3\n";
			customerMoney = customerMoney - product2;
			cout << "You have $" << customerMoney << " left\n";

			cout << "Would you like to exit the program? (Enter '1' for true or '2' for false)";
			cin >> exitProgram;
			if (exitProgram == 1)
			{
				return 0;
			}
			else if (exitProgram == 2)
			{
				break;
			}
		}
		else if (customerChoice == 3)
		{
			cout << "You have selected Call of Duty Ghosts\n";
			customerMoney = customerMoney - product3;
			cout << "You have $" << customerMoney << " left\n";

			cout << "Would you like to exit the program? (Enter '1' for true or '2' for false)";
			cin >> exitProgram;
			if (exitProgram == 1)
			{
				return 0;
			}
			else if (exitProgram == 2)
			{
				break;
			}
		}
		else if (customerChoice == 4)
		{
			cout << "You have selected Madden NFL 25\n";
			customerMoney = customerMoney - product4;
			cout << "You have $" << customerMoney << " left\n";

			cout << "Would you like to exit the program? (Enter '1' for true or '2' for false)";
			cin >> exitProgram;
			if (exitProgram == 1)
			{
				return 0;
			}
			else if (exitProgram == 2)
			{
				break;
			}
		}
		/*else
		{
			cout << "You don't have enough money to purchase anything else. Thank you for shopping!";
		}*/
	}

	cin.ignore();
	cin.get();
	return 0;
}
Last edited on
You have to do:
if(customerChoice == SOME_NUM)

This is why:
The "=" operator is the assignment operator in C++, it assigns a value to the variable to the left side the the operator.

The "==" (double equals) is the equality operator in C++, it checks whether the left and right hand side are equal or not.

Same case for the exitProgram. It should be if(exitProgram == 1).
Last edited on
There's a trick to avoid making that mistake : always put the constant value to the left of the equality operator (ie write if (1 == customerChoice)).
That way, every time you'll use "=" instead of "==" the compiler will issue an error.
Thank you, changing the assignment operator worked but I am still having an issue. I thought something was wrong with my exit condition for my if statements that allow the user to end the program but now after a second entry my program just runs out of control and I don't know why.
not sure if im right about this but I think its because "return 0" exits the program and "break" exits the loop [in short, even if you choose 1 or 2 they'd still exit the program]. just a suggestion, why not turn exitProgram into an int and change while (customerMoney >= 59.99) to while (customerMoney >= 59.99 && exitProgram == 1) (note: you'd have to initialize exitProgram to 1 [exitProgram = 1] for it to enter the loop).

Also, I though of some changes that you might like to put in your code. its your choice if you want to do it though.

since your choices are constant (i.e input 1 for product1, 2 for product2, etc...). why not try to use "switch" instead? (just a suggestion though: http://www.cplusplus.com/doc/tutorial/control/ ). don't know if you know this but if you read the link I gave, its just the same as the if and ifelse statement except the values given are constant (i.e "a == b" only. not "a < b", not "a>b" etc...).

2nd, i've noticed that in evrey "if" there's a "would you like to exit this program?" and so forth in you code. i'd suggest you turn this into a function to lessen the lines.

i've edited your code so that you can understand what "switch" and "functions" mean (just in case if you don't know)

for switch: http://www.cplusplus.com/doc/tutorial/control/

for functions: http://www.cplusplus.com/doc/tutorial/functions/

p.s: you can also use switch for your products1,2,3 etc... too

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
#include <iostream>
#include <stdlib.h>
using namespace std;

///this is a function
int exitP(int x){

    cout<<"\nWould you like to exit the program? [1 -> yes :: 2 -> no]: " ;
    cin>>x;

///this is a switch
    switch(x){
    case 1: cout<<"Goodbye!";
            exit(EXIT_SUCCESS);
            break;
    case 2: return x;
            break;
    default: cout<<"Pls enter numbers 1-2 only. ";
             exitP(x);
             break;
    }

}

int main(){

double product1 = 59.99;
	double product2 = 59.99;
	double product3 = 59.99;
	double product4 = 59.99;
	double customerMoney = 0.00;
	int exitProgram = 1;

	cout << "How much money do you have? ";
	cin >> customerMoney;

	while (customerMoney >= 59.99 && exitProgram == 1)
	{
		int customerChoice = 0;

		cout << "Which product would you like to buy?\n Enter '1' for Killzone: Shadow Fall ($59.99)\n Enter '2' for Deadrising 3 ($59.99)\n Enter '3' for Call of Duty Ghosts ($59.99)\n Enter '4' for Madden NFL 25 ($59.99)"
		<<"\n\nChoice: ";
		cin >> customerChoice;
		if (customerChoice == 1)
		{
			cout << "You have selected Killzone: Shadow Fall\n";
			customerMoney = customerMoney - product1;
			cout << "You have $" << customerMoney << " left\n";
                        
                       ///this is what a function is used for
			exitP(exitProgram);
		}
		else if (customerChoice == 2)
		{
			cout << "You have selected Deadrising 3\n";
			customerMoney = customerMoney - product2;
			cout << "You have $" << customerMoney << " left\n";


                       ///this is what a function is used for
			exitP(exitProgram);
		}
		else if (customerChoice == 3)
		{
			cout << "You have selected Call of Duty Ghosts\n";
			customerMoney = customerMoney - product3;
			cout << "You have $" << customerMoney << " left\n";

                       ///this is what a function is used for
			exitP(exitProgram);
		}
		else if (customerChoice == 4)
		{
			cout << "You have selected Madden NFL 25\n";
			customerMoney = customerMoney - product4;
			cout << "You have $" << customerMoney << " left\n";

                       ///this is what a function is used for
			exitP(exitProgram);
		}

	}

	cout << "You don't have enough money to purchase anything else. Thank you for shopping!";



	cin.ignore();
	cin.get();
	return 0;

}

Last edited on
Thank you for the advice. I changed the bool to an integer and changed al my breaks to continues and now my program works perfectly fine. Also the reason I didn't use a switch statements or functions was because for this assignment we were only allowed to use certain things and we could not use switch or functions but thank you regardless I really appreciate it.
Topic archived. No new replies allowed.