1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

help on c++

Discussion in 'Software Development' started by kknla, Nov 3, 2001.

Thread Status:
Not open for further replies.
Advertisement
  1. kknla

    kknla Thread Starter

    Joined:
    Jan 19, 2001
    Messages:
    257
    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;
    }
     
  2. MacFromOK

    MacFromOK

    Joined:
    Mar 18, 2001
    Messages:
    1,741
    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. :)
     
  3. kknla

    kknla Thread Starter

    Joined:
    Jan 19, 2001
    Messages:
    257
    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================================
     
  4. MacFromOK

    MacFromOK

    Joined:
    Mar 18, 2001
    Messages:
    1,741
    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. :)
     
  5. MacFromOK

    MacFromOK

    Joined:
    Mar 18, 2001
    Messages:
    1,741
    I forgot something...

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

    Sorry.
     
  6. MacFromOK

    MacFromOK

    Joined:
    Mar 18, 2001
    Messages:
    1,741
    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
     
  7. MacFromOK

    MacFromOK

    Joined:
    Mar 18, 2001
    Messages:
    1,741
    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. :)
     
  8. kknla

    kknla Thread Starter

    Joined:
    Jan 19, 2001
    Messages:
    257
    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;
     
  9. MacFromOK

    MacFromOK

    Joined:
    Mar 18, 2001
    Messages:
    1,741
    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...
     
  10. MacFromOK

    MacFromOK

    Joined:
    Mar 18, 2001
    Messages:
    1,741
    I'm also outta here for tonight. I'll try to check back
    tomorrow.

    Cheers, Mac :)
     
  11. kknla

    kknla Thread Starter

    Joined:
    Jan 19, 2001
    Messages:
    257
    thanks again :)
     
  12. MacFromOK

    MacFromOK

    Joined:
    Mar 18, 2001
    Messages:
    1,741
    I removed the text file attachment and just
    posted the code. I had no idea the text
    attachment would be so scrambled...

    Cheers, Mac
     
  13. kknla

    kknla Thread Starter

    Joined:
    Jan 19, 2001
    Messages:
    257
    When I downloaded the text file, it worked fine. I appreciate all your help, Barret. :D
     
  14. MacFromOK

    MacFromOK

    Joined:
    Mar 18, 2001
    Messages:
    1,741
    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
     
  15. kknla

    kknla Thread Starter

    Joined:
    Jan 19, 2001
    Messages:
    257
    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.
     
  16. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/57557