I'm making a simple operator calculator, it currently works well with one exception. I can't figure out how to test user input in my num1 and num2 variables. Evaluation of my operator works and the code seems to loop properly if its incorrect or after the calculation is made.
I've tried using an if statement containing isalnum, its quite possible I'm using it incorrectly. My thought was to place the if statement nested inside the op evaluation before the switch
int main()
{
char op;
float num1, num2;
cout << "Welcome To my first Attempt back at programming in C++!" << endl << endl;
cout << "Enter an operator + or - or * or /: " << endl; // Prints instructions
cin >> op; // Selected operation
while ( op )
{
if (op == '+' || op == '-' || op == '*' || op == '/')
{
cout << "Enter two Numbers please!!: " << endl; // Prints Intrusctions
cin >> num1 >> num2; // Slected numbers
// if(num1 is a number && num2 is a number)
switch(op)
{
case '+':
cout << num1+num2 << endl << "Enter another Operator to start again" << endl << endl;
cin >> op;
break;
case '-':
cout << num1-num2 << endl << "Enter another Operator to start again" << endl << endl;
cin >> op;
break;
case '*':
cout << num1*num2 << endl << "Enter another Operator to start again" << endl << endl;
cin >> op;
break;
case '/':
cout << num1/num2 << endl << "Enter another Operator to start again" << endl << endl;
cin >> op;
break;
}
// else (Error Message)
}
else
{
cout << "No! " << op << " is not an operator!" << endl << "Please enter a vaild operator" << endl << endl;
cin >> op;
}
}
return 0;
}
Id like it to test that num1 and num2 are indeed numbers. Then have the user try again if they're not, then preform the calculation if they are indeed numbers
Aucun commentaire:
Enregistrer un commentaire