How to skip a varibles

I am just a beginner in c++
I am making a program on a physics formula
PV=nRT(this is formula of ideal gas equation)
i have build the program and it run excellently but i want to improve this

As you can see i am using a condition p==0 because i want to find 'p' but the problem is every time i run the program i have to input p=0 in the screen but i want the program to skip "p" ( take automatically "p" as 0 when i press enter and got to another varibles)

Thank in advance.

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
#include <iostream>
#include <cmath>
using namespace std;


int main()
{
    double p;
    double v;
    double n;
    double r = 8.32;
    double t;
    double answer;

   

    cout << "enter pressure \n";
    cin >> p;
    cout << "enter volume \n";
    cin >> v;
    cout << "enter number of molecules \n";
    cin >> n;
    cout << "enter temperature \n";
    cin >> t;

   
     if(p == 0){
    answer = (n*r*(t+273))/v;
    cout << "the pressure is " << answer << " Pa" << endl;
     }
     if(v == 0){
    answer = (n*r*(t+273))/p;
    cout << "the volume is " << answer << " m^3" << endl;
     }
     if(n == 0){
    answer = (p*v)/(r*(t+273));
    cout << "the number of molecules is " << answer << " moles" << endl;
     }
     if(t == 0){
    answer = ((p*v)/(n*r))+273;
    cout << "the temperature is " << answer << " K" << endl;
     }


    return 0;

}
Last edited on
I am just a beginner in c++

Excellent. Hopefully it is self-evident why this belongs in the Beginners forum. Please post similar queries there in the future.
Use switch statements
Not too sure what you're asking, but you haven't declared the variables as anything.

double p = 0;

The default isn't 0 if you don't declare a value, it the lowest number I believe, so for a double it would be: -9.2559631349317831e+061.

Here is a link that tells you the data ranges for variables, worth looking over it.
http://msdn.microsoft.com/en-us/library/s3f49ktz%28v=vs.80%29.aspx
i am declaring the variable by inputting the variable through the cmd
cin >> p;
my problem is i don't want the program to take 0 as the variable instead i want the program to take the "enter" key as the condition (or command) and skip to catch another variables
that means if i leave "p" as blank or empty it will go to catch the variable "v"
and give me the answer by this condition
1
2
3
4
{
    answer = (n*r*(t+273))/v;
    cout << "the pressure is " << answer << " Pa" << endl;
     }

@greenleaf800073 (372) thanks for your help switch statement really helps me a lot
Last edited on
I'm not sure how you can have it just skip the input by hitting enter.
Perhaps you could prompt for which variable to solve for (p in this case)? Then prompt for the other variables only.
The default isn't 0 if you don't declare a value, it the lowest number I believe, so for a double it would be: -9.2559631349317831e+061.

Incorrect, and also irrelevant. Variables which are uninitialized and have automatic storage duration can have any initial value.

One way:
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
#include <iostream>
#include <string>
#include <limits>
#include <cmath>
using namespace std;

double get_num(const std::string& prompt)
{
    std::cout << prompt << "\n> " ;
    if ( std::cin.peek() == '\n' )
    {
        std::cin.ignore() ;
        return 0.0 ;
    }

    double num ;
    std::cin >> num ;
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n') ;
    return num ;
}


int main()
{
    double p = get_num("enter pressure") ;
    double v = get_num("enter volume") ;
    double n = get_num("enter number of molecules") ;
    double t = get_num("enter temperature") ;

    double r = 8.32;
    double answer;

    if(p == 0)
    {
        answer = (n*r*(t+273))/v;
        cout << "the pressure is " << answer << " Pa" << endl;
    }
    if(v == 0)
    {
        answer = (n*r*(t+273))/p;
        cout << "the volume is " << answer << " m^3" << endl;
    }
    if(n == 0)
    {
        answer = (p*v)/(r*(t+273));
        cout << "the number of molecules is " << answer << " moles" << endl;
    }
    if(t == 0)
    {
        answer = ((p*v)/(n*r))+273;
        cout << "the temperature is " << answer << " K" << endl;
    }

    return 0;
}



Last edited on
line 17 have an error
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n') ;

(doesn't run with code block)
Last edited on
Originally, I had left out #include <limits> as the compiler I tested with was apparently including it in another file which I was including, although I corrected it a minute after posting. Make sure you have that line at the top.
Last edited on
thankyou very much cire
as you understand i am just a beginner can you please describe what did you do between this code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
double get_num(const std::string& prompt)
{
    std::cout << prompt << "\n> " ;
    if ( std::cin.peek() == '\n' )
    {
        std::cin.ignore() ;
        return 0.0 ;
    }

    double num ;
    std::cin >> num ;
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n') ;
    return num ;
}

or can you please tell me what should i have to know to understand those code
I'm going to assume that you understand what a function is.

So, line 3 simply outputs the prompt that is fed to the function. If the string fed to the function is "enter pressure" after line 3 has executed the console looks like this:

enter pressure
> _


where the underscore represents the cursor.

On line 4 we check to see if the next character that would be extracted from std::cin is a newline character. If it is, on line 6 we exttract and discard it, then on line 7 we return a value of 0. That takes care of interpreting a 'blank' line as 0.

I believe you understand what lines 10 and 11 do, but you may not be familiar enough with the details to realize what is not extracted in line 11. It may help to visualize the input stream as a string. If a person enters 5.4 and presses enter, the input stream looks like: "5.4\n".

cin >> num will extract the 5.4 from the stream leaving "\n" in the stream.

If we just returned num at this point, you can see there would still be a newline in the input stream. On the next call to get_num we would encounter this newline on line 4 of get_num and interpret it as a blank line, robbing the user of the chance to enter any value. To avoid this we extract and discard the newline on line 12, so that we are assured of having an empty input stream for the next input operation.

Now, you might be wondering... why not just do what you did on line 6? And it is a good question. That would work for most uses of the program however if a user were to input, instead of "5.4\n", "5.4 \n" after line 11 the input stream would look like " \n" and if we were only to extract and discard one character, you can see there would still be a newline in the stream ready to gunk up the works.

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n') ; is the canonical way to say "discard the rest of the current line." The first argument is the maximum number of characters to discard, and the last argument is the character which we should discard values up to (if you don't specify one it will consume all characters up to the end of file marker.)

std::numeric_limits<std::streamsize>::max() returns the maximum value a std::streamsize type can hold. As we're dealing with a stream, I think the reason we just std::streamsize is fairly obvious.

If you need further explanation, ask away.

[edit: typo]
Last edited on
Nice solution cire. I figured it could be done. I just haven't ever found a need to process input like that.
Topic archived. No new replies allowed.