Soda Machine Invetory
Nov 19, 2015 at 5:08am UTC
So this is what I have so far. But I need to able make the machine keep track of how many sodas/drinks are left, and then, if the user choses it, tell them it was sold out. I figure I could do the last part, but I'm having serious problems actually figure out how to get it to count down and keep track of the sodas.
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 110 111 112 113 114
#include<iostream>
#include<iomanip>
using namespace std;
char drinkSelect(char choice) {
cout << "A: Coke $1.25\n" << "B: Sprite $1.25\n" << "C: Water $0.75\n"
<< "D: Dr. Pepper $1.25\n\n" << "Press 'E' to Exit\n" << endl;
cin >> choice;
choice = toupper(choice);
return choice;
}
bool validChoice(char validChoice) {
bool valid;
if (validChoice == 'A' || validChoice == 'B' || validChoice == 'C' || validChoice == 'D' || validChoice == 'E' )
{
valid = true ;
}
else
{
valid = false ;
}
return valid;
}
int calculate(char choice) {
double userCash;
double change;
double more;
if (choice == 'A' ) {
cout << "\nCoke chosen\n" << "Total $1.25\n" << "\nInsert Cash\n" ;
cin >> userCash;
while (userCash < 1.25) {
cout << "\nInsufficent funds, retry.\n" ;
more = 1.25 - userCash;
cout << "You needed: " << more << " more\n" ;
cin >> userCash;
}
change = userCash - 1.25;
cout << "\nYour change is $" << change << endl;
}
else if (choice == 'B' ) {
cout << "\nSprite chosen\n" << "Total $1.25\n" << "\nInsert Cash\n" ;
cin >> userCash;
while (userCash < 1.25) {
cout << "\nInsufficent funds, retry.\n" ;
more = 1.25 - userCash;
cout << "You needed: " << more << " more\n" ;
cin >> userCash;
}
change = userCash - 1.25;
cout << "\nYour change is $" << change << endl;
}
else if (choice == 'C' ) {
cout << "\nWater chosen\n" << "Total $0.75\n" << "\nInsert Cash\n" ;
cin >> userCash;
while (userCash < .75) {
cout << "\nInsufficent funds, retry.\n" ;
more = .75 - userCash;
cout << "You needed: " << more << " more\n" ;
cin >> userCash;
}
change = userCash - .75;
cout << "\nYour change is $" << change << endl;
}
else if (choice == 'D' ) {
cout << "\nDr. Pepper chosen\n" << "Total $1.25\n" << "\nInsert Cash\n" ;
cin >> userCash;
while (userCash < 1.25) {
cout << "\nInsufficent funds, retry.\n" ;
more = 1.25 - userCash;
cout << "You needed: " << more << " more\n" ;
cin >> userCash;
}
change = userCash - 1.25;
cout << "\nYour change is $" << change << endl;
}
else {
return 0;
}
}
int main(){
char tempChoice = ' ' ;
char choice;
bool validity;
choice = drinkSelect(tempChoice); //storing users choice as choice
validity = validChoice(choice); // checking validity of choice
calculate(choice);
system("pause" );
return 0;
}
Last edited on Nov 19, 2015 at 5:08am UTC
Nov 19, 2015 at 10:29am UTC
You need some variables to store the initial amount you have in the beginning - like
int num_cokes = 10
After you have sold one you decrement the variable. If the variable is 0 then you put out "Sold out"
Topic archived. No new replies allowed.