Why won't this else statement work?

mayorredbeard

Daemon Poster
Messages
1,246
In followup to my sloppily written grahams law program, i decided to make one for boyles law. But i get an error on my else statement, and i can't figure out whats wrong. It tells me i am 'missing a primary expression before else'

Code:
//INCLUDE FILES
#include <iostream>
#include <cmath>
#include <string>
#include <conio.h>

using namespace std;

//Main function
int main ()
{
    double pressureOne, pressureTwo, volumeOne, volumeTwo, answer;
    string problem;
            
    cout << "Are you solving for pressureOne, pressureTwo, volumeOne, or volumeTwo?";
    cin >> problem;
    
    
    //SOLVING FOR pressureOne
        if (problem == "pressureOne");
           
                    cout << "PressureTwo: ";
                    cin >> pressureTwo;
                    cout << "VolumeOne: ";
                    cin >> volumeOne;
                    cout << "VolumeTwo: ";
                    cin >> volumeTwo;
           
                    {
                    answer = (pressureTwo * volumeTwo) / volumeOne;
                    cout << "The answer is: " << answer;
                    }
           
           
    //SOLVING FOR pressureTwo
        if (problem == "pressureTwo");
        
                    cout << "PressureOne: ";
                    cin >> pressureOne;
                    cout << "VolumeOne: ";
                    cin >> volumeOne;
                    cout << "VolumeTwo: ";
                    cin >> volumeTwo;
          
                    { 
                    answer = (pressureOne * volumeOne) / volumeTwo;
                    cout << "The answer is: " << answer;
                    }
            
            
    //SOLVING FOR volumeOne
        if (problem == "volumeOne");
           
                    cout << "PressureOne: ";
                    cin >> pressureOne;
                    cout << "PressureTwo: ";
                    cin >> pressureTwo;
                    cout << "VolumeTwo: ";
                    cin >> volumeTwo;
                    
                    {
                    answer = (pressureTwo * volumeTwo) / pressureOne;
                    cout << "The answer is: " << answer;
                    }
           
           
    //SOLVING FOR volumeTwo
        if (problem == "volumeTwo");
           
                    cout << "PressureOne: ";
                    cin >> pressureOne;
                    cout << "PressureTwo: ";
                    cin >> pressureTwo;
                    cout << "VolumeOne: ";
                    cin >> volumeOne;
                    
                    {
                    answer = (pressureOne * volumeOne) / pressureTwo;
                    cout << "The answer is: " << answer;
                    }
     //ELSE STATEMENT
     else
          {
                cout << "Invalid selection, bailing";
                getche();
                return 0;
          }
 
 
      
}
 
My C++ is a little rusty, but I think it's because you've got many an if statement and only one else. Try this:

if (problem == "pressureOne");

cout << "PressureTwo: ";
cin >> pressureTwo;
cout << "VolumeOne: ";
cin >> volumeOne;
cout << "VolumeTwo: ";
cin >> volumeTwo;

{
answer = (pressureTwo * volumeTwo) / volumeOne;
cout << "The answer is: " << answer;
}


//SOLVING FOR pressureTwo
else if (problem == "pressureTwo");

cout << "PressureOne: ";
cin >> pressureOne;
cout << "VolumeOne: ";
cin >> volumeOne;
cout << "VolumeTwo: ";
cin >> volumeTwo;

{
answer = (pressureOne * volumeOne) / volumeTwo;
cout << "The answer is: " << answer;
}


//SOLVING FOR volumeOne
else if (problem == "volumeOne");

cout << "PressureOne: ";
cin >> pressureOne;
cout << "PressureTwo: ";
cin >> pressureTwo;
cout << "VolumeTwo: ";
cin >> volumeTwo;

{
answer = (pressureTwo * volumeTwo) / pressureOne;
cout << "The answer is: " << answer;
}


//SOLVING FOR volumeTwo
else if (problem == "volumeTwo");

cout << "PressureOne: ";
cin >> pressureOne;
cout << "PressureTwo: ";
cin >> pressureTwo;
cout << "VolumeOne: ";
cin >> volumeOne;

