Help with cin

Pages: 12
Bear with me here, I've never actually done programming before, and I thought that I'd just throw myself into the thick of it. I've already got one program that determines the distance between two points using cartesian coordinates, so I thought I'd make another.

I'm working on a program to calculate the pH of an acid (for practice), and in order to calculate, I have to identify strong or weak acid. When I run the program and enter "true," it ignores the first part of my "if" statement and goes right on to the second. Also, the part where I input m, H, or Ka is completely ignored.

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
#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;

int main ()
{
    bool x;
    int m;
    double Ka, result, H;
    cout << "This program calculates the pH of a certain concentration of acid." <<endl;
    cout << "Are you using a strong acid?" <<endl;
    cin >> x;
    
    if
        (x == true)
    {
        cout << "Input molar concentration of acid." <<endl;
        cin >> m;
        cout << "The pH is " << -log10(m) << "." <<endl;
    }
    else if
        (x == false)
    {
        cout << "Input molar concentration of weak acid." <<endl;
        cin >> H;
        cout << "Input equilibrium constant of weak acid.";
        cin >> Ka;
        result = -log10((sqrt(4 * H + Ka) * sqrt (Ka))/2 - (Ka / 2));
        cout << "The pH of " << x << " is " << result << "." <<endl;
    }
}
closed account (N36fSL3A)
It literally makes no sense.

I think you meant.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    if
        (x == "true")
    {
        cout << "Input molar concentration of acid." <<endl;
        cin >> m;
        cout << "The pH is " << -log10(m) << "." <<endl;
    }
    else if
        (x == "false")
    {
        cout << "Input molar concentration of weak acid." <<endl;
        cin >> H;
        cout << "Input equilibrium constant of weak acid.";
        cin >> Ka;
        result = -log10((sqrt(4 * H + Ka) * sqrt (Ka))/2 - (Ka / 2));
        cout << "The pH of " << x << " is " << result << "." <<endl;
    }


Which checks if the user entered true or false, then handles accordingly.
Last edited on by Fredbill30
closed account (jwkNwA7f)
You can't type true to make x = true.

instead try:
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
#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;

int main ()
{
    bool x;
    int m;
    double Ka, result, H;
    int choice;
    cout << "This program calculates the pH of a certain concentration of acid." <<endl;
    cout << "Are you using a strong acid?" <<endl;
    cin >> choice;

    if (choice == 1)
   {
       x = true;
   }
   else if (choice == 2)
   {
       x = false;
   }
    
    if
        (x == true)
    {
        cout << "Input molar concentration of acid." <<endl;
        cin >> m;
        cout << "The pH is " << -log10(m) << "." <<endl;
    }
    else if
        (x == false)
    {
        cout << "Input molar concentration of weak acid." <<endl;
        cin >> H;
        cout << "Input equilibrium constant of weak acid.";
        cin >> Ka;
        result = -log10((sqrt(4 * H + Ka) * sqrt (Ka))/2 - (Ka / 2));
        cout << "The pH of " << x << " is " << result << "." <<endl;
    }
}


Hope this helped!
That's just about perfect, thanks! Is there any way to change the (choice == 1) to either y/n or yes/no?
closed account (jwkNwA7f)
Yes, just give me a minute to type it up.
closed account (Dy7SLyTq)
char choice
closed account (jwkNwA7f)
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
#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;

int main ()
{
    bool x;
    int m;
    double Ka, result, H;
    char choice;
    cout << "This program calculates the pH of a certain concentration of acid." <<endl;
    cout << "Are you using a strong acid?" <<endl;
	cin >> choice;

    if (choice = 'yes')
   {
       x = true;
   }
   else if (choice = 'no' )
   {
       x = false;
   }
    
    if
        (x == true)
    {
        cout << "Input molar concentration of acid." <<endl;
        cin >> m;
        cout << "The pH is " << -log10(m) << "." <<endl;
    }
    else if
        (x == false)
    {
        cout << "Input molar concentration of weak acid." <<endl;
        cin >> H;
        cout << "Input equilibrium constant of weak acid.";
        cin >> Ka;
        result = -log10((sqrt(4 * H + Ka) * sqrt (Ka))/2 - (Ka / 2));
        cout << "The pH of " << x << " is " << result << "." <<endl;
    }
}


(Code is tested and worked)

Hope this helped!
Last edited on
I changed it to what you had, and it brought up the problem of ignoring the cin >> m and cin >> H commands again. Now it doesn't matter if I type yes or no. It goes immediately to my second "if" statement, and ignores all chances for me to input data. It just says, "pH is inf."

If I have it like this, it just doesn't let me use the weak acid part of the 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
#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;

