Hi, I have this program it is almost done..it runs fine except for the last condition that I am not getting right. Last function BOOL SOLD OUT it does not work.
struct Drink
{
string beverage;
double cost;
int remaining;
};
void Display (Drink listofdrinks);
//listofdrinks is actually an array of 5 structs; and since it is an array it should be by default passed by reference???
bool MoneyValidation(double moremoney, Drink listofdrinks);
//this function will be used 5 times to validate money enterred by the user; if returns true then will start RemainingBev function to substract
int RemainingBev(Drink listofdrinks);
//this function should calculate how many drinks are left IF the money validation is positive
bool SoldOut(Drink listofdrinks);
//once the amount of drinks is 0 it should display appropriate message
string answer;
double money=0;
double total=0;
//total amount earned by the machine;
do
{
cout<<"Drink name "<<"\t\t"<<"Cost\t"<<"Number in machine\n";
for (int i=0;i<5;i++)
{
Display (alldrinks[i]);
//function to display list of drinks alldrinks[i]= display[each array element] and each array element is a struct= (a) or (b) etc..
}
cout<<" "<<endl;
cout<<"Please choose a drink or enter\'quit' to stop the program: ";
answer=GetLine();
if (answer==a.beverage|| answer=="cola")
{ if (SoldOut(alldrinks[0])==true)
//CONDITION & FUNCTION CALL
{
cout<<"Sorry, we`ve run out!"<<endl;
} i get this message even if the amount of my beverage is NOT 0
cout<<"Please enter $"<<a.cost<<endl;
double money=GetDouble();
if (MoneyValidation(money,alldrinks[0])==true)
{
alldrinks[0].remaining = RemainingBev(alldrinks[0]);
cout<<"There are "<<alldrinks[0].remaining<<" "<<alldrinks[0].beverage<<"(s) left."<<endl;
total+=alldrinks[0].cost;
}
while (answer!="quit");
bool SoldOut(Drink listofdrinks)
{
int x=listofdrinks.remaining;
if (x==0)
return true;
}
Nwn, sorry, I tried to indent it but on the preview screen and here it is not indented for some reason.
Peter87, I changed it to this (if that is what you meant):
bool SoldOut(Drink listofdrinks)
{
int x=listofdrinks.remaining;
if (x<=0)
return true;
else
return false;
}
and then I had to enter break here:
if (SoldOut(alldrinks[0])==true)
{
cout<<"Sorry, we`ve run out!"<<endl; continue;
}
Now it does what I wanted it to do BUT I still do not get why it kept going to -1,-2 etc..without that continue?
It does, thank you.
If I just had to do it without continue:
My condition for loop should be
while (answer!="quit" && alldrinks[0,1,2,3,4].remaining>0)
//because I have 5 structs
I understand this would not compile..I guess the only way would be to just type it in
(answer!="quit" && alldrinks[0].remaining>0)
|| (answer!="quit" && alldrinks[1].remaining>0)
||(answer!="quit" && alldrinks[3].remaining>0)
||(answer!="quit" && alldrinks[4].remaining>0)