{
answer = (pressureOne * volumeOne) / pressureTwo;
cout << "The answer is: " << answer;
}
//ELSE STATEMENT
else
{
cout << "Invalid selection, bailing";
getche();
return 0;
}

Notice the extra else's? That, I hope, will fix the problem.

Hope I've helped.
 
then i tried running it (plus found some missign brackets :-d) and it will only follow the forumla for finding pressureOne. It's like the program is ignoring the statement if (problem == "pressureOne") and continues to follow the code as if i were solving for pressureOne no matter what.

Code:
//INCLUDE FILES
#include <iostream>
#include <cmath>
#include <string>
#include <conio.h>

using namespace std;

//Main function
int main ()

{
   string problem;         
    cout << "Solving for pressureOne, pressureTwo, volumeOne, volumeTwo?: ";
    cin >> problem;
    
    //SOLVING FOR pressureOne
        if (problem == "pressureOne");
           
           double pressureTwo, volumeOne, volumeTwo, answerOne;
           {
                    cout << "PressureTwo: ";
                    cin >> pressureTwo;
                    cout << "VolumeOne: ";
                    cin >> volumeOne;
                    cout << "VolumeTwo: ";
                    cin >> volumeTwo;
           
                    
                    answerOne = (pressureTwo * volumeTwo) / volumeOne;
                    cout << "The answer is: " << answerOne;
                    
           getche ();
           return 0;
           }
           
    //SOLVING FOR pressureTwo
        if (problem == "pressureTwo");
        {
           double pressureOne, volumeOne, volumeTwo, answerTwo;
           
                    cout << "PressureOne: ";
                    cin >> pressureOne;
                    cout << "VolumeOne: ";
                    cin >> volumeOne;
                    cout << "VolumeTwo: ";
                    cin >> volumeTwo;
          
                     
                    answerTwo = (pressureOne * volumeOne) / volumeTwo;
                    cout << "The answer is: " << answerTwo;
                    
            
            getche ();
            return 0;
        }
        
    //SOLVING FOR volumeOne
        if (problem == "volumeOne");
           {
           double pressureOne, pressureTwo, volumeTwo, answerThree;
           
                    cout << "PressureOne: ";
                    cin >> pressureOne;
                    cout << "PressureTwo: ";
                    cin >> pressureTwo;
                    cout << "VolumeTwo: ";
                    cin >> volumeTwo;
                    
                    
                    answerThree = (pressureTwo * volumeTwo) / pressureOne;
                    cout << "The answer is: " << answerThree;
                    
                   
           getche ();
           return 0;
           }
           
    //SOLVING FOR volumeTwo
        if (problem == "volumeTwo");
           {
           double pressureOne, pressureTwo, volumeOne, answerFour;
           
                    cout << "PressureOne: ";
                    cin >> pressureOne;
                    cout << "PressureTwo: ";
                    cin >> pressureTwo;
                    cout << "VolumeOne: ";
                    cin >> volumeOne;
                    
                    
                    answerFour = (pressureOne * volumeOne) / pressureTwo;
                    cout << "The answer is: " << answerFour;
                     
           getche ();
           return 0;
           }
 
      
}

And to malbuc87uk, if i do that, i get the same error just more times. I think ill just leave out the else statement for now, but what i cant understand is why when i run the program it solves as if i told it problem == pressureOne no matter what. I even ran a little test, in which after i told it what problem equaled, to display problem, by doign this:

Code:
string problem;         
    cout << "Solving for pressureOne, pressureTwo, volumeOne, volumeTwo?: ";
    cin >> problem;
    cout << problem;

and that worked. If i told the program i was solving for pressureTwo it would display, pressureTwo. So obviously it knows what the variable problem is. It's just completly ignoring my if statements, and just solvign for pressureOne no matter what.
 
figured it out... The semicolons after if (problem = pressureOne); ended the statement right their. I needed to get rid of those.
 
yeah, exactally what I said in the other thread.

conditional statements,
if, else, loops, switch case don't have semi colons
 
Back
Top Bottom