/*
Week 4 Program
Lets continue with our Coke Machine program from week 3.
Now, you will need to add 3 functions to it and move the
appropriate code into them. Create an InsertMoney function
and move all of the money insertion code into it. Create a
Menu function that will display the drink choices and Create
a Change function that will give the user his or her change.
Here’s how the main function will look
main()
//Beginning of loop
Call the insert money function
Call the DisplayMenu function
Call the GiveChange function
//end of loop
//end of main
At this point, a menu( which is also a separate function) will
display saying something like:
What would you like?
Press 1 for Coke
Press 2 for Dr. Pepper
Press 3 for Pepsi
Once they make their choice, you can dispense the drink and any
change that they may owe. Lets use a function for this also.
To be clear, I’m asking you to create three functions, not counting Main.
1. You should have one function to accept and keep track of the money.
2. One function to display the menu
3. One function to dispense the drink and give the change
You may choose to add more functions to your program. If you wish to do so,
or if you wish to have one function call another one rather than having to
return to main each time, that’s fine.
*/
#include <iostream>
usingnamespace std;
void insertMoney();
void displayMenu();
void giveChange();
int main()
{
insertMoney();
displayMenu();
giveChange();
return 0;
}
void insertMoney()
{
// declare and initiate variables and one array
double selection = 0;
double sodaPrice = 1.00;
double inMachine = 0;
double newDep = 0;
double change = 0;
char more = 'y';
int a[] = {0,0,0};
while (more == 'y')
{
// This while loop will keep executing until the logic is false
while (inMachine < sodaPrice)
{
// Outputing to console display and passing inputs to variables
cout << "Please insert $" << sodaPrice << endl;
cout << "You have deposited: $" << inMachine << endl;
// Subtracting the amount in the machine from the cost of the soda
cout << "Amount Needed: $" << (sodaPrice - inMachine) << endl;
cout << "Amount you wish to deposit: $";
cin >> newDep;
inMachine = (newDep + inMachine);
//Some fancy CLEAR SCREENS
system("CLS");
}
void displayMenu()
{
cout << "Thank you! Please select a type of soda:" << endl << endl;
cout << "Coke - " << 1 << endl;
cout << "Pepsi - " << 2 << endl;
cout << "Dr. Pepper - " << 3 << endl << endl;
cin >> selection;
if (selection == 1)
{
// Incrementing the array for each soda bought
a[0] = a[0]+1;
cout << "Thank you and enjoy your Coke!" <<endl;
}
elseif (selection == 2)
{
a[1] = a[1]+1;
cout << "Thank you and enjoy your Pepsi!" <<endl;
}
elseif (selection == 3)
{
a[2] = a[2]+1;
cout << "Thank you and enjoy your Dr. Pepper!" <<endl;
}
}
void giveChange()
{
if (inMachine > sodaPrice)
{
cout << "Your change is: $" << (inMachine - sodaPrice) << endl;
}
// Clearing the variables
inMachine = 0;
selection = 0;
cout << "Would you like to purchase another soda? Y or N ";
cin >> more;
system("CLS");
}
// Displaying the totals of soda bought
cout << "You have purchased " << a[0] << " Coke(s)" << endl;
cout << "You have purchased " << a[1] << " Pepsi(s)" << endl;
cout << "You have purchased " << a[2] << " Dr. Pepper(s)" << endl;
system("PAUSE");
}
I added two '}' braces after 96 and deleted the one '}' on 135.
Did you compile and run this or just a visual inspection? Because I get the following errors:
1 2 3 4 5 6 7 8 9 10 11
C:\Documents and Settings\Doug\Desktop\SodaMachineWithFuncs_CPP.cpp In function `void insertMoney()':
98 C:\Documents and Settings\Doug\Desktop\SodaMachineWithFuncs_CPP.cpp expected primary-expression before "void"
98 C:\Documents and Settings\Doug\Desktop\SodaMachineWithFuncs_CPP.cpp expected `;' before "void"
123 C:\Documents and Settings\Doug\Desktop\SodaMachineWithFuncs_CPP.cpp expected primary-expression before "void"
123 C:\Documents and Settings\Doug\Desktop\SodaMachineWithFuncs_CPP.cpp expected `;' before "void"
141 C:\Documents and Settings\Doug\Desktop\SodaMachineWithFuncs_CPP.cpp expected `}' at end of input