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.
#include <iostream>
#include <stdio.h>
#include <math.h>
usingnamespace 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;
}
elseif
(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;
}
}
#include <iostream>
#include <stdio.h>
#include <math.h>
usingnamespace 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;
}
elseif (choice == 2)
{
x = false;
}
if
(x == true)
{
cout << "Input molar concentration of acid." <<endl;
cin >> m;
cout << "The pH is " << -log10(m) << "." <<endl;
}
elseif
(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;
}
}
#include <iostream>
#include <stdio.h>
#include <math.h>
usingnamespace 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;
}
elseif (choice = 'no' )
{
x = false;
}
if
(x == true)
{
cout << "Input molar concentration of acid." <<endl;
cin >> m;
cout << "The pH is " << -log10(m) << "." <<endl;
}
elseif
(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;
}
}
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.
#include <iostream>
#include <stdio.h>
#include <math.h>
usingnamespace 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;
}
elseif (choice = 'n')
{
x = false;
}
if
(x == true)
{
cout << "Input molar concentration of acid." <<endl;
cin >> m;
cout << "The pH is " << -log10(m) << "." <<endl;
}
elseif
(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;
}
}
#include <iostream>
#include <stdio.h>
#include <math.h>
usingnamespace 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;
}
elseif (choice = 'no' )
{
x = false;
}
if
(x == true)
{
cout << "Input molar concentration of acid." <<endl;
cin >> m;
cout << "The pH is " << -log10(m) << "." <<endl;
}
elseif
(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.
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <math.h>
usingnamespace 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;
}
elseif (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;
}
elseif
(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.
#include <iostream>
#include <stdio.h>
#include <math.h>
usingnamespace 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;
}
elseif (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;
}
elseif
(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.