int main ()
{
    bool x;
    int m;
    char choice;
    double Ka, result, H;
    cout << "This program calculates the pH of a certain concentration of acid." <<endl;
    cout << "Are you using a strong acid (y/n)?" <<endl;
    cin >> choice;
    
    if (choice = 'y')
    {
        x = true;
    }
    else if (choice = 'n')
    {
        x = false;
    }
    
    if
        (x == true)
    {
        cout << "Input molar concentration of acid." <<endl;
        cin >> m;
        cout << "The pH is " << -log10(m) << "." <<endl;
    }
    else if
        (x == false)
    {
        cout << "Input molar concentration of weak acid." <<endl;
        cin >> H;
        cout << "Input equilibrium constant of weak acid.";
        cin >> Ka;
        result = -log10((sqrt(4 * H + Ka) * sqrt (Ka))/2 - (Ka / 2));
        cout << "The pH of " << x << " is " << result << "." <<endl;
    }
}
Last edited on
closed account (jwkNwA7f)
Oh, sorry. I think I know how to fix it. Just give me a few seconds and I will have it fixed.
closed account (jwkNwA7f)
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
#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;

int main ()
{
    bool x;
    int m;
    double Ka, result, H;
    char choice[10];
    cout << "This program calculates the pH of a certain concentration of acid." <<endl;
    cout << "Are you using a strong acid?" <<endl;
	cin.getline(choice, 10);

    if (choice = 'yes')
   {
       x = true;
   }
   else if (choice = 'no' )
   {
       x = false;
   }
    
    if
        (x == true)
    {
        cout << "Input molar concentration of acid." <<endl;
        cin >> m;
        cout << "The pH is " << -log10(m) << "." <<endl;
    }
    else if
        (x == false)
    {
        cout << "Input molar concentration of weak acid." <<endl;
        cin >> H;
        cout << "Input equilibrium constant of weak acid.";
        cin >> Ka;
        result = -log10((sqrt(4 * H + Ka) * sqrt (Ka))/2 - (Ka / 2));
        cout << "The pH of " << x << " is " << result << "." <<endl;
    }
}


char choice[10]; means it can accept 10 characters.

I have not tested it, but it should work.

Hope this helped!
Now it says, "Array type 'char [10]' is not assignable."
closed account (jwkNwA7f)
I am sorry I keep goofing this up. I should be able to this.

I'll try it again.
Haha, no worries. You're doing a lot better job than I would be.
closed account (jwkNwA7f)
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
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <math.h>
using namespace std;

int main ()
{
    bool x;
    int m;
    double Ka, result, H;
    char choice[10];
    cout << "This program calculates the pH of a certain concentration of acid." <<endl;
    cout << "Are you using a strong acid?" <<endl;
	cin.getline(choice, 10);

	if (strcmp (choice, "yes") == 0)
	{
		x = true;
	}

	else if (strcmp (choice, "no") == 0)
	{
		x = false;
	}
    
    if
        (x == true)
    {
        cout << "Input molar concentration of acid." <<endl;
        cin >> m;
        cout << "The pH is " << -log10(m) << "." <<endl;
    }
    else if
        (x == false)
    {
        cout << "Input molar concentration of weak acid." <<endl;
        cin >> H;
        cout << "Input equilibrium constant of weak acid.";
        cin >> Ka;
        result = -log10((sqrt(4 * H + Ka) * sqrt (Ka))/2 - (Ka / 2));
        cout << "The pH of " << x << " is " << result << "." <<endl;
    }
}


To learn how to use cstring you can go hear:
http://www.cplusplus.com/reference/cstring/

I should have done this in the first place. I am sorry I keep goofing up. I have done stuff like this many times.

Hope this helped!
I think we're close. Now when I type "yes" and hit enter, nothing happens.
closed account (jwkNwA7f)
I am not sure what's wrong. Give me a minute to look through it.
closed account (jwkNwA7f)
Try it again. The first time I ran the code my result was the same as yours, but then I just ran it again and it said
Input molar concentration of acid.
and I entered a number and it came up with something (I don't know anything about ph of acid or something like that.)
Last edited on
closed account (N36fSL3A)
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
#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;

int main ()
{
    bool x;
    int m;
    char choice;
    double Ka, result, H;
    cout << "This program calculates the pH of a certain concentration of acid." <<endl;
    cout << "Are you using a strong acid (y/n)?" <<endl;
    cin >> choice;
    
    if (choice == 'y') // < You had a single equal sign, it just set the variable
                                        // To true.
    {
        x = true;
    }
    else if (choice == 'n') // < Same here.
    {
        x = false;
    }
    
    if
        (x == true)
    {
        cout << "Input molar concentration of acid." <<endl;
        cin >> m;
        cout << "The pH is " << -log10(m) << "." <<endl;
    }
    else if
        (x == false)
    {
        cout << "Input molar concentration of weak acid." <<endl;
        cin >> H;
        cout << "Input equilibrium constant of weak acid.";
        cin >> Ka;
        result = -log10((sqrt(4 * H + Ka) * sqrt (Ka))/2 - (Ka / 2));
        cout << "The pH of " << x << " is " << result << "." <<endl;
    }
}


Didn't test it, but that's one thing you had wrong cppprogrammer.
Last edited on by Fredbill30
closed account (jwkNwA7f)
Oh, okay. Thank you FredBill30!
I've run it a couple of times, and I keep getting the same result. A good test sample would be to input "yes" and "1." You should get 0 as a result.

Edit: Sorry, I didn't see your post Fred.
Last edited on
Pages: 12