I would like to make my do-while loop stop using a char. is that possible or should i use just a while loop? What is the problem in my code, it runs but when i press Q it turns into a continuous loop?
p.s. there's a longer program to this code i just didnt add it
1 2 3 4 5 6 7
do {
cout << "Enter the Operator: ";
cin >> operators;
cout << "Would you like to stop? Enter Q to stop" << endl << endl;
} while (operators != 'Q');
int main()
{
int operand1, operand2;
double result = 0, number = 0;
char operators;
bool Q = false;
menu();
do {
cout << "Enter the Operator: ";
cout << endl << "Enter Q as the operator to stop"
<< endl << endl;
cin >> operators;
if (operators != 'R' || operators != 'F' || operators != 'T') {
cout << "First Number: ";
cin >> operand1;
cout << "Second Number: ";
cin >> operand2;
result = calculate(operand1, operand2, operators);
cout << operand1 << " " << operators << " " << operand2
<< " = " << result << endl << endl;
}
elseif (operators == 'R') {
cout << "What number would you like to find the reciprocal of: ";
cin >> number;
result = reciprocal(number);
cout << "Reciprocal of " << number << " is " << result << endl;
}
elseif (operators == 'F') {
cout << "What number would you like to find the factorial of: ";
cin >> number;
result = factorial(number);
cout << "Factorial of " << number << " is " << result << endl;
}
elseif (operators == 'T') {
cout << "What number would you like to square root: ";
cin >> number;
result = sqrt(number);
cout << "Square Root of " << number << " is " << result << endl;
}
elseif (operators != 'Q') //now if will not output this if they enter 'Q'
cout << "Error. You have not typed in a correct operator." << endl;
} while (operators != 'Q'); //This does not work
system("PAUSE");
return 0;
}
I can't test this because I don't have your function definitions, but it worked when I commented out the stuff in-between line 13 and 47 (and also line 8)
It will only check whether operators is equal to Q or not at the end of the program, that's why when you enter 'Q' operator at the beginning, you will have to go through the entire program before it stops.
#include <iostream>
usingnamespace std;
int main()
{
int operand1, operand2;
double result = 0, number = 0;
char operators;
bool Q = false;
menu();
do {
cout << "Enter the Operator: ";
cin >> operators;
if(operators == 'Q' || operators == 'q'){
goto END;
}
if (operators != 'R' || operators != 'F' || operators != 'T') {
cout << "First Number: ";
cin >> operand1;
cout << "Second Number: ";
cin >> operand2;
result = calculate(operand1, operand2, operators);
cout << operand1 << " " << operators << " " << operand2
<< " = " << result << endl << endl;
}
elseif (operators == 'R') {
cout << "What number would you like to find the reciprocal of: ";
cin >> number;
result = reciprocal(number);
cout << "Reciprocal of " << number << " is " << result << endl;
}
elseif (operators == 'F') {
cout << "What number would you like to find the factorial of: ";
cin >> number;
result = factorial(number);
cout << "Factorial of " << number << " is " << result << endl;
}
elseif (operators == 'T') {
cout << "What number would you like to square root: ";
cin >> number;
result = sqrt(number);
cout << "Square Root of " << number << " is " << result << endl;
}
else
cout << "Error. You have not typed in a correct operator." << endl;
cout << endl << "Enter Q as the operator to stop"
<< endl << endl;
END:;
} while (operators != 'Q'); //This does not work
system("PAUSE");
return 0;
}