Gas law C++ please helppppp asap

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));

return 0;
}
Last edited on
Please use the code tags for posting code.

Use double type for p,v,t,n. Also why should you use !(p==0) simply use p!=0.

Any number is negative if it is less than zero. So simply check
1
2
3
4
if(v<0)
{
      cout<<"invalid input";
}


Do it for every input.
Here's your main issue:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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'

I'll post some updated code in a moment to try.
Try this. I still see some significant issues and can give you some pointers on that. But see if you can figure it out first.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
//#include <cctype> not needed
//#include <windows.h> not needed
using namespace 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; 
    }
    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'));
    }
    return 0;
}
Last edited on
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
using namespace 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;
        }
        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;
        }

        cout << "do you want to go again? (Y/n): ";
        cin >> again;
    }while (again != 'n' && again != 'N');
    return 0;
}
Last edited on
THANK YOU THIS A GREAT BIG HELP SRSLY THIS IS FOR MY PROJECT THANK YOU THANK YOU
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');

} while (p <= 0);

you've been asked to use code tags, but you seem not to want to..

and stewbond's solution work's fine for me.
Last edited on
please correct my work

if the user inputs negative it the output will be invalid input
Last edited on
You wanted to use do-while, but I'd rather use while. If you want to display "invalid input" change this:
1
2
3
4
5
do
{
    cout<<"input value of Pressure"<<endl;
    cin>>p;
} while (p <= 0);

to this:
1
2
3
4
5
6
7
8
9
while (true)
{
    cout<<"input value of Pressure"<<endl;
    cin>>p;

    if (p > 0) break;

    cout << "Invalid Input" << endl;
}
is there another way for break bcoz our teacher havent yet taught us the break/case/switch
There's always another way...
1
2
3
4
5
6
7
8
do
{
    cout<<"input value of Pressure"<<endl;
    cin>>p;

    if (p <= 0) 
        cout << "Invalid Input" << endl;
} while ( p <= 0 );


But in this case you have the conditional statement twice, so it's not as elegant.
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');

return 0;
}
Last edited on
please correct all the mistakes ive done. i have to make flowchart for this. this project will be submitted on wednesday huhu. :))
Topic archived. No new replies allowed.