Tech Support Guy banner
Status
Not open for further replies.
1 - 20 of 20 Posts

·
Registered
Joined
·
259 Posts
Discussion Starter · #1 ·
I am writing this project for school. I keep getting a parse error before `=' on about all the assignment statements in a certain function. I am tired or something, because I do not seem to see any errors. Anyone see what they are? Thanks ahead of time, Barret.
I am just listing the function, if you need more, just let me know.
Keep in mind, that I am new to this :)
adult, child, and senior_citizen have all beed predefined.

void get_subTotal (int age, char drink, float& subtotal, float& drink_price)
{
float price;
if (age < 12)
{
price = age * child;
}
else if (age >= 12 && age < 62)
{
price = adult;
}
else
price = senior_citizen;

if (drink == 'b' || drink == 'B')
{
drink_price = beer;
subtotal = price + drink_price;
}

else if (drink == 'c' || drink == 'C')
{
drink_price = coke;
subtotal = price + drink_price;
}
else
{
drink_price = water;
subtotal = price;
}
return;
}
 

·
Registered
Joined
·
1,951 Posts
You seem to have a bracket problem with your
if/else statements.

I'm assuming beer, coke, and water have been defined...

Code:
void get_subTotal (int age, char drink, float& subtotal, float& drink_price) 
{ 
  float price; 

  if (age < 12) 
     { price = age * child; 
     }
  else
     { if (age >= 12 && age < 62) 
          { price = adult; 
          }
       else 
          { price = senior_citizen; 
          }
     }

  if (drink == 'b' || drink == 'B') 
     { drink_price = beer; 
       subtotal = price + drink_price; 
     } 
  else
    { if (drink == 'c' || drink == 'C') 
         { drink_price = coke; 
           subtotal = price + drink_price; 
         } 
      else 
         { drink_price = water; 
           subtotal = price; 
         } 
    }

  return; 
}
Let me know if this helps. :)
 

·
Registered
Joined
·
259 Posts
Discussion Starter · #3 ·
I am still getting the same errors. Here is all of my code. Everyhing was working fine up untill I wrote that fuction. Thanks.

//ø¤°`°¤ø,¸ ¸,ø¤°`°¤ø¤°`°¤ø,¸ ¸,ø¤°`°¤ø¤°`°¤ø,¸ ¸,ø¤°`°¤ø¤°`°¤ø,¸ ¸,ø¤°`°¤ø¤°`
//
//
// Barret Kendrick
// CMPS 161
// 10-27-01
// Project 5
//
// This Program will ask the user for the drink he/she would like to have with
// his buffet order. It will then calculate everything and print the receipt
// for the user.
//
//ø¤°`°¤ø,¸ ¸,ø¤°`°¤ø¤°`°¤ø,¸ ¸,ø¤°`°¤ø¤°`°¤ø,¸ ¸,ø¤°`°¤ø¤°`°¤ø,¸ ¸,ø¤°`°¤ø¤°`

#include <iostream.h>
#define coke = 1.20
#define beer = 2.10
#define water = 0.0
#define adult = 5.95
#define senior_citizen = 4.50
#define child = .45

void getAge (int& age);
void getDrink (int age, char& drink);
void get_subTotal (int age, char drink, float& subtotal, float& drink_price);

void main ()
{
int age;
char drink;
float subtotal;
float drink_price;

cout <<"Welcome to this program. I hope that it helps you to make your"
<<"work a little easier and quicker to complete.";
getAge (age);
getDrink (age, drink);
get_subTotal (age, drink, subtotal, drink_price);
}
//=============================start of getAge===============================
//This function will get the users age.
void getAge (int&age)
{
cout <<"\n\n\nHow old are you? ";
cin >>age;
return;
}
//==============================end of getAge================================

//============================start of getDrink==============================
//This function will get the users drink, and make sure he/she is old enough
//if they choose beer
void getDrink (int age, char& drink)
{
cout <<"\nWhat kind of drink would you like?"
<<"\n 'C' for coke"
<<"\n 'B' for beer"
<<"\n 'W' for water\n"
<<endl;
cin >>drink;

while (drink == 'B' || drink == 'b' && age < 21)
{
cout <<"\nYou are not old enough to drink beer."
<<"\nYou have to be at least 21 years old to purchase beer."
<<"\nYou are only " <<age <<" year(s) old."
<<"\nPlease choose something else.\n\n";
cin >>drink;
}

return;
}
//=============================end of getDrink===============================

//===========================start of subTotal===============================
//This function will calculate the subtotal for this customer.

void get_subTotal (int age, char drink, float& subtotal, float& drink_price)
{
float price;

if (age < 12)
{ price = age * child;
}
else
{ if (age >= 12 && age < 62)
{ price = adult;
}
else
{ price = senior_citizen;
}
}

if (drink == 'b' || drink == 'B')
{ drink_price = beer;
subtotal = price + drink_price;
}
else
{ if (drink == 'c' || drink == 'C')
{ drink_price = coke;
subtotal = price + drink_price;
}
else
{ drink_price = water;
subtotal = price;
}
}

return;
}

//============================end of subTotal================================
 

·
Registered
Joined
·
1,951 Posts
OK, here's some problems.

Your procedures should be "int" instead of "void"
so they can return the value back to main.
Otherwise the info you're getting from the user
doesn't get back to main. Make any sense?

