Thread: Error: expected primary expression and ; before else

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    3

    Error: expected primary expression and ; before else

    Hi! I am getting this Error: expected primary expression and ; before else on line 77. For the life of me I can't see it. Can anyone please help me out?

    Code:
    /* 
    IF
    Write a program that will calculate a customer's purchase amount
    for movie tickets.
    The user should be prompted for the amount of adult movie tickets
    they would like to purchase, the amount of child movie tickets
    that they would like to purchase, and whether or not they have a 
    discount coupon.
    If the user has a discount coupon, determine if it is for an adult
    movie ticket ("adult") or for child movie ticket ("child").
    Adult movie tickets cost 10.50 each, while child movie tickets
    cost 5.00 each.
    The discount coupon (if any) should be subtracted from the purchase
    amount.
    Calculate and display the overall purchase amount with two digits after
    the decimal point.
    */
    
    #include <iostream>
    #include <iomanip>
    #include <string>
    using namespace std;
    int main()
    {
    cout << fixed << setprecision(0) << setw(3) << endl;
    float  numAdtTix, numChdTix, TotPurchase, DiscountCoup = 0;
    string haveDiscount,  DiscountType;
    cout << setprecision(2) << fixed << endl;
    
    //get the number of adult movie tickets
    cout << "Enter the number of adult movie tickets that are being purchased: ";
    cin >> numAdtTix;
    
    //get the number of children's movie tickets
    cout << "Enter the amount of child tickets that are being purchased: ";
    cin >> numChdTix;
    
    //ask if the user has a discount coupon
    //if the user has a discount coupon
    //  ask the user for the discount coupon type
    //  if the discount coupon is "adult"
    //    discount coupon is 10.50
    //  else
    //   if the discount coupon is "child"
    //    discount coupon is 5.00
    //   else
    //    error message for bad discout type
    //   endif
    //  endif
    //endif
    
    cout << "Do you have a discount coupon (Y for yes, N for no)? ";
    cin >> haveDiscount;
    if ( haveDiscount == "Y" )
       {
       haveDiscount = "Y";
       cout << "Is this discount for an adult or child's ticket (A for adult or C for child)? ";
       cin >> DiscountType;
       }
       if ( DiscountType == "A" )
          {
          DiscountCoup = 10.50;
          cout << "Discount: " << DiscountCoup <<  endl;
          }
       else 
          {    
          if ( DiscountType == "C" )
          {
          DiscountCoup = 5.00;
          cout << "Discount: " << DiscountCoup << endl;
          }
          else
             {
          cout << DiscountCoup << "is an invalid coupon type" << endl;
             }
       }
    else
       {
       ( haveDiscount == "N" )
       haveDiscount = "N";
       cout << "Total Purchase: " << TotPurchase << endl;
       }
    
    //calculate the purchase amount
    TotPurchase = (numAdtTix * 10.50) + (numChdTix * 5.00) - DiscountCoup;
     
    return 0;
    }
    Last edited by dragonfly22; 09-16-2012 at 08:58 PM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You would probably be able to see these problems better if you used a consistent indent style.
    Code:
    /*
    IF
    Write a program that will calculate a customer's purchase amount
    for movie tickets.
    The user should be prompted for the amount of adult movie tickets
    they would like to purchase, the amount of child movie tickets
    that they would like to purchase, and whether or not they have a
    discount coupon.
    If the user has a discount coupon, determine if it is for an adult
    movie ticket ("adult") or for child movie ticket ("child").
    Adult movie tickets cost 10.50 each, while child movie tickets
    cost 5.00 each.
    The discount coupon (if any) should be subtracted from the purchase
    amount.
    Calculate and display the overall purchase amount with two digits after
    the decimal point.
    */
    
    #include <iostream>
    #include <iomanip>
    #include <string>
    using namespace std;
    int main()
    {
       cout << fixed << setprecision(0) << setw(3) << endl;
       float  numAdtTix, numChdTix, TotPurchase, DiscountCoup = 0;
       string haveDiscount,  DiscountType;
       cout << setprecision(2) << fixed << endl;
    
    //get the number of adult movie tickets
       cout << "Enter the number of adult movie tickets that are being purchased: ";
       cin >> numAdtTix;
    
    //get the number of children's movie tickets
       cout << "Enter the amount of child tickets that are being purchased: ";
       cin >> numChdTix;
    
    //ask if the user has a discount coupon
    //if the user has a discount coupon
    //  ask the user for the discount coupon type
    //  if the discount coupon is "adult"
    //    discount coupon is 10.50
    //  else
    //   if the discount coupon is "child"
    //    discount coupon is 5.00
    //   else
    //    error message for bad discout type
    //   endif
    //  endif
    //endif
    
       cout << "Do you have a discount coupon (Y for yes, N for no)? ";
       cin >> haveDiscount;
       if ( haveDiscount == "Y" )
       {
          haveDiscount = "Y";
          cout << "Is this discount for an adult or child's ticket (A for adult or C for child)? ";
          cin >> DiscountType;
       }
       if ( DiscountType == "A" )
       {
          DiscountCoup = 10.50;
          cout << "Discount: " << DiscountCoup <<  endl;
       }
       else
       {
          if ( DiscountType == "C" )
          {
             DiscountCoup = 5.00;
             cout << "Discount: " << DiscountCoup << endl;
          }
          else
          {
             cout << DiscountCoup << "is an invalid coupon type" << endl;
          }
       }
       else
       {
          ( haveDiscount == "N" )
          haveDiscount = "N";
          cout << "Total Purchase: " << TotPurchase << endl;
       }
    
    //calculate the purchase amount
       TotPurchase = (numAdtTix * 10.50) + (numChdTix * 5.00) - DiscountCoup;
    
       return 0;
    }
    Compiling the above code I get these errors:
    In function ‘int main()’:|
    main.cpp|77|error: ‘else’ without a previous ‘if’|
    main.cpp|80|error: expected ‘;’ before ‘haveDiscount’|
    ||=== Build finished: 2 errors, 0 warnings ===|
    You seem to have a mismatch between the number of your if statements and your else statements. Also line 79 seems to be missing something.

    Jim

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    3
    Thx for the help. I tried to seperate the if/else statements, now i get an error: expected : before cout

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Post your latest code. If your error is "expected `;' before cout", then look on the previous line and see if it's missing a semicolon. Also look a few lines earlier on, certain kinds of syntax errors (especially missing curly braces) don't cause the compiler to complain right away; it will keep going and choke later on.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You can't have two else's to an if-statement because each else is like saying "and in all other cases do this".
    else-if on the other hand simply allows you to look at a specific case which has not yet been addressed.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    3
    Thank you for the help. I printed it out so I could see better and I did leave out a curly brace. Spent too hours looking at it on computer.
    I do appreciate it!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. expected primary expression
    By lilhawk2892 in forum C++ Programming
    Replies: 10
    Last Post: 11-22-2007, 07:50 PM
  2. Expected primary expression?
    By Beowolf in forum C++ Programming
    Replies: 17
    Last Post: 11-11-2007, 09:57 PM
  3. expected primary expression
    By mju4t in forum C Programming
    Replies: 2
    Last Post: 03-27-2007, 06:59 PM
  4. Replies: 24
    Last Post: 09-06-2006, 06:17 PM