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
|
// Returning to beginning of while loop halfway through almostdone 269535
#include <cctype> // <--- For "std::tolower()" and "std::toupper()".
#include <iostream>
#include <iomanip>
#include<string>
//using namespace std; // <--- Best not to use.
// The most recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/
using std::cin;
using std::cout;
using std::endl;
using std::string;
constexpr size_t MAXSIZE{ 6 };
constexpr int TOTALDRINKS{ 100 };
// structure for drink data
struct DrinkData
{
string name[MAXSIZE] = { "", "Cola","Root Beer","Lemon-Lime","Grape","Cream Soda" }; //array for drink names
double price[MAXSIZE] = { 0.0, 0.75, 0.75, 0.75, 0.75, 0.75 }; //array for drink costs
//int number[MAXSIZE] = { 0, 20, 20, 20, 20, 20 }; //array for number of drinks in machine
int number[MAXSIZE] = { 0, 0, 20, 20, 20, 20 }; // <--- Used for testing. Delete when finished.
};
int main()
{
//declare variables
DrinkData drink; //drink is a DrinkData structure. You do not have to comment the incredibly obvious, the merely obvious will do. Unless it helps you then disregard.
char yesNo{ 'Y' }; // <--- Added.
int choice; //for choice of drink
size_t totalDrinks{ 100 }; // <--- Added. Total drinks in the machine. Adjust to a smaller number for testing.
double moneyReceived{};
double change{};
double earnings{};
bool quit{}; // <--- Sets to (0) zero same as false.
double totalearnings{};
std::cout << std::fixed << std::setprecision(2); // <--- Only needs done once unless something is changed.
while (!quit) //THIS while loop is where I need help! Sets up an endless loop unlless you wnt to end the program. Say when the machine is empty.
{
if (totalDrinks == 0)
{
std::cout << "\n The machine is empty.\n";
quit = true;
continue;
}
// Display a list of drinks preceded by a number. This will be the menu.
cout << "\n 1. Cola: .75 cents " << endl;
cout << " 2. Root Beer: .75 cents " << endl;
cout << " 3. Lemon Lime: .75 cents " << endl;
cout << " 4. Grape Soda: .75 cents " << endl;
cout << " 5. Cream Soda: .75 cents " << endl;
cout << " Please select a drink by pressing on the corresponding number: ";
cin >> choice;
//input validation
while (choice < 1 || choice > 5)
{
cout << "\n Please enter a valid choice, 1-5.\n\n Enter choice: ";
cin >> choice;
}
//make sure there's enough cans
if (!drink.number[choice]) // <--- Any number greater than (0) zero is the same as true.
{
cout << "\n I'm sorry, we've run out of your selected drink. Please select another.\n";
//cin >> quit; // <--- You could use this, but it is better to start the loop over.
continue;
}
//process money
cout << "\n Please enter an amount at most $1.00 to pay for your " << drink.name[choice] << " drink: ";
cin >> moneyReceived;
//input validation
while (/*moneyReceived < 0 ||*/ moneyReceived > 1)
{
cout << "\n Sorry, this vending machine does not accept money over $1.00. Please re-enter: ";
cin >> moneyReceived;
}
//input validation
while (moneyReceived < drink.price[choice])
{
double additionalMoney{};
cout << "\n You did not enter enough to pay for your selection. Please add: " << drink.price[choice] - moneyReceived << ": ";
cin >> additionalMoney;
moneyReceived += additionalMoney;
}
//dispense drink message, or process money
cout << "\n Your money is being processed. Your drink will be dispensed shortly.\n";
//calculate change due
change = moneyReceived - drink.price[choice];
//update earnings
earnings = moneyReceived - change;
totalearnings = earnings + totalearnings; // <--- totalEarnings += earbubgs; does the same.
//decrease number of cans of selected drink in the machine
drink.number[choice]--; // <--- Does the same as what you have just simpler.
totalDrinks--;
//update user
cout << "\n Here is your change: " << change << ". Thank you for purchasing a drink!"
"\n\n If you would like to purchase another one, please enter (Y or N): ";
cin >> yesNo;
if (std::toupper(yesNo) == 'N') // <---Added.
break;
}
cout
<< "\n The total earnings made by this vending machine has been " << totalearnings
<< " during this program.\n"
<< "\n With a total of " << TOTALDRINKS - totalDrinks << " drinks sold."
<< endl;
return 0;
}
|