I made a basic calculator. Everything works as intended, the only problem being is that when num1 or num2 are inputted as a string/letter, everything just breaks and causes an endless loop. Is there a way to detect if an integer/double is inputted as a string? If there is, maybe I can make this calculator a little bit better.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double num1;
double num2;
string op;
string rep;
bool again1 = true;
bool again = true;
while(again == true)
{
cout << "Enter first number: ";
cin >> num1;
cout << "Operation: ";
cin >> op;
cout << "Enter second number: ";
cin >> num2;
again = false;
again1 = true;
if(op == "+" || op == "add")
{
cout << num1 << "+" << num2 << " = " << num1+num2 << endl;
} else if(op == "-"){
cout << num1 << "-" << num2 << " = " << num1-num2 << endl;
} else if(op == "*" || op == "x"){
cout << num1 << "x" << num2 << " = " << num1*num2 << endl;
} else if(op == "/"){
if(num2 == 0){
cout << "(SYNTAX ERROR)";
} else {
cout << num1 << "/" << num2 << " = " << num1/num2 << endl;
}
}
cout << endl;
while(again1 == true){
again1 = false;
cout << "Would you like to try again? Y/N: ";
cin >> rep;
if(rep == "Y" || rep == "y")
{
again = true;
cout << "Very well!" << endl;
cout << endl;
}
else if(rep == "N" || rep == "n")
{
again = false;
cout << "Ok, see you later!" << endl;
} else {
cout << "Invalid Answer" << endl;
again1 = true;
}
}
}
return 0;
}