Skip to content Skip to sidebar Skip to footer

Write a Function That Will Help a Vending Machine

How to make vending machine program

Hello. I was assigned a project to make a program that will be like a vending machine. The vending machine has 3 drinks. With only 10 bottles of each. It has to keep on asking what flavor you want till the drink runs out. If just one drink runs out, it has to tell you to make another selection. If all run out, it must end the program. It also has to ask to put in change with dollar bills, quarters, nickels, and dimes. It has to give you change too. I have started making it, but have no idea what I am doing. I can't get it to stop working after you select a drink 10 times. I have tried for loops and while loops. I am really stuck as I am a beginner. I have some failed attempts in the ignored texts. My coding sucks. I would really appreciate some guidance with 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
                                                      #include "stdafx.h"                            int                            main() { 	printf("Arya Vending Machine\n\n");                            int                            Coke, Pepsi, Water, Choice;                            int                            Dollar = 1;                            int                            Quarter = 0.25;                            int                            Dime = 0.10;                            int                            Nickel = 0.05;                            int                            x = 10;                            int                            y = 10;                            int                            z = 10;                            /*for (int x = 10; x > 0; x--) { 		for (int y = 10; y >  0; y--) { 			for (int z = 10; z > 0; z--) */                            //if ((C <= 10) && (P <= 10) && (W <= 10)) {                            while                            (x > 0) {                            while                            (y > 0) {                            while                            (z > 0) { 				printf("Drinks Available\n1: Coke: $0.55\n2: Pepsi: $0.45\n3: Water: $0.85\n\nPlease enter number corresponding to the drink\n"); 				scanf("%d", &Choice);                            switch                            (Choice) {                            case                            1: 					x--; 					printf("Coke\n");                            break;                            case                            2: 					y--; 					printf("Pepsi\n");                            break;                            case                            3: 					z--; 					printf("Water\n");                            break;                            default: 					printf("Your entry was invalid. Please try again.\n"); 				} 			} 		} 	} 			printf("You ran out");                            //	return 0;                            }                                                  

Hi, you are in the right track. Here are some changes I made to your code.
Notice for the dollars etc I change them to float from int as you are using a decimal point. The x,y,z are redundant as you can just use the coke, pepsi and water vars. I changed up your while loops also look over the differences in them.
Should get you going again.

                          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
                                                      #include <iostream>                            using                            namespace                            std;                            int                            main() {                            int                            Coke = 10;                            int                            Pepsi = 10;                            int                            Water = 10;                            int                            Choice;                            float                            Dollar = 1, Quarter = 0.25, Dime = 0.10, Nickel = 0.05;  	cout <<                            "Arya Vending Machine\n\n";                            while                            (Coke > 0 || Pepsi >0 || Water > 0  ) { 		printf("\nDrinks Available\n1: Coke: $0.55\n2: Pepsi: $0.45\n3: Water: $0.85\n\nPlease enter number corresponding to the drink\n"); 		cin >> Choice;                            switch                            (Choice) {                            case                            1:                            if                            (Coke > 0) { 				Coke--; 				cout << Coke <<                            " Coke left\n"; 			}                            else{ 				cout <<                            "\nNo Coke left\n"; 			}                            break;                            case                            2:                            if                            (Pepsi > 0) { 				Pepsi--; 				cout << Pepsi <<                            " Pepsi left\n"; 			}                            else                            { 				cout <<                            "\nNo Pepsi left\n"; 			}                            break;                            case                            3:                            if                            (Water > 0) { 				Water--; 				cout << Water <<                            " Water left\n"; 			}                            else                            { 				cout <<                            "\nNo Water left\n"; 			}                            break;                            default: 			printf("Your entry was invalid. Please try again.\n"); 		} 	}  	printf("\n***You ran out\n");  	system("pause");                            //	return 0;                            }                        

You might want to have a look at this. It would stop serving drinks if there are none left and it will exit if you ask for something that is not "on the menu" / if you provide invalid input.

