//Calculator Revision to say error when invalid character
#include <iostream>
using namespace std;
int main()
{
cout <<"Welcome to the 1.1 calculator by Niklas Kunkel";
cout << "\nPlease enter 2 numbers and press enter after each number";
float num1;
float num2;
char op;
float result;
char again = 'y';
while (again == 'y')
{
cout <<"\n Please enter the first number: ";
cin >> num1;
cout <<"\n num 1 = " << num1 << endl;
if (num1//This is where im stuck I am trying to have it say "if num1 is a # do the following code but skip the else part
{
cin >> num2;
cout << "num2 = " << num2 << endl;
if (num2 //Im stuck here as well with the same problem as above. If num2 is a # skip else and do the rest of the code
{ cout <<"\n Now enter an operation symbol (+ - / *)";
cin >> op;
if (op == '+')
cout << "\n num1 + num2 = " << num1 + num2 << endl;
if (op == '-')
cout <<"\n num1 - num2 = " << num1 - num2 << endl;
if (op == '*')
cout <<"\n num1 * num2 = " << num1 * num2 << endl;
if (op == '/')
cout << " \n num1 / num2 = " << num1 / num2 << endl;
else
{cout << "Invalid Operation";
cout << "\n Do you want to try again? (y/n): ";
cin >> again;
}
else
{
cout <<"\n Error: Item Entered is not a number";
cout <<"\n Do you want to try again? (y/n): ";
cin >> again;
}
}
else
{
cout<< "Error: Item entered is not a number";
cout << "\n Do you want to try again? (y/n): ";
cin >> again;
}
return 0;
}
I know its really hard to read. I had to nest a lot of if statements together and I know there must be an easier way to make it look less cluttered. I'm having a problem where I am trying to tell the program "if varaible num1 is a # do the following code if it isnt a number go to else which will say "invalid character".
Other than that I think I might quite a few mistakes with the nesting to. Ive heard of something called switch blocks but I have no idea how to use them.