PV = nRT Solver

Hello! Below is a program I wrote after a Chemistry lecture today. PV = nRT is an incredibly fundamental equation within Chemistry. Otherwise known as the Ideal Gas Law, this formula combines basis’ of Boyle’s Law, Charles’ Law, and Avangadro’s Law into a simple equation, which we can use to find any missing variables from the equation, when given the other circumstances.

PV = nRT

P = pressure,

V = volume,

n = substance in moles,

R = 0.0821 (when unit is (atm*L)/(mol*K)) With other units, this constant will vary however, this is the constant used in this program, for these are the units used.            Read more on this constant

T = temperature

The program I have written will take input from the user for the values they DO have, and use those to find the “mystery” value, whichever variable that may be.

Screen Shot 2018-02-22 at 3.04.08 PM

//
//  main.cpp
//  PV=nRT
//
//  Created by Aidan Takami on 2/22/18.
//  Copyright © 2018 Aidan Takami. All rights reserved.
//


#include 
using namespace std;

void numbers();
double calculateP();
double calculateV();
double calculateN();
double calculateT();

//Fields
double p, v, n, t;
string pTemp = "";
string vTemp, nTemp, tTemp;
bool keepGoing = true;
const double R = 0.0821;

//Main
int main() {
    
    //Introduction & directions
    cout << "Hello! Welcome to the PV = nRT calculator! Type \"?\" for the value you are trying to find" << endl;

    
    while(keepGoing){
        //Calls calculation method
        numbers();
    }
    
}


//Method which gathers the numbers to be used in calculation
void numbers(){
    
    //Bool to represent whether mystery value has been found yet
    bool mysteryValue = false;
    
    //Bools to represent which value is mystery value
    bool pBool = false;
    bool vBool = false;
    bool nBool = false;
    bool tBool = false;
    
    //Used for continue/end process
    string temp;
    
   
    //Pressure
    cout << "What is the pressure in atm?" <> pTemp;
    
    //if the first value of the string is a digit
    if(isdigit(pTemp[0])){
        
        //convert the string to a double
        p = stod(pTemp);
    }
    else{
        
        pBool = true;
        mysteryValue = true;
    }

    
    
  
    //Volume
    cout << "What is the volume in L?" <> vTemp;
    
    //if the first value of the string is a digit
    if(isdigit(vTemp[0])){
        
        //convert the string to a double
        v = stod(vTemp);
    }
    else if(mysteryValue == true){
        
        //error message & restart
        cout << "We already have a mystery value! Let's try again!" << endl << endl;
        numbers();
    }
    else{
        
        vBool = true;
        mysteryValue = true;
    }
    
    
    
    
    
    //Moles
    cout << "What is the mole to molar mass ratio of the compound/element?" <> nTemp;
    
    //if the first value of the string is a digit
    if(isdigit(nTemp[0])){
        
        //convert the string to a double
        n = stod(nTemp);
    }
    else if(mysteryValue == true){
        
        //error message and restart
        cout << "We already have a mystery value! Let's try again!" << endl << endl;
        numbers();
    }
    else{
        
        nBool = true;
        mysteryValue = true;
    }
    
    
    
    
    
    //Temperature
    cout << "What is the temperature in Kelvin" <> tTemp;
    
    //if the first value of the string is a digit
    if(isdigit(tTemp[0])){
        
        //convert the string to a double
        t = stod(tTemp);
    }
    else if(mysteryValue == true){
        
        //error message and restart
        cout << "We already have a mystery value! Let's try again!" << endl << endl;
        numbers();
    }
    else{
        
        tBool = true;
        mysteryValue = true;
    }
    
    
    
    //if pressure = mystery value
    if(pBool){
        cout << "The Pressure is: " << calculateP() << " atm!" << endl << endl;
    }
    
    //if volume = mystery value
    else if(vBool){
        cout << "The Volume is: " << calculateV() << " L!" << endl << endl;
    }
    
    //if mole = mystery value
    else if(nBool){
        cout << "The Molar Ratio is: " << calculateN() << " mol!" << endl << endl;
    }
    
    //if temp = mystery value
    else if(tBool){
        cout << "The Temperature is: " << calculateT() << " K!" << endl << endl;
    }
    
    else{
        cout << "You answered your own question! I cannot verify whether it's true or not.!" << endl;
        cout << "Try again!" << endl << endl << endl;
    }

    
    
    //Continue?
    cout << "Would you like to continue? (y/n)" <> temp;
    
    if(temp == "no" || temp == "n"){
        keepGoing = false;
    }
    
    cout << endl;
}

//Calculates Pressure
double calculateP(){
    cout << (n * R * t) << endl;
    
    return (n*R*t)/v;
}


//Calculates Volume
double calculateV(){
    
    return (n*R*t)/p;
}


//Calculates Molar Ratio
double calculateN(){
    
    return (p*v)/(R*t);
}


//Calculates Temp
double calculateT(){
    
    return (p*v)/(R*n);
}

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s