Thread: Chemistry Problem In C++ Script Not WORKING

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    5

    Chemistry Problem In C++ Script Not WORKING

    O.K. I'm very very new to C++ but I am trying to make a programm that can do PV=nRT and for whatever you put 0 in for it solves the equation out. But when I was trying to test if I could get even one of the variables solved I came into a problem, it's not giving me the right answer XD.
    Here is my script
    Code:
    #include <iostream>
    
    using namespace std;
    
    
    int main()
    {
        int P;
        float V;
        float r;
        float n;
        float temp;
        float answer;
        
        cout<<"Please enter value for Pressure in kPa \n";
        cin>> P;
        cin.ignore ();
        cout<<"Please enter value for Volume in L \n";
        cin>> V;
        cin.ignore ();
        cout<<"Please enter value for n in moles \n";
        cin>> n;
        cin.ignore ();
        cout<<"Please enter value for Temperature in Celsius \n";
        cin>> temp;
        cin.ignore ();
        r = 8.31; 
    
    if ( P = 0 ) { answer = n * r * temp / V; }
    cout<<answer; 
    cin.ignore();
    cin.get ();
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You don't think that could be caused by the temperature beign in Celsius rather than Kelvin?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    5
    well actually no i havent made it to add the 273 to the temp yet because i was trying just to get this small part of it to work

  4. #4
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Code:
    if ( P = 0 ) { answer = n * r * temp / V; }
    Use the "==" operator instead of the "=" operator. That way, the calculation will perform (since "P = 0" evaluates to 0, you never actually do the calculation).

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    5
    Thank you so much foxman. That worked WOOT. You rock man.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Replies: 0
    Last Post: 07-26-2007, 09:55 AM
  3. Passing arguments to script.....
    By suwie in forum C Programming
    Replies: 5
    Last Post: 09-25-2004, 11:10 PM
  4. Game structure, any thoughts?
    By Vorok in forum Game Programming
    Replies: 2
    Last Post: 06-07-2003, 01:47 PM
  5. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 08:34 PM