Alright, so what I want to do is make a program where it only stops if I hit a certain letter - in this case, 'e'.
I have everything figured out, except after adding the loop whenever I press 'e', it just continues to ask me the first line over and over.
I tried using break; before the input for 'e', but then the loop wouldn't work at all. Here's the code:
#include <iostream>
usingnamespace std;
int main()
{
char input;
double num1, num2, result;
for(;;)
{
cout << "Hello, please give me an operation (+, -, *, /) to perform, or press 'e' to escape: " << endl;
cin >> input;
if (input == '+')
{
cout << "You selected +! Please enter the two operands: ";
cin >> num1 >> num2;
result = num1 + num2;
cout << "The result is: " << result << endl;
}
if (input == '-')
{
cout << "You selected -! Please enter the two operands: ";
cin >> num1 >> num2;
result = num1 - num2;
cout << "The result is: " << result << endl;
}
if (input == '*')
{
cout << "You selected *! Please enter the two operands: ";
cin >> num1 >> num2;
result = num1 * num2;
cout << "The result is: " << result << endl;
}
if (input == '/')
{
cout << "You selected /! Please enter the two operands: ";
cin >> num1 >> num2;
result = num1 / num2;
cout << "The result is: " << result << endl;
}
break;
}
if (input == 'e')
{
cout << "Goodbye!" << endl;
}
return 0;
}
@LowestOne: I tried putting the break where you said to, but then the program would loop at the point after I selected what kind of operation I want to do.
And you input num1 and num2 after you select which kind of operation you want to do.
The way it is written the user inputs data, and if that data doesn't match +, -, *, /, it leaves the loop. It then checks to see if input is equal to 'e' then terminates the program.
Look at where the braces are located and notice that the loop control structure terminates after the break; statement. The test to see if input == 'e' never occurs within the loop itself, only after it. try moving the brace that is directly below the break; command beneath the if{...} section that checks for input == 'e', and placing that break; statement within that decision structure that tests for the same.
@progrady: I tried that, but then that makes the loop not happen at all. If I input 'e', it'll end the program. And if I input + and the two numbers, it won't ask me to pick what sort of operation I want to try again, it'll just stop.
Edit: I was placing the break; in the wrong spot. Thank you so much!