#include <iostream>
#include <string>
using namespace std;
int main()
{
int numOne;
int numTwo;
int result;
string operation;
cout << "Please enter what operation you'd like to perform or e/E to end program: ";
cin >> operation;
while (operation == "e" || operation == "E")
{
cout << "Operation type invalid." << endl;
cout << "Please enter what operation you'd like to perform or e/E to end program: ";
cin >> operation;
}
while (operation == "+" || operation == "-" || operation == "*" || operation == "/")
{
cout << "Please enter integer one: ";
cin >> numOne;
cout << "Please enter integer two: ";
cin >> numTwo;
if (operation == "+")
{
result = numOne + numTwo;
cout << "The numbers you entered were " << numOne << "," << numTwo << endl;
cout << "The operation you chose was " << operation << "." << endl;
cout << "The operations result is " << result << "." << endl;
cout << "Your equation was: " << numOne << " " << operation << " " << numTwo << " = " << result << ".";
}
else if (operation == "-")
{
result = numOne - numTwo;
cout << "The numbers you entered were " << numOne << "," << numTwo << endl;
cout << "The operation you chose was " << operation << "." << endl;
cout << "The operations result is " << result << "." << endl;
cout << "Your equation was: " << numOne << " " << operation << " " << numTwo << " = " << result << ".";
}
else if (operation == "*")
{
result = numOne * numTwo;
cout << "The numbers you entered were " << numOne << "," << numTwo << endl;
cout << "The operation you chose was " << operation << "." << endl;
cout << "The operations result is " << result << endl;
cout << "Your equation was: " << numOne << " " << operation << " " << numTwo << " = " << result << ".";
}
else if (operation == "/")
{
if (numTwo == 0)
{
cout << "You cannot divide by zero!" << endl;
}
else
{
result = numOne / numTwo;
cout << "The numbers you entered were " << numOne << "," << numTwo << endl;
cout << "The operation you chose was " << operation << "." << endl;
cout << "The operations result is " << result << endl;
cout << "Your equation was: " << numOne << " " << operation << " " << numTwo << " = " << result << ".";
}
}
}
return 0;
}
我不允许使用break
。还有另一种方式吗?
答案 0 :(得分:1)
只需将while
更改为if
即可。如果你只想执行一次,就不需要循环。
答案 1 :(得分:0)
我对C ++不是很熟悉,但我认为你的意思是break;
语句
答案 2 :(得分:0)
使用
break;
这将每次结束循环。您可以在其周围包装if语句以应用退出条件。
答案 3 :(得分:0)
代码块:
cout << "Please enter what operation you'd like to perform or e/E to end program: "; cin >> operation; while (operation == "e" || operation == "E") { cout << "Operation type invalid." << endl; cout << "Please enter what operation you'd like to perform or e/E to end program: "; cin >> operation; }
不做你所承诺的用户:
Please enter what operation you'd like to perform or e/E to end program:
如果输入"e"
或"E"
,循环将不会停止并再次请求输入。
你可能想要这样的东西:
do {
cin >> operation;
while (operation == "+" || operation == "-" || operation == "*" || operation == "/") {
// ...
}
} while(operation != "e" && operation != "E");
至于你愚蠢的限制,不要使用do {} while();
(踢你的教授,至少做两次):
cout << "Please enter what operation you'd like to perform or e/E to end program: ";
cin >> operation;
while(operation != "e" && operation != "E") {
if (operation == "+" || operation == "-" || operation == "*" || operation == "/") {
// ...
}
else {
cout << "Operation type invalid." << endl;
cout << "Please enter what operation you'd like to perform or e/E to end program: ";
cin >> operation;
}
}
答案 4 :(得分:0)
如果你必须使用while循环,只需添加break;这将打破循环。如果您不必,我会将其更改为if语句。示例:if(varName =='+'){
Num1 + num2;}
对不起格式化,在手机上
答案 5 :(得分:0)
我想你实际上想要实现的是这个。 第一个循环应该重构(即:循环直到操作为'e'或'E'),第二个循环应该是if-else语句(检查操作是否有效)。
int main()
{
int numOne;
int numTwo;
int result;
string operation;
cout << "Please enter what operation you'd like to perform or e/E to end program: ";
cin >> operation;
while (operation != "e" && operation != "E") {
if (operation == "+" || operation == "-" || operation == "*" || operation == "/") {
// your valid operation code...
} else {
cout << "Operation type invalid." << endl;
}
cout << "Please enter what operation you'd like to perform or e/E to end program: ";
cin >> operation;
}
return 0;
}