2016-04-11 51 views
2

我的程序将编译,但我遇到了几个问题。我的第一个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; 
} 

回答

2
while (operation == "e" || "E") 

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

  1. 是否operation == "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") 

这可能只是一个错字或监督比什么都重要。

+0

非常感谢。是的,这绝对是一个监督,而不是任何事情。我确保在我的第二个while循环中解决这个问题,我想我只是忽略了它。 –