I expect you will be able to adapt this to your needs.

                          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
                                                      #include <iostream>                            int                            inventory[3]={10,10,10};                            int                            getDrink(int                            choice) {                            if                            (choice > 0 && choice <                            sizeof(inventory)/sizeof(inventory[0]) )                            // valid input                            {                            if                            (inventory[choice]>0)         {             inventory[choice]--;             std::cout <<                            "Here is your drink."                            << std::endl;         }                            else                            std::cout <<                            "We are all out of those. Try across the street."                            << std::endl;                            return                            1;     }     std::cout <<                            "That is not on todays menu."                            << std::endl;                            return                            0; }                            int                            main() {                            int                            choice = -1;                            int                            result = 0;                            do                            {         std::cout <<                            "If you give me all your money, what would you like in return?"                            << std::endl;         std::cin >> choice;         result = getDrink(choice);     }                            while                            (result == 1);                            return                            0; }                        

Pretty good nico.

Here is a bit more of an OOP approach.

inv.h;

                          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
                                                      #include <sstream>                            using                            namespace                            std;                            class                            inventory {                            struct                            drink 	{ 		string name;                            float                            price;                            int                            quantity; 	};  	drink inv[3];                            public: 	inventory(); 	string getCurrentdrinks();                            void                            sellDrink(int                            i); 	string getDrinkName(int                            i);                            int                            getQuantity(int                            i);                            int                            getSelectionSize();                            bool                            hasInv(); };  inventory::inventory() { 	inv[0].name =                            "coke"; 	inv[0].price = 0.55; 	inv[0].quantity = 10;  	inv[1].name =                            "pepsi"; 	inv[1].price = 0.45; 	inv[1].quantity = 10;  	inv[2].name =                            "water"; 	inv[2].price = 0.85; 	inv[2].quantity = 10; }  string inventory::getCurrentdrinks() { 	ostringstream oss; 	string str; 	oss <<                            "\nDrinks Available\n";                            for                            (int                            i = 0; i < (sizeof(inv) /                            sizeof(*inv)); i++) { 		oss << (i) <<                            ": "                            << inv[i].name <<                            ": $"                            << inv[i].price <<                            "\n"; 	}                            return                            str = oss.str();; }                            void                            inventory::sellDrink(int                            i) { 	inv[i].quantity--; }  string inventory::getDrinkName(int                            i) {                            return                            inv[i].name; }                            int                            inventory::getQuantity(int                            i) {                            try                            {                            return                            inv[i].quantity; 	}                            catch                            (exception ex) {                            return                            -1; 	} }                            int                            inventory::getSelectionSize() {                            return                            (sizeof(inv) /                            sizeof(*inv)); }                            bool                            inventory::hasInv() {                            int                            val = 0;                            for                            (int                            i = 0; i < (sizeof(inv) /                            sizeof(*inv)); i++) { 		val += inv[i].quantity; 	}                            if                            (val > 0) {                            return                            1; 	}                            else                            {                            return                            0; 	} }                        

Main.cpp

                          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
                                                      #include <iostream>                            #include "inv.h"                            using                            namespace                            std;                            int                            main() { 	inventory inv;                            int                            Choice;                            //int Dollar = 1;                            //float Quarter = 0.25, Dime = 0.10, Nickel = 0.05;                            cout <<                            "Arya Vending Machine\n\n";                            while                            (inv.hasInv()) { 		cout << inv.getCurrentdrinks(); 		cin >> Choice;                            if                            (Choice > inv.getSelectionSize() || Choice < 0) { 			cout <<                            "Invalid Selection\n"; 		}                            else                            {                            if                            (inv.getQuantity(Choice) > 0) { 				inv.sellDrink(Choice); 				cout << inv.getQuantity(Choice) <<                            " "                            << inv.getDrinkName(Choice) <<                            " left\n"; 			}                            else                            { 				cout <<                            "\nNo "                            << inv.getDrinkName(Choice) <<                            "left\n"; 			} 		} 	}  	printf("\n***You ran out***\n");  	system("pause"); }                        

