I'm not sure what I'm doing wrong.

I've been working on a program for a little while now, it's a program to help with the ideal gas laws in chemistry. (I understand the equation inside and out, I'm making this to practice programming, and maybe help get rid of the repetitive work in that class. Just clarifying, that I'm not attempting to use this to cheat in any way.)
Anyway, the program compiles without any problems, but when I run it, it seems to skip entire if statements and I'm not sure what I'm doing wrong. So, I was wondering if someone with a little more experience could take a glance at the code and see where I went wrong.

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
//PV=nRT
using namespace std;

float celtokel(float cels)
{
      return (cels + 273.15); //Celsius to Kelvin
}

float fartokel(float far)
{
      float cel = (far-32)/(9/5);
      float kel = celtokel(cel); //Farenheit to Kelvin
      return kel;
}

float leftsolve(float a, float b, float c, float R)
{
      return ( (a*b*R)/c ); //Solve for a variable on the left of the equation.
}

float rightsolve(float a, float b, float c, float R)
{
      return( (b*c)/(R*a) ); //Solve for a variable on the right side.
}

int main(int argc, char* argv[])
{
    float temp, pressure, volume, moles, R;
    float Rp[3] = {.08205746, 62.36367, 8.314472}; //Define R constant.
    int control;
    control = 0;
    if(argc != 4)
    {
            cout<<"You need to enter: Variable to be solved for (IE: /p, /v, /n, /t)\nThe temp units (IE /f /c /k)\nAnd the pressure units (IE /atm, /torr, /kPa)";
            return 1;
    }
    else
    {
        if(control == 0) //set R constant depending on user input.
        {
            if (argv[3] == "/atm") { R = Rp[0];}
            else if(argv[3] == "/torr") { R = Rp[1]; }
            else if(argv[3] == "/kPa") { R = Rp[2]; }
            control++;
        }
        else if(control == 1) //convert all temp to Kelvin, based on user input.
        {
             if(argv[1] != "/t")
             {
             cout<<"Enter temperature: ";
             cin>>temp;
             if(argv[2] == "/f") { temp = fartokel(temp); }
             else if(argv[2] == "/c") { temp = celtokel(temp); }
             }
             control++;
        }
        else if(control == 2)//solve equations
        {
                 if(argv[1] == "/p")
                 {
                   cout<<"\nEnter Volume: ";
                   cin>>volume;
                   cout<<"\nEnter number of moles: ";
                   cin>>moles;
                   cout<<"\nPressure equals: " << leftsolve(temp, moles, volume, R);
                 }
                 else if(argv[1] == "/v")
                 {
                 cout<<"\nEnter Pressure: ";
                 cin>>pressure;
                 cout<<"\nEnter number of moles: ";
                 cin>>moles;
                 cout<<"\nVolume equals: "<< leftsolve(temp, moles, pressure, R);
                 }
                 else if(argv[1] == "/n")
                 {
                 cout<<"\nEnter Pressure: ";
                 cin>>pressure;
                 cout<<"\nEnter Volume: ";
                 cin>>volume;
                 cout<<"\nNumber of moles equals: "<< rightsolve(pressure, volume, temp, R);
                 }
                 else if(argv[1] == "/t")
                 {
                 cout<<"\nEnter Volume: ";
                 cin>>volume;
                 cout<<"\nEnter Pressure: ";
                 cin>>pressure;
                 cout<<"\nEnter number of moles: ";
                 cin>>moles;
                 cout<<"\nTemperature equals: " << rightsolve(pressure, volume, moles, R);
                 }
        }
    }
    return 0;
}



Also, a side note, I'm not sure how well formatting is preserved as I've never posted in the forum before, so please excuse it if it looks kinda sucky.
I tried converting all the instances of argv[whatever] == "whatever" to the cmpstr(argv[whatever], "whatever") and I haven't noticed any improvement.

