Vending Machine Code

***edited 4-24-13***

The problem here is that I have been asked to use Functions to create the code and i dont really know quite how to do that. Other than "main" im suppose to use a function for cash tendered, menu list, customer choice, and pin entry. Im expected to use constants for the shutdown pin, the cost of food items, and the number of customers who can use the machine before it shuts down. (i think i have that done correctly) My variables will need to be fixed as im expected to use local instead of global but i havent made that change completely because i cant compile and i dont quite get what i should be doing to fix it or get the variables correct. Can anyone offer a bit of guidance as to how to break this up into functions correctly or at least patch the code enough to make it work?

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
#include <iostream>
#include <iomanip>
using namespace std;
const int CUSTOMER = 100;
const int SHUTDOWN = 9110;
const double beverage = 5.00;
const double candy = 2.25;
const double hotdog = 7.00;
const double popcorn = 6.75;
const double nachos = 4.50;
char getMenu (void);
char getSelection (void);
double getTendered (void);
int getPin (void);
//Please note that some variables are missing as i dont quite know how to list them yet
int main()
{
char ch;
do
{
system("cls");
getPin();
getMenu();
getSelection();
getTendered();
}
while (ch=='Y'); 
return 0;
}
int getPin(void)
{
int pin;
do
{
system("cls");
cout<<"Welcome to the Movie Food System\n";
cout<<"Please enter your 4-digit pin code: ";
cin>>pin;
cin.clear();
cin.ignore(10,'\n');
} 
while (pin<1000 || pin>9999);
return pin;
} 
char getMenu(void)
{
char ch;
do
{
ch = '\n';
system("cls");
cout<<"Please select a Snack\n"
<<"B - Beverage $5.00\n"
<<"C - Candy $2.25\n"
<<"H - Hot Dog $7.00\n"
<<"N - Nachos $4.50\n"
<<"P - Popcorn $6.75\n"
<<"Q - Quit"<<endl;
cin>>ch; 
ch = toupper(ch);
}
while (ch!='B' && ch!='C' && ch!='H' && ch!='N' && ch!='P' && ch!='Q');
return ch;
if (ch == 'Q') 
{

}      
char getSelection(void)
{
char ch;
double cost;
do
{
cout<<"\nYou selected ";
switch(ch)
{
case 'B': cout<<"Beverage"<<endl; cost = beverage; break;
case 'C': cout<<"Candy"<<endl; cost = candy; break;
case 'H': cout<<"Hot Dog"<<endl; cost = hotdog; break;
case 'N': cout<<"Nachos"<<endl; cost = nachos; break;
case 'P': cout<<"Popcorn"<<endl; cost = popcorn; break;
}
double getTendered(void)
{

do
{
cout<<"Please insert "<<cost<<"\nEnter amount tendered ";
cin>>tendered;
cin.clear();
cin.ignore(10,'\n');
cost = cost - tendered;
} 
while (cost>0);
cost = (-1 * cost);
if (cost>0) 
{
cout<<"Change: "<<cost<<endl; 
}
system("pause");
} 
while (counter!= CUSTOMER);
{
cout<<endl;
cout<<"This unit is shutting down to restock"<<endl<<endl;
system("pause");
return 0;
}
Last edited on
collegeconfused wrote:
i dont want to break a program that is already working
Then make a copy of your code.

There are some great resources on functions allready available on this website.
Take a look and see if it helps.

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

http://www.cplusplus.com/doc/tutorial/functions2/
Code has been changed. Still isnt working correctly and obviously wont compile in this condition.
Your prof wants you to use different functions for different tasks. Functions break the code up in to smaller pieces that serve specific roles

e.g
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>
#include <iomanip>
#include <string>

using namespace std;

void PrintMenu() {
	cout << "------------ MENU ---------------- " << endl;
	for (unsigned i = 0; i < 10; ++i)
		cout << setw(20) << std::left << "Chips" << "$1.50" << endl;
}

string GetUserChoice() {
	string input = "";
	cout << "Please enter your choice: ";
	getline(cin, input);
	return input;
}



int main() {
	PrintMenu();

	string choice = GetUserChoice();
	if (choice == "q" || choice == "quit") {
		cout << "Quitting" << endl;
		return 0;
	}

	cout << "Doing other crap with your choice of " << choice << endl;
	return 0;
}


have a read through http://www.cplusplus.com/forum/articles/6046/ to understand how to deal with user input as well.
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
#include <iostream>
#include <iomanip>
using namespace std;
const int CUSTOMER = 100;
const int SHUTDOWN = 9110;
const double beverage = 5.00;
const double candy = 2.25;
const double hotdog = 7.00;
const double popcorn = 6.75;
const double nachos = 4.50;
char getMenu (void);
char getSelection (void);
double getTendered (void);
int getPin (void);
int main()
{
char ch;
int pin, counter = 0;
do
{
system("cls");
pin = getPin();
if (pin==SHUTDOWN)
{break;}
getMenu();
getSelection();
getTendered();
}
while (counter!=CUSTOMER); 
cout<<endl;
cout<<"This unit is shutting down to restock"<<endl<<endl;
system("pause");
return 0;
}
char getMenu (void)
{
char ch;
int counter;
do
{
ch = '\n';
system("cls");
cout<<"Please select a Snack\n"
<<"B - Beverage $5.00\n"
<<"C - Candy $2.25\n"
<<"H - Hot Dog $7.00\n"
<<"N - Nachos $4.50\n"
<<"P - Popcorn $6.75\n"
<<"Q - Quit"<<endl;
cin>>ch; 
ch = toupper(ch);
}
while (ch!='B' && ch!='C' && ch!='H' && ch!='N' && ch!='P' && ch!='Q');
return ch;
//Q exit hasnt been setup
counter++;
// if (ch == 'Q') {continue;}
}
char getSelection (void)
{
char ch;
double cost;
cout<<"\nYou selected ";
switch(ch)
{
case 'B': cout<<"Beverage"<<endl; cost = beverage; break;
case 'C': cout<<"Candy"<<endl; cost = candy; break;
case 'H': cout<<"Hot Dog"<<endl; cost = hotdog; break;
case 'N': cout<<"Nachos"<<endl; cost = nachos; break;
case 'P': cout<<"Popcorn"<<endl; cost = popcorn; break;
}
return 'Z';   //what am i returning here?
}
double getTendered (void)
{
double cost, tendered, change = 0;
//not returning costs correctly
do
{
cout<<"Please insert "<<cost<<"\nEnter amount tendered ";
cin>>tendered;
cin.clear();
cin.ignore(10,'\n');
cost = cost - tendered;
} 
while (cost>0);
cost = (-1 * cost);
if (cost>0) 
{ 
cout<<"Change: "<<cost<<endl; 
}
return change;
}
int getPin(void)
{
int pin;
do
{
system("cls");
cout<<"Welcome to the Movie Food System\n";
cout<<endl;
cout<<"Please enter your 4-digit pin code: ";
cin>>pin;
cin.clear();
cin.ignore(10,'\n');
} 
while (pin<1000 || pin>9999);
return pin;
} 



Still some notes in there but my functions are in place. The calculations dont work. i tried using "continue" but it says it cant use it outside of a loop which confuses me at this point. Anybody know how to fix this bit?
Last edited on
Just a few minor errors if you guys could help out that would be great. All i really need is to figure out where pieces go and how to pass the data that needs passing. If someone could just plug in the pieces and explain it that would be a huge help. Ive read the articles ive been linked to but im still not getting it so much.
Topic archived. No new replies allowed.