Hi switchy. I modified your code to work on mine with stdafx.h. That is what I need to use. Thank you so much. Except. How would I make it so you have to "enter in" coins or a dollar bill into it? It would need to also give you back your change. Where would I start? Thank you so much for your help. Here is the 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
                                                      #include "stdafx.h"                            using                            namespace                            std;                            int                            main() {                            int                            Coke = 10;                            int                            Pepsi = 10;                            int                            Water = 10;                            int                            Choice;                            float                            Dollar = 1, Quarter = 0.25, Dime = 0.10, Nickel = 0.05;  	printf("Arya Vending Machine");                            while                            (Coke > 0 || Pepsi >0 || Water > 0) { 		printf("\nDrinks Available\n1: Coke: $0.55\n2: Pepsi: $0.45\n3: Water: $0.85\n\nPlease enter number corresponding to the drink\n"); 		scanf("%d", &Choice);                            switch                            (Choice) {                            case                            1:                            if                            (Coke > 0) { 				Coke--; 				printf("There is %d Coke left", Coke); 			}                            else                            { 				printf("\nNo Coke left\n"); 			}                            break;                            case                            2:                            if                            (Pepsi > 0) { 				Pepsi--; 				printf("There is %d Pepsi left", Pepsi); 			}                            else                            { 				printf("No Pepsi left"); 			}                            break;                            case                            3:                            if                            (Water > 0) { 				Water--; 				printf("There is %d water left", Water); 			}                            else                            { 				printf("No water left"); 			}                            break;                            default: 			printf("Your entry was invalid. Please try again.\n"); 		} 	}  	printf("\n***You ran out\n");                            return                            0;  }                        

Hi, maybe have your coins as int like you did before and have it such as quarter = 25. Multiply the users input by 100 and keep looping until the value is less than the current coin. For example

                          1
