Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 02-22-2006, 02:30 PM   #1
Daemon Poster
 
mayorredbeard's Avatar
 
Join Date: Nov 2004
Posts: 1,246
Default Why won't this else statement work?

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;
          }
 
 
      
}
__________________

__________________
Karma/rep is always appreciated
01010010011001010110010001100010011001010110000101 11001001100100
010011110111010101110100

There are only 10 kinds of people in this world. Those who can read binary, and those who can't.
mayorredbeard is offline   Reply With Quote
Old 02-22-2006, 04:30 PM   #2
Fully Optimized
 
UK31337's Avatar
 
Join Date: Feb 2005
Posts: 2,776
Default Re: Why won't this else statement work?

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.
__________________

__________________
Master of common sense. If you don't like it, stop reading.
UK31337 is offline   Reply With Quote
Old 02-22-2006, 04:37 PM   #3
Daemon Poster
 
mayorredbeard's Avatar
 
Join Date: Nov 2004
Posts: 1,246
Default Re: Why won't this else statement work?

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.
__________________
Karma/rep is always appreciated
01010010011001010110010001100010011001010110000101 11001001100100
010011110111010101110100

There are only 10 kinds of people in this world. Those who can read binary, and those who can't.
mayorredbeard is offline   Reply With Quote
Old 02-22-2006, 05:03 PM   #4
Daemon Poster
 
mayorredbeard's Avatar
 
Join Date: Nov 2004
Posts: 1,246
Default Re: Why won't this else statement work?

figured it out... The semicolons after if (problem = pressureOne); ended the statement right their. I needed to get rid of those.
__________________
Karma/rep is always appreciated
01010010011001010110010001100010011001010110000101 11001001100100
010011110111010101110100

There are only 10 kinds of people in this world. Those who can read binary, and those who can't.
mayorredbeard is offline   Reply With Quote
Old 02-23-2006, 11:34 PM   #5
Daemon Poster
 
RewtGuy's Avatar
 
Join Date: Dec 2004
Posts: 595
Send a message via AIM to RewtGuy
Default Re: Why won't this else statement work?

yeah, exactally what I said in the other thread.

conditional statements,
if, else, loops, switch case don't have semi colons
__________________
Windows: A thirty-two bit extension and GUI shell to a sixteen bit patch to an eight bit operating system originally coded for a four bit microprocessor and sold by a two-bit company that can't stand one bit of competition.
RewtGuy is offline   Reply With Quote
Old 02-26-2006, 09:24 PM   #6
Daemon Poster
 
mayorredbeard's Avatar
 
Join Date: Nov 2004
Posts: 1,246
Default Re: Why won't this else statement work?

ignorant me forgot, hehe this one gave me a headache for quite some time and drove me insane.
__________________

__________________
Karma/rep is always appreciated
01010010011001010110010001100010011001010110000101 11001001100100
010011110111010101110100

There are only 10 kinds of people in this world. Those who can read binary, and those who can't.
mayorredbeard is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 09:08 PM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0