Apr 19, 2013 at 9:32pm Apr 19, 2013 at 9:32pm UTC
***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 Apr 25, 2013 at 12:22am Apr 25, 2013 at 12:22am UTC
Apr 25, 2013 at 12:23am Apr 25, 2013 at 12:23am UTC
Code has been changed. Still isnt working correctly and obviously wont compile in this condition.
Apr 27, 2013 at 1:44am Apr 27, 2013 at 1:44am UTC
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 Apr 28, 2013 at 5:23pm Apr 28, 2013 at 5:23pm UTC
Apr 28, 2013 at 5:26pm Apr 28, 2013 at 5:26pm UTC
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.