struct in functions

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


int main ()

{

Drink a={"Cola ", 1.25,20};
Drink b ={"Root Beer", 1.5,20};
Drink c={"Lemon Lime", 1.75,20};
Drink d ={"Fanta ",1.10,20};
Drink e={"7up ", 1.35, 20};

Drink alldrinks[5] ={a,b,c,d,e};

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;
}
closed account (o3hC5Di1)
Hi there,

Can you please put your code in [code ] {code here} [/code] blocks and do some indentation?
http://en.wikipedia.org/wiki/Programming_style#Indentation

This is very hard to read right now.

Thanks!

NwN
Last edited on
SoldOut only returns something when x==0 so what happens otherwise is undefined. Change the code to return a value in all cases.
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?
Last edited on
closed account (o3hC5Di1)
Hi there,

I think it would be because your loop while (answer!="quit"); only stops when someone gives answer 'quit'.

With "continue" you break out of that loop manually.

Hope that makes sense?

All the best,
NwN
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)
closed account (o3hC5Di1)
Hi there,

You can make it a bit shorter by using parenthesis for grouping:

1
2
if (answer!="quit" 
    && ( alldrinks[0].remaining>0 || alldrinks[1].remaining>0 || alldrinks[3].remaining>0 || alldrinks[4].remaining>0) ) 


Maybe one of the many experts on here has an eve better solution :)

All the best,
NwN
Topic archived. No new replies allowed.