Error: Expected primary expression before else
Mar 6, 2011 at 2:36am UTC
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!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
//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;
}
Mar 6, 2011 at 2:39am UTC
All of your if statements are inside of the brackets rather than outside.
Mar 6, 2011 at 2:47am UTC
Thank you for pointing that out to me. I have made changes to the code, as shown below, but am still getting the same errors when I try it out in g++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
//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 Regular, P\
riority 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;
}
Mar 6, 2011 at 3:46am UTC
When making elses and else ifs, you must put them directly after the closing brace of the previous statement. Also, why you do have so many variables with numbers on the ends?
Topic archived. No new replies allowed.