I have been learning C++ for a couple of days and have recently attempted to create a simple calculator program. All seamed to be working fine however I found that after the first calculation, the first digit on any sum would disappear (If it was a two digit number, if not it would not work at all.). Any help or information on why this is happening would be greatly appreciated.
Code...
#include <iostream>
using namespace std;
int main()
{
float x, z;
string y;
int i;
cout << "Please enter a number, a symbol and then another number (seperated by spaces)\ne.g. 3 * 6\n";
while (true)
{
float x=0, z=0;
string y="";
cin >> x >> y >> z;
cin.ignore();
if (y == "*"){
cout << x << "*" << z << "=" << x*z << "\n";
}
else if (y == "/"){
cout << x << "/" << z << "=" << x/z << "\n";
}
else if (y == "+"){
cout << x << "+" << z << "=" << x+z << "\n";
}
else if (y == "-"){
cout << x << "-" << z << "=" << x-z << "\n";
}
cin.ignore();
}
}
Also, as a small matter of style, the mathematical operator is only one character, so you could use datatype char for it.
It saves you from using an extra library (string) and i believe it would take up less memory as well, which on this small scale is of course not as much of an issue.