首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Sentinel循环不会运行

Sentinel循环不会运行
EN

Stack Overflow用户
提问于 2016-04-11 12:29:17
回答 1查看 69关注 0票数 2

我的程序可以编译,但我遇到了几个问题。我的第一个要求e/E结束的cout语句可以工作,但是在我声明(+ || - || * || /)的第二个while循环中不会运行。+/-/*//返回“操作类型无效”。你们能帮我找出我的错误吗?

第一个前哨循环,只是学习循环:

代码语言:javascript
复制
#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;
}
EN

回答 1

Stack Overflow用户

发布于 2016-04-11 12:39:43

代码语言:javascript
复制
while (operation == "e" || "E")

这里比较的是以下两种情况之一:

  1. Does operation == "E" "e"
  2. If not,a
    1. aoperation=="E"

第二个条件就是你的问题:"E"当然是一个有效的指针,所以这个条件总是true。一直都是。请注意,在第二种情况下,您没有将operation"E"进行比较。

你永远被困在这里:

代码语言:javascript
复制
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;
}

您只需具备以下条件:

代码语言:javascript
复制
while (operation == "e" || operation == "E")

这可能只是一个打字错误或疏忽。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36539863

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档