please check what is wrong help please please
i need do and while in the program if the user inputs negative the output will be invalid
#include <iostream>
#include <cctype>
#include <windows.h>
using namespace std;
int main ()
{
int n,p,v,t, ctr;
char again,A,B,C,letter;
cout<<"(A) TO find pressure: \n (B) To find volume: \n (C) To find Temperature: "<< endl;
cin>>letter;
if (letter==A)
do{
if(!(p == 0))
cout<<"input value of number of moles"<<endl;
cin>>n;
cout<<"input value of temperature."<<endl;
cin>>t;
cout<<"input value of Volume"<<endl;
cin>>v;
cout<<"the value of Pressure="<<p<<endl;
}while ((!letter == A));
p=(n*0.0821*t)/v;
else if (letter==B)
do {cout<<"input value of number of moles"<<endl;
cin>>n;
cout<<"input value of temperature."<<endl;
cin>>t;
cout<<"input value of Pressure"<<endl;
cin>>p;
v=(n*0.0821*t)/p;
cout<<"the value of volume="<<v<<endl;
}while (!(letter == B));
else if (letter==C)
do {cout<<"input value of number of moles"<<endl;
cin>>n;
cout<<"input value of Pressure."<<endl;
cin>>p;
cout<<"input value of Volume"<<endl;
cin>>v;
t=(p*v)/(0.0821*n);
cout<<"the value of temperature="<<t<<endl;
}while ((!letter == C));
int n,p,v,t, ctr;
char again,A,B,C,letter;
// ...
do{
if(!(p == 0))
cout<<"input value of number of moles"<<endl;
cin>>n;
cout<<"input value of temperature."<<endl;
cin>>t;
cout<<"input value of Volume"<<endl;
cin>>v;
cout<<"the value of Pressure="<<p<<endl;
}while ((!letter == A));
First, p is not initialized so you don't know if it will be 0 or not.
Second You need { } around all of those cout/cins. Otherwise, only line 7 above is inside of the if
Third, you need { } around each if(letter == 'A') block.
Fourth, n,p,v,t, are all ints, make them doubles.
Fifth: A/B/C aren't initialized to a value. I assume you wanted letter == 'A'
#include <iostream>
//#include <cctype> not needed
//#include <windows.h> not needed
usingnamespace std;
int main ()
{
double n,p,v,t;
char letter;
cout<<"(A) TO find pressure: \n (B) To find volume: \n (C) To find Temperature: "<< endl;
cin>>letter;
if (letter=='A') // Could use a switch statement here
{
do
{
if(!(p == 0)) // What do you think p is now?
{
cout<<"input value of number of moles"<<endl;
cin>>n;
cout<<"input value of temperature."<<endl;
cin>>t;
cout<<"input value of Volume"<<endl;
cin>>v;
cout<<"the value of Pressure="<<p<<endl; // P isn't calculated yet!
}
}while ((!letter == 'A')); // Of course letter == 'A', That's why we're in the if statement
p=(n*0.0821*t)/v;
}
elseif (letter=='B')
{
do
{
cout<<"input value of number of moles"<<endl;
cin>>n;
cout<<"input value of temperature."<<endl;
cin>>t;
cout<<"input value of Pressure"<<endl;
cin>>p;
v=(n*0.0821*t)/p;
cout<<"the value of volume="<<v<<endl;
}while (!(letter == 'B'));
}
elseif (letter=='C')
{
do
{
cout<<"input value of number of moles"<<endl;
cin>>n;
cout<<"input value of Pressure."<<endl;
cin>>p;
cout<<"input value of Volume"<<endl;
cin>>v;
t=(p*v)/(0.0821*n);
cout<<"the value of temperature="<<t<<endl;
}while ((!letter == 'C'));
}
return 0;
}
I think this one is a working solution. I put everything into one big do loop. The only thing that isn't in there is a message telling you that you've entered an invalid input.
#include <iostream>
usingnamespace std;
int main ()
{
double n,p,v,t;
char letter, again;
do
{
do
{
cout<<"(A) To find pressure: \n(B) To find volume: \n(C) To find Temperature: "<< endl;
cin>>letter;
} while (letter != 'A' && letter != 'B' && letter != 'C');
if (letter != 'A')
{
do
{
cout<<"input value of Pressure"<<endl;
cin>>p;
} while (p <= 0);
}
if (letter != 'B')
{
cout<<"input value of Volume"<<endl;
cin>>v;
}
if (letter != 'C')
{
cout<<"input value of Temperature."<<endl;
cin>>t;
}
cout<<"input value of number of Moles"<<endl;
cin>>n;
if (letter=='A')
{
p=(n*0.0821*t)/v;
cout<<"the value of Pressure="<<p<<endl;
}
elseif (letter=='B')
{
v=(n*0.0821*t)/p;
cout<<"the value of Volume="<<v<<endl;
}
elseif (letter=='C')
{
t=(p*v)/(0.0821*n);
cout<<"the value of Temperature="<<t<<endl;
}
cout << "do you want to go again? (Y/n): ";
cin >> again;
}while (again != 'n' && again != 'N');
return 0;
}
kindly please check this. there is something wrong if i press Y the output is invalid input but it should be returning back to the main choices like "To find pressure etc...
if (letter != 'A')
{
do
{
cout<<"input value of Pressure"<<endl;
cin>>p;
do{
if (p<0)
{
cout<<"invalid input"<<endl;
}
cout << "do you want to go again? (Y/n): ";
cin >> again;
}while (again != 'n' && again != 'N');
oh i see. please check this. please correct my mistakes
i tried to put (t<=0) but when i executed it everytime i put negative number and after i correct it into postive it still resulted to "please enter a non negative number
#include <iostream>
#include <windows.h>
using namespace std;
int main ()
{
double n,p,v,t;
char letter, again;
do{
system("cls");
do
{
cout<<"GAS LAW SOLVER"<<endl;
cout<<"******************************\n**PLEASE CHOOSE YOUR PROBLEM**\n** (A) To find pressure: **\n** (B) To find volume: **\n**(C) To find Temperature: **\n**(D) To find Number of Moles **\n******************************"<< endl;
cin>>letter;
letter=toupper(letter);
if (letter!='A' && letter!='B' && letter!='C' && letter!='D')
cout<<endl<<endl<<"INVALID INPUT!"<<endl<<endl;
} while (letter !='A' && letter !='B' && letter != 'C' && letter != 'D');
if (letter != 'A')
{
do{
cout<<"input value of Pressure"<<endl;
cin>>p;
if (p <= 0)
cout << "Invalid Input" << endl;
} while ( p <= 0 );
}
if (letter != 'B')
{
do{
cout<<"input value of Volume:"<<endl;
cin>>v;
if (v<=0)
cout << "Invalid Input" << endl;
cout<<"\n**Please enter a non negative number for pressure to proceed**\n"<<endl<<endl;
}while (v<=0);
}
if (letter != 'C')
{
do{
cout<<"input value of Temperature."<<endl;
cin>>t;
if (t<=0)
cout<< "Invalid Input"<<endl;
cout<<"\n**Please enter a non negative number for pressure to proceed**\n"<<endl<<endl;
}while (t<=0);
}
if (letter != 'D')
{
do{
cout<<"input value of number of Moles"<<endl;
cin>>n;
if (n<=0)
cout << "Invalid Input" << endl;
cout<<"\n**Please enter a non negative number for pressure to proceed**\n"<<endl<<endl;
}while (n<=0);
}
if (letter=='A')
{
p=(n*0.0821*t)/v;
cout<<"the value of Pressure="<<p<<endl;
}
else if (letter=='B')
{
v=(n*0.0821*t)/p;
cout<<"the value of Volume="<<v<<endl;
}
else if (letter=='C')
{
t=(p*v)/(0.0821*n);
cout<<"the value of Temperature="<<t<<endl;
}
else if (letter=='D')
{
n=(p*v)/(0.0821*t);
cout<<"the value of Moles="<<n<<endl;
}
cout << "do you want to go again? (Y/n): ";
cin >> again;
}while (again != 'n' && again != 'N');