I'm just writing this program based off some things we have been doing in chemistry. I couldn't get it to go to choice two without going through choice one. So I changed some code and used strings. Now I'm getting the error 'else' without a previous 'if'
I just want to know how to get it to go to choice 2 ... any help is appreciated. (sorry for being a noob)
EDIT:
You guys rock! Thanks so much, I'm having trouble with trying to figure out the remainders now. The program runs good without the d1 and the % to find variable. When I added it, it skips steps and whatnot. Any idea on how I can get remainder with answer. (I updated code)
#include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <math.h>
usingnamespace std;
//Was getting error of the having variables undefined
// MISSING THE SECOND PRESSURE INT
int a;
int b;
int c;
int d;
int d1;
//MISSING THE SECOND VOLUME INT
int pressureone;
int volume;
int pressuretwo;
int answer;
int main()
{
cout << "Boyles Law" << endl;
cout << "Are you missing the pressure or volume?" << endl;
string choice1;
cout << "Enter, pressure or volume to continue" << endl;
getline (cin, choice1);
if(choice1=="pressure")
{
int a;
cout << "Enter the pressure" << endl;
cin >> a;
int b;
cout << "Enter the first volume" << endl;
cin >> b;
int c;
cout << "Enter the second volume" << endl;
cin >> c;
d = a * b / c ;
d1 = a + b % c;
cout << "The missing pressure is: ";
cout << d << endl;
cout << d1 << endl;
}
elseif(choice1=="volume")
{
int pressureone;
cout << "Enter the first pressure" << endl;
cin >> pressureone;
int volume;
cout << "Enter the volume" << endl;
cin >> volume;
int pressuretwo;
cout << "Enter the second pressure" << endl;
cin >> pressuretwo;
int answer;
answer = pressureone * volume / pressuretwo;
cout << answer << endl;
}
else {
cout << "Error:1" << endl;
system("PAUSE");
cout << " Please enter; volume or pressure\n";
string choice1;
cout << "I'm missing a pressure." << endl;
cout << "I'm missing a volume." << endl;
getline (cin, choice1);
}
return 0;
}
One thing your missing for sure is {} around your if.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
if(choice1=="pressure")
{int a;
cout << "Enter the pressure" << endl;
cin >> a;
int b;
cout << "Enter the first volume" << endl;
cin >> b;
int c;
cout << "Enter the second volume" << endl;
cin >> c;
d = a * b / c ;
cout << "The missing pressure is: ";
cout << d << endl;
}