c++ - 哨兵循环不会运行

标签 c++

我的程序可以编译,但遇到了一些问题。我的第一个 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")

这里您正在比较两个条件之一:

  1. 操作 == “e”吗?
  2. 如果不是,"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++ - 哨兵循环不会运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36539863/

相关文章:

c++ - 为什么我不能打印工资?

c++ - GCC 警告 : ignoring attributes on template argument (-Wignored-attributes) 的含义

c++ - 如何从 const float* 中获取 C++ vector

c++ - 如何使用 WinCrypt 和 C++ 以 PEM 格式导入私钥?

C++ Socket 连接自动建立?

c++ - 错误 : ‘int’ is not a class, 结构或 union 类型`

c++ - 为什么尝试在模板中使用纯虚函数构建 C++ 代码时会出现链接器错误?

c++ - valgrind : invalid read of size 1 during strcpy

c++ - 将外部库链接到项目仍然提供 undefined reference

c++ - 编译器显示错误消息 "makes pointer from integer without cast"(反转数组)。我该如何解决这个问题?