哨兵循环不会运行
Sentinel loop won't run
我的程序会编译,但我遇到了几个问题。我的第一个cout
语句要求e/E
结束,但在我的第二个while循环中,我声明(+ || - || * || /)
不会运行。+/-/*//
返回"操作类型无效"。你们能帮我看看我的错误吗?
第一个哨点循环,只是学习循环:
#include <iostream>
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" || "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: " << endl;
cin >> numOne;
cout << "Please enter integer two: " << endl;
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;
}
while (operation == "e" || "E")
在这里,您正在比较两种条件之一:
operation
=="e"
吗- 如果不是,
"E"
是否为有效指针
第二个条件就是你的问题:"E"
当然是一个有效的指针,所以这个条件总是true
。总是请注意,在第二个条件中,您没有比较operation
和"E"
。
你永远被困在这里:
while (operation == "e" || "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 == "e" || operation == "E")
这可能只是一个拼写错误或疏忽,而不是其他任何事情。
相关文章:
- 循环无限运行C++解决骑士之旅问题
- 我的代码运行良好,但在游戏循环中中断
- 如何根据用户在C++中的输入运行不同数量的 for 循环
- 我可以根据用户输入在运行时生成"循环"吗?
- 重新启动后,线程无法在 while 循环中再次运行
- 运行无限循环的最小二叉树问题
- 目标C++,如何在后台线程中使用运行循环?
- 如何在指定的时间内运行循环
- 苹果运行循环的回调方法
- 我怎样才能一直运行循环,直到按下键 (C++)
- 如何在C++中每次运行循环时将变量递增1`
- 如何在第一次运行循环时忽略代码的特定部分
- 如何在每次运行循环时更改for循环中的对象
- 应用程序在访问主运行循环时崩溃 (SIGABRT)
- osX :运行循环选项(创建事件点击时)
- 线程如何运行循环直到连接
- 在指定时间运行c++循环
- 在运行循环外声明、分配和释放线程局部指针变量
- 这是在c++中运行循环的有效方式吗?
- 如何在qt中使用gui对象名运行循环