expected primary-expression before else
Feb 21, 2012 at 2:14am UTC
What exactly am I doing wrong here? At the last else statement the expected primary expression before the else statement error keeps coming up. If I don't use the else statement, and just have the if, the program compiles, but doesn't run right. So, i need the else statement and I need this error to go away. Here's the program:
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
// Markus Wedderburn Feb 2 (#23)
// Homework #4 - Internet Service Provider
#include <iostream>
using namespace std;
int main()
{
char package, q;
short hours;
double baseCostA, baseCostB, baseCostC, Tcost;
baseCostA = 9.95;
baseCostB = 14.95;
baseCostC = 19.95;
cout << "Please enter your internet service package" ;
cout << "\nand the number of hours used below." ;
cout << "\n\nPackage letter (a, b, or c): " ;
cin >> package;
cout << "Hours of service used: " ;
cin >> hours;
if (package == 'a' )
{
if (hours > 9)
{
Tcost = baseCostA + (hours-10)*2;
}
else
{
if (hours < 10)
{
Tcost = baseCostA;
}
}
}
else
{
if (package == 'b' )
{
if (hours > 19)
{
Tcost = baseCostB + (hours-20);
}
else
{
if (hours < 20)
{
Tcost = baseCostB;
}
}
}
}
else
{
if (package == 'c' )
{
Tcost = baseCostC;
}
}
cout << "\n\nTotal cost for the month: $" ;
cout << Tcost;
cout << "\n\nPress q and enter to quit. " ;
cin >> q;
return 0;
}
Thanks
Feb 21, 2012 at 2:21am UTC
lines 23, 37, 54:
1 2 3 4 5 6 7 8 9 10 11 12
if (package == 'a' )
{
//...
}
else
{
//...
}
else // <- wtf?
{
//...
}
You can't put an else after an else. else's can only come after ifs.
You probably meant to do this:
1 2 3 4 5 6 7 8 9 10 11 12
if (package == 'a' )
{
//...
}
else if (package == 'b' )
{
//...
}
else /*if (package == 'c')*/ // <-condition here isn't needed because it's the last one
{
//...
}
Feb 21, 2012 at 2:40am UTC
Thanks a million man. If I didn't finish this program it would've haunted me all week!
Topic archived. No new replies allowed.