I need to figure out a way to cout
I need to figure out a way to cout << \" Sorry that is sold out\" << endl; once one of the soda counts gets down to 0. Then I also need to stop the count down for that soda, so it doesn\'t go into a negative count. If you run my code, you will see that eventually the count starts going negative i.e.,. 2,1,0,-1,-2...
#include <iostream>
#include <string>
using namespace std;
struct Machine // creating the \"machine\" structure
{
string drinkName;
double drinkCost;
int numofdrinks;
};
void find(int); // creates a prototype
const int TOTAL_MACHINES = 5; // initializes a constant value for our amount of sodas
double totalAmount = 0;
Machine machine[TOTAL_MACHINES] = { // we are creating and initializing the machine structures
{ \"(1) Cola \", 0.75, 20 },
{ \"(2) Root Beer \", 0.75, 20 },
{ \"(3) Lemon Lime\", 0.75, 20 },
{ \"(4) Grape Soda\", 0.80, 20 },
{ \"(5) Cream Soda\", 0.80, 20 } };
int main()// starts our main function && declares our variables
{
string drink;
int value = 20;
bool t = true;
while (t)
{
cout << \"----------------------------------------------------\" << endl;
cout << \"Drink Name |Cost |Number in Machine\" << endl;
cout << \"----------------------------------------------------\" << endl;
for (int index = 0; index < TOTAL_MACHINES; index++)
{
cout << machine[index].drinkName << \" \"
<< machine[index].drinkCost << \" \"
<< machine[index].numofdrinks << endl;
}
cout << endl;
//prompt the user
cout << \"Enter the exact name of the drink you want or the number associated with it... or type quit:\" << endl;
cin >> drink;
// compare the users input with the machine drinks
if ((drink == \"Cola\") || drink == \"1\")
{
find(0);
drink = \"/0\";
}
else if ((drink == \"Root Beer\") || (drink == \"2\"))
{
find(1);
drink = \"/0\";
}
else if ((drink == \"Lemon Lime\") || (drink == \"3\"))
{
find(2);
drink = \"/0\";
}
else if ((drink == \"Grape Soda\") || (drink == \"4\"))
{
find(3);
drink = \"/0\";
}
else if ((drink == \"Cream Soda\") || (drink == \"5\"))
{
find(4);
drink = \"/0\";
}
else if ((drink == \"quit\") || (drink == \"Quit\"))
{
cout << \"Okay bye bye\" << endl;
t = false;
}
else
{
cout << \"Input invalid, cycle ending\" << endl;
t = false;
}
}
cout << endl;
cout << \" The total amount of money that we have earned is : $\" << endl;
cout << totalAmount << endl;
cout << endl;
system(\"pause\");
return 0;
}
void find(int idx)
{
double amount;
double change;
cout << endl;
cout << \"Ok you selected: \" << endl;
cout << machine[idx].drinkName << endl;
cout << \"Please ener the amount you inserted: $\" << endl;
cin >> amount;
//While statement for if they entered a negative number or over a dollar
while (amount < 0 || amount > 1)
{
cout << endl;
cout << \"Please enter an amount between $0-$1.00\" << endl;
cin >> amount;
}
// if the user doesn\'t enter enough money this will pop up until they do
while (amount < machine[idx].drinkCost)
{
cout << \"Brah you are short money\" << endl;
cin >> amount;
}
//calculating change
change = amount - machine[idx].drinkCost;
cout << endl;
cout << \" Your change is : $ \" << change;
cout << endl;
//changing the amount of soda and calculating the total money we made
machine[idx].numofdrinks = machine[idx].numofdrinks - 1;
if (machine[idx].numofdrinks < 0)
{
cout << \"Sorry that choice is sold out\" << endl;
}
totalAmount = totalAmount + machine[idx].drinkCost;
}
Solution
Here as mentioned in question you just need the part where it counts number of drinks and decrease the count and should stop once reaches 0.Below are the code changes required:
if(numofdrinks>0)
{machine[idx].numofdrinks = machine[idx].numofdrinks - 1;}
else if (machine[idx].numofdrinks < = 0)
{
cout << \"Sorry that choice is sold out\" << endl;
}