This line also has errors:
Code:
&lt&lt"\nYou are only " &lt &lt&lt"\nPlease choose something else.\n\n";
No "age" after "You are only".

Give these a try and let me know what happens. :)
 

·
Registered
Joined
·
1,951 Posts
I forgot something...

The procedure that gets the drink type should
be a "char".

Sorry.
 

·
Registered
Joined
·
1,951 Posts
OK, never mind.

I'm asleep at the wheel here. I rarely use
pointers like your example, so give me a few
minutes on this.

Mac
 

·
Registered
Joined
·
1,951 Posts
Well, duh.

Your #defines shouldn't use the "=" sign.
Code:
#define coke 1.20  
#define beer 2.10 
#define water 0.0
#define adult 5.95 
#define senior_citizen 4.50 
#define child .45
Sorry I'm so dense tonight. :)
 

·
Registered
Joined
·
259 Posts
Discussion Starter · #8 ·
I just had to kick myself for that one. Thanks, that cleared up most errors. I am still getting 2. Im getting a parse error before else, and a parse error at end of input.

void get_subTotal (int age, char drink, float& subtotal, float& drink_price)
{
float price;
if (age < 12)
{
price = age * child;
}
else
if (age >= 12 && age < 62)
{
price = adult;
else //this is where the first error is.
price = senior_citizen;
 

·
Registered
Joined
·
1,951 Posts
I've corrected some other errors, but haven't
checked your get_subTotal procedure for accuracy.
I'm attaching the code as a text file.

Code:
#include &ltiostream.h>

#define coke 1.20 
#define beer 2.10 
#define water 0.0 
#define adult 5.95 
#define senior_citizen 4.50 
#define child .45 

void getAge (int& age); 
void getDrink (int age, char& drink); 
void get_subTotal (int age, char drink, float &subtotal, float &drink_price); 

void main () 
{ 
  int age; 
  char drink; 
  float subtotal = 0; 
  float drink_price = 0; 

  cout <<"Welcome to this program. I hope that it helps you to make your" 
      <<"work a little easier and quicker to complete."; 
  getAge(age); 
  getDrink(age, drink); 
  get_subTotal(age, drink, subtotal, drink_price); 
} 

void getAge(int& age) 
{ 
  cout <<"\n\n\nHow old are you? "; 
  cin >> age; 
  return;
} 

void getDrink(int age, char &drink) 
{ 
  cout <<"\nWhat kind of drink would you like?" 
  <<"\n 'C' for coke" <<"\n 'B' for beer" <<"\n 'W' for water\n";
  cin >> drink; 

  while ((drink == 'B' || drink == 'b') && age < 21) 
        { cout << "\nYou are not old enough to drink beer." 
          <<"\nYou have to be at least 21 years old to purchase beer." 
          <<"\nYou are only " << age <<"\nPlease choose something else.\n\n"; 
          cin >> drink; 
        } 

  return; 
} 

void get_subTotal(int age, char drink, float &subtotal, float &drink_price)
{ 
  float price; 

  if (age < 12) 
     { price = age * child;
     }
  else
     { if (age >= 12 && age < 62) 
          { drink_price = adult; 
          }
       else 
          { drink_price = senior_citizen; 
          }
     }

  if (drink == 'b' || drink == 'B') 
     { drink_price = beer; 
       subtotal = price + drink_price; 
     } 
  else
    { if (drink == 'c' || drink == 'C') 
         { drink_price = coke; 
           subtotal = price + drink_price; 
         } 
      else 
         { drink_price = water; 
           subtotal = price; 
         } 
    }
  return; 
}
OK, the text file was a bad idea...
 

·
Registered
Joined
·
1,951 Posts
I'm also outta here for tonight. I'll try to check back
tomorrow.

Cheers, Mac :)
 

·
Registered
Joined
·
1,951 Posts
I removed the text file attachment and just
posted the code. I had no idea the text
attachment would be so scrambled...

Cheers, Mac
 

·
Registered
Joined
·
1,951 Posts
One last thing (hopefully)...

I think "drink_price" in the following should
be "price". I believe I changed it while testing
and forgot to change it back...

Code:
  if (age >= 12 && age < 62)
     { drink_price = adult;
     }
  else
     { drink_price = senior_citizen;
     }
Cheers, Mac
 

·
Registered
Joined
·
259 Posts
Discussion Starter · #15 ·
That is correct. I does need to be price. Hopefully, I can finish the rest of this error free. :D I am working on it now. So far, so good. Thanks again, Barret.
 

·
Registered
Joined
·
259 Posts
Discussion Starter · #17 ·
Everything is compiling fine, but something is wrong. After is asks the user if he wants to enter a tip, it runs through the rest of the program and terminates. If you have the time, would you mind taking a look. Thanks, Barret. You have to download the text file to view it correctly.
 

Attachments

·
Registered
Joined
·
259 Posts
Discussion Starter · #18 ·
I know that it is skipping the first "if" statement in the getTip function, and going straight to the "else". I'm just not seeing why.
 

·
Registered
Joined
·
1,951 Posts
Your "user_answer" should be a "char" not a "float"
since you want a Y/y or N/n... :)
 

·
Registered
Joined
·
259 Posts
Discussion Starter · #20 ·
I have to kick myself for that one too. It was a float at one time, but I changed it, and did not change float to char. Thanks again, Barret
 
1 - 20 of 20 Posts
Status
Not open for further replies.
Top