Thread: parse error before else

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    22

    Post parse error before else

    Hi. I am trying to program a short algebra routine to give me the answers to a quadratic function. The compiler says there are "parse errors before 'else'" on lines 35, 41 and 45. Somebody probably knows why. Thanx

    code:

    #include <iostream.h>
    #include <stdlib.h>

    int main()
    {
    //Solve a quadratic equation for x.
    cout<<"Solve a*x^2 + b*x + c = 0, for x."<<endl;
    cout<<""<<endl;
    cout<<""<<endl;
    //declare variables
    double a,b,c,delta,x,x1r,x2r,real,imag;
    cout<<""<<endl;
    cout<<"Please type in a."<<endl;
    cin>>a;
    cout<<"Please type in b."<<endl;
    cin>>b;
    cout<<"Please type in c."<<endl;
    cin>>c;
    cout<<""<<endl;
    cout<<""<<endl;
    cout<<"The value for a is "<<a<<". The value for b is "<<b<<". The value for c is "<<c<<"."<<endl;
    cout<<""<<endl;
    cout<<""<<endl;
    system("PAUSE");
    if(a==0)
    if(b==0)
    if(c==0)
    cout<<"Indefinite equation."<<endl;
    else
    cout<<"Impossible equation."<<endl;
    else
    x=-c/b;
    cout<<"One real solution x="<<x<<"."<<endl;
    else
    delta=b^2-4*a*c;
    if(delta>0)
    x1r=(-b+sqrt(delta))/(2*a);
    x2r=(-b-sqrt(delta))/(2*a);
    cout<<"Two real solutions x1="<<x1r<<"x2="<<x2r<<"."<<endl;
    else
    if(delta=0)
    x=b/(2*a);
    cout<<"One double solution x="<<x<<"."<<endl;
    else
    real=-b/(2*a)
    imag=sqrt(-delta)/(2*a);
    cout<<"Two complex solutions x1="<<real<<" + "<<imag<<"i and x2="<<real<<" - "<<imag<<"i."<<endl;
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Code:
    if(a==0)
    if(b==0)
    if(c==0)
    can't do this, do this:
    Code:
    if(a == 0 && b == 0 && c == 0)
    and use Code Tags

  3. #3
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297

    Re: parse error before else

    I think the illegal part is in bold here...

    Originally posted by sdchem
    Hi. I am trying to program a short algebra routine to give me the answers to a quadratic function. The compiler says there are "parse errors before 'else'" on lines 35, 41 and 45. Somebody probably knows why. Thanx

    code:
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    int main()
    {
    		//Solve a quadratic equation for x.
    		cout<<"Solve a*x^2 + b*x + c = 0, for x."<<endl;
    		cout<<""<<endl;
    		cout<<""<<endl;
    		//declare variables
    		double a,b,c,delta,x,x1r,x2r,real,imag;
    		cout<<""<<endl;
    		cout<<"Please type in a."<<endl;
    		cin>>a;
    		cout<<"Please type in b."<<endl;
    		cin>>b;
    		cout<<"Please type in c."<<endl;
    		cin>>c;
    		cout<<""<<endl;
    		cout<<""<<endl;
    		cout<<"The value for a is "<<a<<".  The value for b is "<<b<<".  The value for c is "<<c<<"."<<endl;
    		cout<<""<<endl;
    		cout<<""<<endl;
            system("PAUSE");
                            if(a==0)
                                    if(b==0)
                                            if(c==0)
                                            cout<<"Indefinite equation."<<endl;
                                            else
                                            cout<<"Impossible equation."<<endl;
                                    else
                                    x=-c/b;
                                    cout<<"One real solution x="<<x<<"."<<endl;
                            else
                            delta=b^2-4*a*c;
                                    if(delta>0)
                                    x1r=(-b+sqrt(delta))/(2*a);
                                    x2r=(-b-sqrt(delta))/(2*a);
                                    cout<<"Two real solutions x1="<<x1r<<"x2="<<x2r<<"."<<endl;
                                    else
                                        if(delta=0)
                                        x=b/(2*a);
                                        cout<<"One double solution x="<<x<<"."<<endl;
                                        else
                                        real=-b/(2*a)
                                        imag=sqrt(-delta)/(2*a);
                                        cout<<"Two complex solutions x1="<<real<<" + "<<imag<<"i and x2="<<real<<" - "<<imag<<"i."<<endl;
            return 0;
    }
    You put an else where there is no if.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    the solution is to use curly brackets around the code that you want to group into an if statement
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  2. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM