I'm working on a program for my class which requires us to create an Ideal Gas Law solver using if and switch commands. The program should ask the user which variable they want to solve for: pressure, temperature or volume. After picking which one, the program should ask for the other two in appropriate units. I've continued working on the program when I ran into an issue. I've also included a few default gasses with their corresponding molar masses and a 6th option for the user to input their own molar mass but rather than redirecting a user to input their own molar mass it goes straight to asking for the variable input and I can't figure out how to get around this issue. I'm fairly new to C++ and stumbled on this forum for help. I'm not asking for anyone to complete my program for me, but rather point out what I can include to make it suit what I'm looking for. Any additional help on how what is wrong in my program would be very very appreciated. Again, I know that I need to know how to do this to pass my class so all I ask is guidance. Thank you so much!
Code:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
//Declare and initialize variables and constants
//p = Pressure (kPa), V = Volume (m^3), m = mass (kg), R = Gas Constant (0.0281), T = Temperature (C) and (K)
//MM = other molar mass
int p, V, m, R, T, K, MM, code ;
R = 0.287 ;
K = T + 273.15 ;
//**Print**//
//Introduction
cout << "Welcome to the My Ideal Gas Law Solver! \n" <<endl;
cout << "This program solves p*V=m*R*T for p, V or T given" <<endl;
cout << "the other two along with mass and the type of gas. \n" <<endl;
cout << "Select a gas by entering the corresponding number:" <<endl;
cout << "(1) Steam M = 18.02" <<endl;
cout << "(2) Nitrogen M = 28.01" <<endl;
cout << "(3) Air M = 28.97" <<endl;
cout << "(4) Oxygen M = 32.00" <<endl;
cout << "(5) Carbon Dioxide M = 44.01" <<endl;
cout << "(6) Other \n" <<endl;
//User inputs
switch (code)
{
case 1:
cout << "You have chosen Steam." <<endl;
break;
case 2:
cout << "You have chosen Nitrogen." <<endl;
break;
case 3:
cout << "You have chosen Air." <<endl;
break;
case 4:
cout << "You have chosen Oxygen." <<endl;
break;
case 5:
cout << "You have chosen Carbon Dioxide." <<endl;
break;
case 6:
cout << "Please enter a Molar Mass for the gas you wish to calculate in kg:" <<endl;
break;
default:
cout << "Error with selection. Please pick a number between 1 and 6 next time." <<endl;
break;
}
int input = 0;
while (((input<1 || input>5) && input!=6))
{ cout << "Please enter an integer between 1 and 6: ";
cin >> input;
}
if (code == 6)
{
cin >> MM ;
cout << "You have chosen " << MM <<endl;
}
cout << "Select a variable to solve for by entering the corresponding number: " <<endl;
cout << "(1) Pressure" <<endl;
cout << "(2) Volume" <<endl;
cout << "(3) Temperature" <<endl;
cin >> code;
switch (code)
{
case 1:
cout << "Please enter the volume in cubic meters: " <<endl;
cin >> V;
cout << "Please enter the temperature in degrees celsius: " <<endl;
cin >> T;
break;
case 2:
cout << "Please enter the pressure in kiloPascals: " <<endl;
cin >> p;
cout << "Please enter the temperature in degrees celsius: " <<endl;
cin >> T;
break;
case 3:
cout << "Please enter the pressure in kiloPascals: " <<endl;
cin >> p;
cout << "Please enter the volume in cubic meters: " <<endl;
cin >> V;
break;
//Compute Ideal Gas Law equation
if (code == 1)
{
p = (m * R * T)/V ;
}
if (code == 2)
{
V = (m * R * T)/p ;
}
if (code == 3)
{
T = (p * V)/(m * R) ;
}
//Compute Temperature conversion
cout << "Ideal Gas Law computation results:" << endl;
cout << "Gas name: " << endl;
cout << "Unknown value: " << endl;
cout << "Molar weight: " << endl;
cout << "Gas constant: " << R << " kJ/kg-K" << endl;
cout << "Mass: " << endl;
cout << "Pressure: " << p << " kPa" << endl;
cout << "Temperature: " << T << " C" << endl;
cout << "Volume: " << V << " m^3" << endl;
return 0;
}
}