Having Trouble With Nested Ifs

Hello, So I'm working on an assignment for a class in which we take user input and solve for a function. I'm using the ideal gas law PV = NRT. The code seems to run fine but it skips the nested if on line 18, and just solves for pressure.

Hello, So I'm working on an assignment for a class in which we take user input and solve for a function. I'm using the ideal gas law PV = NRT. The code seems to run fine but it skips the nested if on line 18, and just solves for pressure. Sorry if it looks weird, I'm new to the site.

1 #include<iostream>
2using namespace std;

3 int main()
4 {
5 // declaring variables and constants
6 double press, vol, mol, temp;
7 const double GAS_CONSTANT = 0.08206;
8 char letter, letter2;
9
10 do
11 {
12 // asking what to solve for
13 cout << "What Would You Like To Solve For?\n Pressure = P\n Volume = V\n Moles = M\n Temperature = T\n";
14 cin >> letter;
15
16 // solving for pressure
17 if (letter == 'P')
18 {
19 cout << "Enter The Volume (In Liters).\n";
20 cin >> vol;
21 cout << "Enter The Number Of Moles.\n";
22 cin >> mol;
23 cout << "Enter The Temperature (In Kelvin).\n";
24 cin >> temp;
25
26 press = (mol*temp*GAS_CONSTANT) / vol;
27 cout << "The Pressure Is\n" << press << " Atmospheres\n";
28 }
29 else
30 {
31 // solving for volume
32 if (letter == 'V')
33 {
34 cout << "Enter The Pressure (In Atmospheres)\n";
35 cin >> press;
36 cout << "Enter The Number Of Moles.\n";
37 cin >> mol;
38 cout << "Enter The Temperature (In Kelvin).\n";
39 cin >> temp;
40
41 vol = (mol*temp*GAS_CONSTANT) / press;
42 cout << "The Volume Is\n" << vol << " Liters";
43 }
44 else
45 {
46 // solving for moles
47 if (letter == 'M')
48 {
49 cout << "Enter The Pressure (In Atmospheres)\n";
50 cin >> press;
51 cout << "Enter The Volume (In Liters).\n";
52 cin >> vol;
53 cout << "Enter The Temperature (In Kelvin).\n";
54 cin >> temp;
55
56 mol = (press*vol) / (GAS_CONSTANT*temp);
57 cout << "The Number Of Moles Is\n" << mol << " Moles";
58 }
59 else
60 {
61 // solving for temperature
62 cout << "Enter The Pressure (In Atmospheres)\n";
63 cin >> press;
64 cout << "Enter The Volume (In Liters).\n";
65 cin >> vol;
66 cout << "Enter The Number Of Moles.\n";
67 cin >> mol;
68
69 temp = (press*vol) / (mol*GAS_CONSTANT);
70 cout << "The Temperature Is\n" << temp << " Kelvin";
71 }
72 }
73 }
74 cout << "Would You Like To Calculate Something Else?\nY or N\n";
75 cin >> letter2;
76 }
77 while (letter2 == 'Y');
78 }
Last edited on
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
#include <iostream>

int main()
{
    char again ;
    do
    {
        std::cout << "What would you like to solve for?\n"
                  << "Pressure = P\n Volume = V\n Moles = M\n Temperature = T\n" ;
        char choice ;
        std::cin >> choice ;

        if( choice == 'P' || choice == 'p' )
        {
            // solve for pressure
        }

        else if( choice == 'V' || choice == 'v' )
        {
            // solve for volume
        }

        else if( choice == 'M' || choice == 'm' )
        {
            // solve for moles
        }

        else if( choice == 'T' || choice == 't' )
        {
            // solve for temperature
        }

        else std::cout << "invalid choice\n" ;

        std::cout << "Would you like to calculate something else (y/n): " ;
        std::cin >> again ;
    }
    while( again == 'y' || again == 'Y' ) ;
}


An alternative is to use a switch-case construct.
Topic archived. No new replies allowed.