-
Need help with this loop
Code:
#include <iostream>
using namespace std;
int main()
{
while (true)
{
char op;
float num1, num2;
char ex;
cout << "Select + for addition" << endl; // Give the user the option for addition
cout << "Select - for subtraction" << endl; // Give the user the option for subtraction
cout << "Select * for multiplication" << endl; // Give the user the option for multiplication
cout << "Select / for division" << endl; // Give the user the option for division
cout << "Select x to exit" << endl; // Give the user the option to exit the program
cin >> op; // User selects from the given operations
if (op == 'x')
{
cout << "Are you sure you would like to exit?" << endl;
cout << "Enter y to exit" << endl;
cout << "Enter n to stay" << endl;
cin >> ex;
}
cout << "Enter number one "; // User enters first number which they want to calculate
cin >> num1;
cout << "Enter number two "; // User enters second number which they want to calculate
cin >> num2;
if (op == '+')
{
cout << "Sum of operation = " << num1 + num2 << endl;
}
else if (op == '-')
{
cout << "Sum of operation = " << num1 - num2 << endl;
}
else if (op == '*')
{
cout << "Sum of operation = " << num1 * num2 << endl;
}
else if (op == '/')
{
cout << "Sum of operation = " << num1 / num2 << endl;
}
}
system("pause");
return 0;
}
when i ask theuser if they with to exit and they select "x" i am then asking another question but im not sure where to go from there. if they select "y" i want them to exit the program however if they select "n" i want them to go back to the menu were they select their operation again and im not sure how to do this??:confused::confused::confused:
-
Without having put much thought into this, here is a quick solution:
Code:
if (op == 'x')
{
cout << "Are you sure you would like to exit?" << endl;
cout << "Enter y to exit" << endl;
cout << "Enter n to stay" << endl;
cin >> ex;
if (ex == 'y') {
break; // leaves the parent while-loop
} else {
continue; // skips remaining loop into next iteration
}
}
-
thank you i didn't realise i could have an if inside another if. would you know how i could include a thing were if the user tries to divide by zero a error would come up saying "cannot divide by zero" and would send the user back to the main menu?
-
You could try that yourself as an exercise! Place an extra check inside the else-if section for division. If the user tries to divide by 0, you could then print a message and go to the next iteration of the loop as in my earlier example.
-
Code:
else if (op == '/')
{
if (num2 == '0')
{
cout << "Cannot divide by zero" << endl;
}
else
{
cout << "Sum of operation = " << num1 / num2 << endl;
}
}
else // If op choice is not valid
{
cout << "Not a valid choice" << endl; // Tell user thier choice is not valid
continue; // Continues with the while loop
}
this is what i managed to do however when i try to divide by 0 the message doesnt show up and im not sure why
-
Code:
if (num2 == 0) { // not '0'
However, comparing a float to 0 isn't quite as simple as that either, so I would recommend you declare num2 as an integer (num1 can stay float). It's complicated ;)