Error: Expected primary expression before "else"

This is a discussion on Error: Expected primary expression before "else" within the C++ Programming forums, part of the General Programming Boards category; I am a beginner student learning to use C++ programming. My assignment is to create a program regarding shipping charges. ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    12

    Error: Expected primary expression before "else"

    I am a beginner student learning to use C++ programming. My assignment is to create a program regarding shipping charges. There are three different shipping services (regular, priority, overnight), and four different types of weight with prices. I have the program written out, but when I put it through g++, it says that all of my else statements require primary expressions before them? I am not sure as to what this means. If anyone could look over my coding and point out my errors to me, any input would be greatly appreciated!

    Code:
    //Lindsay Lewis     March 2011     Project 2_Shipping Charges
    
    #include <iostream>
    using namespace std;
    
    int main ()
    {
    
      float weight;
    
    // This step requires the user to input the service code of the package
      char code;
      cout << "Enter R, P, or O for the service code:" << endl;
      cin >> code;
    
    //This step requires the user to input the weight of the package and calculates the shipping charges for Regul\
    ar, Priority and Overnight shipping service based on the weight of the package.
      switch (code)
      {
        case 'R':
          cout << "The service you chose is Regular service." << endl;
          cout << "Enter the weight of the package:" <<endl;
          cin >> weight;
          cout <<" The weight of your package is" << weight << endl;
          float charger1;
            {
            if (weight <= 2)
              charger1 = weight * 1.00;
              cout << "The shipping charge for this package is" << charger1 << endl;
            }
          float charger2;
            {
            else if (weight > 2 && <= 6)
              charger2 = weight * 1.50;
              cout << "The shipping charge for this package is" << charger2 << endl;
            }
          float charger3;
            {
            else if (weight > 6 && <= 10)
              charger3 = weight * 2.00;
              cout << "The shipping charge for this package is" << charger3 << endl;
            }
          float charger4;
            {
            else
              charger4 = weight * 2.50
              cout << "The shipping charge for this package is" << charger4 << endl;
            }
              break;
       case 'P':
          cout << "The service you chose is Priority service." << endl;
          cout << "Enter the weight of the package:" << endl;
          cin >> weight;
          cout << "The weight of your package is" << weight << endl;
          float chargep1;
            {
            if (weight <= 2)
              chargep1 = weight * 3.50;
              cout << "The shipping charge for this package is:" << chargep1 << endl;
            }
          float chargep2;
            {
            else if (weight >2 && <= 6)
              chargep2 = weight * 5.50;
              cout << "The shipping charge for this package is:" << chargep2 << endl;
            }
          float chargep3;
            {
            else if (weight = >6 && <= 10)
              chargep3 = weight * 7.50;
              cout << "The shipping charge for this pacakge is:" << chargep3 << endl;
            }
          float chargep4;
            {
            else
              chargep4 = weight * 9.50;
              cout << "The shipping charge for this package is:" << chargep4 << endl;
            }
              break;
        case 'O':
          cout << "The service you chose is Overnight service." << endl;
          cout << "Enter the weight of the pacakage:" << endl;
          cin >> weight;
          cout << "The weight of your package is:" << weight << endl;
          float chargeo1;
            {
            if (weight <= 2)
              chargeo1 = weight * 11.50;
              cout << "The shipping charge for this package is:" << chargeo1 << endl;
            }
          float chargeo2;
            {
            else if (weight > 2 && <= 6)
              chargeo2 = weight * 16.50;
              cout << "The shipping charge for this package is:" << chargeo2 << endl;
            }
          float chargeo3;
            {
            else if (weight > 6 && <= 10)
              chargeo3 = weight * 21.50;
              cout << "The shipping charge for this package is:" << chargeo3 << endl;
            }      float chargeo4;
            {
            else
              chargeo4 = weight * 26.50;
              cout << "The shipping charge for this package is:" << chargeo4 << endl;
            }
            break;
        default:
          cout << "Error: Invalid service code." <<endl;
          cout << "Goodbye." << endl;
      }
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,678
    You have braces misplaced. For example;
    Code:
          {
            if (weight <= 2)
              charger1 = weight * 1.00;
              cout << "The shipping charge for this package is" << charger1 << endl;
            }
          float charger2;
            {
            else if (weight > 2 && <= 6)
              charger2 = weight * 1.50;
              cout << "The shipping charge for this package is" << charger2 << endl;
            }
    I'm guessing about what your code is intended to do, but suspect each { in the above needs to be one or two lines further down than they are.

    In the above sample, the declaration of charger2 is positioned above a {. That { needs to be moved to after the "else if ..." line, and the declaration of charger2 needs to be after that {.

    You've made the same sort of mistake repeatedly in your code.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. primary expression before else
    By Rareit in forum C++ Programming
    Replies: 4
    Last Post: 12-03-2010, 11:12 PM
  2. keep getting "expected primary expression before 'else'"
    By Creatlv3 in forum C++ Programming
    Replies: 6
    Last Post: 02-09-2010, 10:47 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  4. recursion error
    By cchallenged in forum C Programming
    Replies: 2
    Last Post: 12-18-2006, 09:15 AM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21