What happens is, the program will accept the correct number of arguments, go through the first set of If statements, then terminate.(I tried adding a couple cout<<'s at points the program was supposed to cross, and it never output them.)

If it helps any, this is the updated code:
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <string.h>
//PV=nRT
using namespace std;

float celtokel(float cels)
{
      return (cels + 273.15); //Celsius to Kelvin
}

float fartokel(float far)
{
      float cel = (far-32)/(9/5);
      float kel = celtokel(cel); //Farenheit to Kelvin
      return kel;
}

float leftsolve(float a, float b, float c, float R)
{
      return ( (a*b*R)/c ); //Solve for a variable on the left of the equation.
}

float rightsolve(float a, float b, float c, float R)
{
      return( (b*c)/(R*a) ); //Solve for a variable on the right side.
}

int main(int argc, char* argv[])
{
    float temp, pressure, volume, moles, R;
    float Rp[3] = {.08205746, 62.36367, 8.314472}; //Define R constant.
    int control;
    control = 0;
    if(argc != 4)
    {
            cout<<"You need to enter: Variable to be solved for (IE: /p, /v, /n, /t)\nThe temp units (IE /f /c /k)\nAnd the pressure units (IE /atm, /torr, /kPa)";
            return 1;
    }
    else
    {
        if(control == 0) //set R constant depending on user input.
        {
            if (strcmp(argv[3],"/atm")) { R = Rp[0];}
            else if(strcmp(argv[3],"/torr")) { R = Rp[1]; }
            else if(strcmp(argv[3],"/kPa")) { R = Rp[2]; }
            control++;
        }
        else if(control == 1) //convert all temp to Kelvin, based on user input.
        {
             if(!strcmp(argv[1],"/t"))
             {
             cout<<"Enter temperature: ";
             cin>>temp;
             if(argv[2] == "/f") { temp = fartokel(temp); }
             else if(argv[2] == "/c") { temp = celtokel(temp); }
             }
             control++;
        }
        else if(control == 2)//solve equations
        {
                 if(strcmp(argv[1],"/p"))
                 {
                   cout<<"\nEnter Volume: ";
                   cin>>volume;
                   cout<<"\nEnter number of moles: ";
                   cin>>moles;
                   cout<<"\nPressure equals: " << leftsolve(temp, moles, volume, R);
                 }
                 else if(strcmp(argv[1],"/v"))
                 {
                 cout<<"\nEnter Pressure: ";
                 cin>>pressure;
                 cout<<"\nEnter number of moles: ";
                 cin>>moles;
                 cout<<"\nVolume equals: "<< leftsolve(temp, moles, pressure, R);
                 }
                 else if(strcmp(argv[1],"/n"))
                 {
                 cout<<"\nEnter Pressure: ";
                 cin>>pressure;
                 cout<<"\nEnter Volume: ";
                 cin>>volume;
                 cout<<"\nNumber of moles equals: "<< rightsolve(pressure, volume, temp, R);
                 }
                 else if(strcmp(argv[1],"/t"))
                 {
                 cout<<"\nEnter Volume: ";
                 cin>>volume;
                 cout<<"\nEnter Pressure: ";
                 cin>>pressure;
                 cout<<"\nEnter number of moles: ";
                 cin>>moles;
                 cout<<"\nTemperature equals: " << rightsolve(pressure, volume, moles, R);
                 }
        }
    }
    return 0;
}


Is there a possibility I'm using too many ifs in a way that doesn't work? Or something like that?
Your comparisons are wrong.
If you had carefully read the link I provided, you'd had seen that (being a and b, strings):
If !strcmp(a,b), then a==b
If strcmp(a,b)<0, then a<b
If strcmp(a,b)>0, them a>b
Last edited on
Yeah, I'd just skimmed it to see the syntax.
I realized that just before you posted. Kind of a "Boy is my face red" moment.
I'd made the assumption that the function would return 1 if the strings were the same, or 0 if they weren't.
Since none were right, that would lead to immediate termination.

Thanks a ton for your help. I would never have realized that on my own.
Topic archived. No new replies allowed.