Quantity reduction help
May 1, 2012 at 9:52pm UTC
Hello all! I've stumbled into a problem while trying to complete this code. I can't seem to figure out how to reduce the number of drinks when the program purchases one. Then I need to add a message when the user wants to purchase a soda when it's currently sold out.
For example: I need the program to reduce 1 soda can from the 'Cola' slot whenever the user purchases a can. Then when the amount of 'Cola' cans are sold out. The program will display a message saying it's sold out.
Any help would be greatly appreciated! Here's my code so far. If you have any questions, please let me know. Please help me out; I'm counting on you!
Please help, this is urgent!
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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
//Drink Machine Simulator
#include<iostream>
#include<iomanip>
using namespace std;
int displayMenu(int );
float payment;
int choice;
float change;
int main()
{
struct drinkInfo
{
char * drinkName;
double cost;
int quantity;
};
int x = 0;
const int ARRAY_SIZE = 5;
float drinkpayment = 0.0f;
int selection = 0;
float drinkchange = 0.0f;
float earnings = 0.0f;
float reduce = 0.0f;
drinkInfo drinkList[ARRAY_SIZE];
drinkList[0].drinkName = "Cola" ;
drinkList[0].cost = .75;
drinkList[0].quantity = 20;
drinkList[1].drinkName = "Root Beer" ;
drinkList[1].cost = .75;
drinkList[1].quantity = 20;
drinkList[2].drinkName = "Lemon-Lime" ;
drinkList[2].cost = .75;
drinkList[2].quantity = 20;
drinkList[3].drinkName = "Grape Soda" ;
drinkList[3].cost = .80;
drinkList[3].quantity = 20;
drinkList[4].drinkName = "Cream Soda" ;
drinkList[4].cost = .80;
drinkList[4].quantity = 20;
do
{
//Totals the money earned in the machine.
earnings += payment - change;
cout << "Make a selection from the menu below!" << endl;
//Shows the drink menu.
selection = displayMenu(selection);
if (selection == 1)
{
cout << "Enter the amount of money to be paid \n(won't accept anything greater than $1): $" ;
cin >> payment;
if ((payment < .75) ||(payment > 1))
{
cout << "That is an invalid amount of change. Please re-enter your amount." ;
cin >> payment;
}
else if ((payment >= .75) || (payment <= 1))
{
//Calculates the amount of change to give.
change = payment - drinkList[0].cost;
cout << "Your change is $" << change << "\n\n" ;
}
}
else if (selection == 2)
{
cout << "Enter the amount of money to be paid \n(won't accept anything greater than $1): $" ;
cin >> payment;
if ((payment < .75) ||(payment > 1))
{
cout << "That is an invalid amount of change. Please re-enter your amount." ;
cin >> payment;
}
else if ((payment >= .75) || (payment <= 1));
{
change = payment - drinkList[1].cost;
cout << "Your change is $" << change << "\n\n" ;
}
}
else if (selection == 3)
{
cout << "Enter the amount of money to be paid \n(won't accept anything greater than $1): $" ;
cin >> payment;
if ((payment < .75) ||(payment > 1))
{
cout << "That is an invalid amount of change. Please re-enter your amount." ;
cin >> payment;
}
else if ((payment >= .75) || (payment <= 1));
{
change = payment - drinkList[2].cost;
cout << "Your change is $" << setprecision(2)<<change << "\n\n" ;
}
}
else if (selection == 4)
{
cout << "Enter the amount of money to be paid \n(won't accept anything greater than $1): $" ;
cin >> payment;
if ((payment < .80) ||(payment > 1))
{
cout << "That is an invalid amount of change. Please re-enter your amount." ;
cin >> payment;
}
else if ((payment >= .80) || (payment <= 1));
{
change = payment - drinkList[3].cost;
cout << "Your change is $" << setprecision(2) << fixed << change << "\n\n" ;
}
}
else if (selection == 5)
{
cout << "Enter the amount of money to be paid \n(won't accept anything greater than $1): $" ;
cin >> payment;
if ((payment < .80) ||(payment > 1))
{
cout << "That is an invalid amount of change. Please re-enter your amount." ;
cin >> payment;
}
else if ((payment >= .80) || (payment <= 1));
{
change = payment - drinkList[4].cost;
cout << "Your change is $" << setprecision(2) << fixed << change << "\n\n" ;
}
}
else if (selection == 6)
//Displays the amount of money the machine earned.
cout << "This machine has earned $" << earnings << "." << endl;
//Returns you to the menu if invalid selection is made.
else
{
cout << "Your selection is invalid. Please review the \nmenu and make another selection." << endl;
displayMenu(selection);
}
}
while (selection != 6);
system("pause" );
return 0;
}
//Drink menu.
int displayMenu(int choice)
{
cout << endl;
cout << "1. Cola $0.75" << endl;
cout << "2. Root Beer $0.75" << endl;
cout << "3. Lemon-Lime $0.75" << endl;
cout << "4. Grape Soda $0.80" << endl;
cout << "5. Cream Soda $0.80" << endl;
cout << "6. Quit" <<endl;
cout << endl;
cout << "Your selection: " ;
cin >> choice;
return choice;
}
Last edited on May 1, 2012 at 11:06pm UTC
Topic archived. No new replies allowed.