I'm trying to make a C++ Acidity/Base Universal Calculator. Upon trying to finalize my code, I stump upon 5 error(s) below;
main.cpp: In function 'int main()':
main.cpp:62:36: error: invalid operands of types 'double' and 'const char [5]' to binary 'operator<<'
cout << "[H+]=" << 10^(W-X) << "*10^" << W << '\n';
^
main.cpp:63:34: error: invalid operands of types 'int' and 'double' to binary 'operator^'
cout << "[OH-]=" << 1/(10^(W-X)) << "*10^" << (-14)-W << '\n';
^
main.cpp:77:33: error: invalid operands of types 'int' and 'double' to binary 'operator^'
cout << "[H+]=" << 1/(10^(U-V)) << "*10^" << (-14)-U << '\n';
^
main.cpp:78:37: error: invalid operands of types 'double' and 'const char [5]' to binary 'operator<<'
cout << "[OH-]=" << 10^(U-V) << "*10^" << U << '\n';
^
D:\EvaxHybrid\Mywork\Cpp\ChempHpOH\Makefile.win:28: recipe for target 'main.o' failed
mingw32-make.exe: *** [main.o] Error 1
I've tried implementing Solution 1 which isn't inline and would make the code too complex to read , Solution 2 which isn't inline and not the same problem (I didn't use new). If there's no other choice, anyone could comment to me about that and I'll do a function related approach.
Here's the code (main.cpp);
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <string>
#include <bits/stdc++.h>
#include <stdlib.h>
#include <conio.h>
#include <cmath>
using namespace std;
int main() {
cout << "Choose Start point..." << '\n';
cout << "1. [H+]\n2. [OH-]\n3. pH\n4. pOH\n";
int choice;
cin >> choice;
system ("cls");
switch(choice)
{
case 1:
cout << "Convert [H+] to Scientific Notation of A*10^B and input A,B\n";
system ("pause");
cout << '\n';
double A,B;
cout << "A:";
cin >> A;
cout << '\n';
cout << "B:";
cin >> B;
cout << '\n';
cout << "[H+]=" << A << "*10^" << B << '\n';
cout << "[OH-]=" << 1/A << "*10^" << (-14)-B << '\n';
cout << "[pH]=" << (-log10(A)-B) << '\n';
cout << "[pOH]=" << 14-(-log10(A)-B) << '\n';
break;
case 2:
cout << "Convert [OH-] to Scientific Notation of Z*10^Y and input Z,Y\n";
system ("pause");
cout << '\n';
double Z,Y;
cout << "Z:";
cin >> Z;
cout << '\n';
cout << "Y:";
cin >> Y;
cout << '\n';
cout << "[H+]=" << 1/Z << "*10^" << (-14)-Y << '\n';
cout << "[OH-]=" << Z << "*10^" << Y << '\n';
cout << "[pH]=" << 14-(-log10(Z)-Y) << '\n';
cout << "[pOH]=" << (-log10(Z)-Y) << '\n';
break;
case 3:
cout << "Input pH as X\n";
system ("pause");
cout << '\n';
double X;
cout << "X:";
cin >> X;
double W;
W = -ceil(X);
cout << '\n';
cout << "[H+]=" << 10^(W-X) << "*10^" << W << '\n';
cout << "[OH-]=" << 1/(10^(W-X)) << "*10^" << (-14)-W << '\n';
cout << "[pH]=" << X << '\n';
cout << "[pOH]=" << 14-X << '\n';
break;
case 4:
cout << "Input pOH as V\n";
system ("pause");
cout << '\n';
double V;
cout << "V:";
cin >> V;
double U;
U = -ceil(V);
cout << '\n';
cout << "[H+]=" << 1/(10^(U-V)) << "*10^" << (-14)-U << '\n';
cout << "[OH-]=" << 10^(U-V) << "*10^" << U << '\n';
cout << "[pH]=" << 14-V << '\n';
cout << "[pOH]=" << V << '\n';
break;
}
}
Operator <<
has precedence over operator ^
.
cout << "[H+]=" << 10^(W-X) << "*10^" << W << '\n';
is read as
(cout << "[H+]=" << 10) ^ ((W-X) << "*10^" << W << '\n');
Put parenthesis:
cout << "[H+]=" << (10^(W-X)) << "*10^" << W << '\n';