2
3
4
5
6
7
8
9
10
                                                      while                            (input >= 100)  { input -= 100; dollarCount++;                            while                            (input >= 25)  { input -= 25; quarterCount++; }                        

There is likely a more efficient way but that is just what I thought of first.

Last edited on

Using an int to represent money is a very good idea, it will ensure that you don't get any trouble with inaccuracies as you may have when you use a float or double.

As for how to calculate the number of dollars, have a look at the modulo operator.
http://www.cplusplus.com/doc/tutorial/operators/

                          1
2
3
4
                                                      int                            dollars = input / 100;                            int                            remainder = input % 100;                            int                            quarters = remainder / 25;                            int                            cents = remainder % 25;                        

Hello. Here is what I tried. I still have no idea what I'm doing. I need to touch up on the little C++ I know. At first, I tried making a function to ask for the money. I could not figure out how to make it use the Dinero int from the function in the main. I tried doing pointers, but I failed. I also couldn't figure out how to make it stop with 3 different amounts, since the prices are different. Should I make 3 different functions? I then just added the code to only the Coke for testing purposes. What I tried doing is using a do loop where it will keep on asking to put in more money till the Money is equal or greater than the price. I also couldn't align the {} brackets with the right one. Am I on the right track? How do I fix it? Thank you.

                          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
                                                      #include "stdafx.h"                            using                            namespace                            std;                            void                            EnterMoney(int                            Dinero,                            int                            Nickel,                            int                            Dime,                            int                            Quarter,                            int                            Dollar);                            int                            main() {                            int                            Coke = 10;                            int                            Pepsi = 10;                            int                            Water = 10;                            int                            Choice, Price;                            int                            Quarter = 25;                            int                            Dime = 10;                            int                            Nickel = 5;                            int                            Dollar = 100;                            int                            Money;  		printf("Arya Vending Machine");                            while                            (Coke > 0 || Pepsi > 0 || Water > 0) {                            do                            { 				printf("\nDrinks Available\n1: Coke: $0.55\n2: Pepsi: $0.45\n3: Water: $0.85\n\nPlease enter number corresponding to the drink\n"); 				scanf("%d", &Choice);                            switch                            (Choice) {                            case                            1:                            do                            { 					printf("Please add money.\n 1 for a Nickel\n2 for a Dime\n 3 for a quarter\n 4 for a dollar"); 					scanf("%d", &Money);                            switch                            (Money) { 						Price = 45;                            case                            1: 						Money = Money + 5;                            break;                            case                            2: 						Money = Money + 10;                            break;                            case                            3: 						Money = Money + 25;                            break;                            case                            4: 						Money = Money + 100;                            break;                            default: 						printf("Your entry is invalid. Please try again");  					 				} 				}                            while                            (Money >= 45);                            if                            (Coke > 0) { Coke--; 					printf("There is %d Coke left", Coke); 				}                            case                            2:                            if                            (Pepsi > 0) { 				Price = 55; 				Pepsi--; 				printf("There is %d Pepsi left", Pepsi); 			}                            else                            { 				printf("No Pepsi left"); 			}                            break;                            case                            3:                            if                            (Water > 0) { 				Price = 85; 				Water--; 				printf("There is %d water left", Water); 			}                            else                            { 				printf("No water left"); 			}                            break;                            default: 			printf("Your entry was invalid. Please try again.\n"); 		} 	}  	printf("\nThere are no more drinks left. Have a nice day!\n");                            return                            0;  }                            /*void EnterMoney (int Dinero, int Nickel, int Dime, int Quarter, int Dollar, int *Price) { 		while (Dinero >= *Price); { 	printf("Please add money.\n 1 for a Nickel\n2 for a Dime\n 3 for a quarter\n 4 for a dollar"); 	scanf("%d", &Dinero); 	switch (Dinero) { 	case 1: 		Dinero = Dinero + 5; 		break; 	case 2: 		Dinero = Dinero + 10; 		break; 	case 3: 		Dinero = Dinero + 25; 		break; 	case 4: 		Dinero = Dinero + 100; 		break; 	default: 		printf("Your entry is invalid. Please try again"); 	} 	} 		return Dinero; }*/                                                  

Hello,

You declared the function as: void EnterMoney(arguments);
"void" means: this function will not return anything.
As a result, your compiler should complain at line 100, because there you try to return an integer, which is not nothing.
Try declaring the function as: int EnterMoney(arguments) both in line 79 and line 3.
Note that in this case, the int Dinero arguments becomes useless, you could just declare the Dinero variable inside the function and initialize it to 0 for any situation.

Another way would be to call the function with the Dinero variable "by reference" instead of "by value". Have a lood at these:
https://www.tutorialspoint.com/cplusplus/cpp_function_call_by_value.htm
https://www.tutorialspoint.com/cplusplus/cpp_function_call_by_reference.htm
If you send the variable from the main function by reference, any change that is made inside the function will immediately update the variable in the main function, so you would not need to return the Dinero variable anymore.

---------------

As for the function arguments, it seems you never use ", int Nickel, int Dime, int Quarter, int Dollar", so I would remove those. The function declaration would then become:
void EnterMoney (int &Dinero, int Price); or int EnterMoney (int Price);

You provide Price as a pointer, you could just do the same as with the other variables. If you want to change the price, you should calculate the price before you call the function. For example, for a Pepsi you would call it like:

                          1
2
                                                      int                            receivedCash = 0; EnterMoney (receivedCash, 55);                        

or
int receivedCash = EnterMoney (55);

Topic archived. No new replies allowed.

Write a Function That Will Help a Vending Machine

Source: http://www.cplusplus.com/forum/beginner/199201/

Post a Comment for "Write a Function That Will Help a Vending Machine"