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
|
#include <string>
#include <iostream>
using namespace std;
struct Snack
{
string name;
double cost;
int inventory;
};
int main()
{
string snack;
struct Snack vendor[4] = {
{"Oreo Cookie Package", 0.75, 25},
{"Granola Bars", 0.80, 30},
{"Oatmeal Raisin Cookies", 0.75, 15},
{"Tortilla Chips", 0.60, 20},
};
double profit = 0.00;
while (true) // change here
{
for (int i = 0; i < 4; i++)
{
if (vendor[i].inventory > 0)
{
cout << i+1 << ". " << vendor[i].name << endl;
}
}
int selection;
cout << "Enter the number of the snack you would like to purchase. If you are done, enter 0 to quit: " << endl;
cin >> selection;
if (selection == 0)
break; // change here
double payment;
cout << "Your snack costs $" << vendor[selection - 1].cost << ". Please enter at least as much as your snack costs, but no more than $5.00: " << endl;
cin >> payment;
while (payment<vendor[selection - 1].cost || payment>5.00)
{
cout << "You have not entered a valid amount. Please try again: " << endl;
cin >> payment;
}
profit += vendor[selection - 1].cost;
cout << "Your change is: " << payment - vendor[selection - 1].cost << endl;
cout << "" << endl;
vendor[selection - 1].inventory --;
}
cout << "Your total money made is: $" << profit << endl;
for (int i = 0; i < 4; i++)
{
cout << "There are " << vendor[i].inventory << " " << vendor[i].name << "'s remaining";
}
return 0;
}
| |