keep getting "expected primary expression before 'else'"

This is a discussion on keep getting "expected primary expression before 'else'" within the C++ Programming forums, part of the General Programming Boards category; i am writing a code for class and have gotten everything else to work with my compiler except my else ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    32

    Post keep getting "expected primary expression before 'else'"

    i am writing a code for class and have gotten everything else to work with my compiler except my else statements.
    I keep getting the "expected primary expression before 'else'" and the "expected ( before 'else'" as well

    the first functions of my code is as follows:
    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int main()
    {
    /*
    a=volume
    b=surface area
    r=radius
    */
    
    	double x,r,a,b,c,h,l,w;
            (putting a resp return command here after i finish this problem)
    	cout<< "What shape would you like?"<<endl;
    	cout<< "1) Sphere"<< endl;
    	cout<< "2) Cone"<< endl;
    	cout<< "3) Rectangular Prism"<< endl;
    	cout<< "4) Cylinder"<< endl;
    	cin>>x;
    		if (x==1); // Sphere
    		{
    		cout<<"Radius=";
    		cin>>r;
    		while(cin.fail()<'0');
    		{
    			cout<<"ERROR! VALUE MUST BE GREATER THAN 0"<<endl;
    			cin.clear();
    			cin.ignore(256,'\n');
    			cout<< "Radius=";
    			cin>>r;
    			};
    		a=((4.0/3.0)*M_PI)*pow(r,3.0);
    		b=(4*M_PI)*pow(r,2.0);
    		cout<<"Volume="<<a<< endl;
    		cout<<"Surface Area="<<b<<endl;
    		}
    		else if (x==2); // Cone
    		{
    (Yes i am a next liner)

    The rest of the program is very similar to this part with the 'if' statement followed by command code followed by and else statement and repeat through all 4 shapes

    if you would like to see the next section of the code let me know and i will post it in here.

    any help would be great, thx

    Dave
    Last edited by Creatlv3; 02-09-2010 at 06:54 PM.

  2. #2
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    A couple points of interest

    Code:
    cout<<"Volume="<<a<< endl;
    		cout<<"Surface Area="<<b<<endl;
    		};
    Why the semicolon after the bracket?

    Code:
    while(cin.fail()<'0');
    Is this really what you want?

    Code:
    (putting a resp return command here after i finish this problem)
    How does this line even compile since it is outside of a comment?

    Code:
    else if (x == 2);
    Hmm.


    You need to format your code a bit better. It is very hard to read.
    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int main()
    {
        /*
        a = volume
        b = surface area
        r = radius
        */
    
        double x;
        double r;
        double a;
        double b;
        double c,
        double h;
        double l;
        double w;
    
        // (putting a resp return command here after i finish this problem)
        cout << "What shape would you like?"<< endl;
        cout << "1) Sphere"<< endl;
        cout << "2) Cone"<< endl;
        cout << "3) Rectangular Prism"<< endl;
        cout << "4) Cylinder"<< endl;
        cin >> x;
    
        if (x == 1); // Sphere
        {
            cout << "Radius=";
            cin >> r;
            while(cin.fail() < '0');
            {
    	cout << "ERROR! VALUE MUST BE GREATER THAN 0"<< endl;
    	cin.clear();
    	cin.ignore(256,'\n');
    	cout << "Radius=";
    	cin >> r;
            };
            
            a = ( (4.0 / 3.0) * M_PI) * pow(r,3.0);
            b = (4 * M_PI) * pow(r,2.0);
            cout << "Volume="<< a << endl;
            cout << "Surface Area=" << b << endl;
        };
        else if (x == 2); // Cone
        {
    Reformatted but not fixed. Now you can probably spot your mistakes.
    Last edited by VirtualAce; 02-09-2010 at 07:02 PM.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    You tried what? Post the complete and "updated" or "fixed" code that you are now running.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    32
    would you like the next section of code to see how i used the
    Code:
     else if (x==2);
    and the
    Code:
     (putting a resp return command here after i finish this problem)
    was a note i put into my notepad version to remind myself to do that, it is not actually in the code itself.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    I dont know what that post is supposed to mean. To get help,
    Quote Originally Posted by nadroj
    Post the complete and "updated" or "fixed" code that you are now running.
    Otherwise we can only guess the exact code that you're currently running. This, of course, would be useless.

    You, of course removed that semi colon and line of raw text ("(putting..."), right?

    EDIT: Also, its only wasting our time (and yours) if you don't post the exact code (hopefully thats enough emphasize). If you put that line of text in your post, which your saying is your "code", then it only makes sense for us to say "this line is an error". Just FYI for the future, post the exact file you just compiled and are receiving errors for.
    Last edited by nadroj; 02-09-2010 at 09:14 PM.

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    32
    ok, the exact code i am now using is
    Code:
     #include <iostream>
    #include <cmath>
    using namespace std;
    
    int main()
    
    {
    
    	/*
    	a=volume
    	b=surface area
    	r=radius
    	*/
    
    
    	double x;
    	double r;
    	double a;
    	double b;
    	double c;
    	double h;
    	double l;
    	double w;
    
    	cout<< "What shape would you like?"<<endl;
    	cout<< "1) Sphere"<< endl;
    	cout<< "2) Cone"<< endl;
    	cout<< "3) Rectangular Prism"<< endl;
    	cout<< "4) Cylinder"<< endl;
    	cin>>x;
    
    	if (x==1); // Sphere
    		{
    		cout<<"Radius=";
    		cin>>r;
    		while(cin.fail()<'0');
    	        {
    		cout << "ERROR! VALUE MUST BE GREATER THAN 0"<<endl;
    		cin.clear();
    		cin.ignore(256,'\n');
    		cout<< "Radius=";
    		cin>>r;
    		}
    
    		a=((4.0/3.0)*M_PI)*pow(r,3.0);
    		b=(4*M_PI)*pow(r,2.0);
    		cout<<"Volume="<<a<< endl;
    		cout<<"Surface Area="<<b<<endl;
    	}
    	else if (x==2); // Cone
    	{
    i reorganized it how bubba recommended

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Quote Originally Posted by Bubba
    Reformatted but not fixed. Now you can probably spot your mistakes.
    As he stated, he reformatted but not fixed it.

    As mentioned a few times, you have trailing semi-colons where they probably shouldnt be. Here is one that you should removed the semi-colon after: "if (x==1);". And another: "while(cin.fail()<'0');"

    Also, cin.fail returns a "bool", so you probably want to compare the result to either 0/false or 1/true, instead of a character (the character zero: '0'), as you are now.

    There is another trailing semi-colon to remove here "else if (x==2);"

    Also
    Code:
    else if (x==2); // Cone
    	{
    is missing the closing bracket "}". Finally, "int main" is missing its closing bracket "}".

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, 06:50 PM
  2. expected primary expression before "." token
    By melodious in forum C++ Programming
    Replies: 4
    Last Post: 07-11-2007, 04:39 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. recursion error
    By cchallenged in forum C Programming
    Replies: 2
    Last Post: 12-18-2006, 08:15 AM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM

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