2016-04-11 30 views
2

私のプログラムはコンパイルされますが、私はいくつかの問題を抱えています。私の最初のcoutステートメントは、終了するのにe/Eを必要としますが、私の最初のwhileループでは、(+ || - || * || /)というステートは実行されません。 +/-/*//は「操作タイプが無効です」を返します。あなたは私のエラーを見るのを助けることができますか?Sentinelループは実行されません

まずセンチネルループ、ループだけを学ぶ:

#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") 

ここでは、2つの条件のいずれかを比較している:

  1. operation == "e"していますか?
  2. 有効でない場合、"E"は有効なポインタですか?第2の条件は、あなたの問題であること

:条件は常にtrueになるよう"E"は、もちろん有効なポインタです。常に。 2番目の条件では、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

ありがとうございました。はい、それは間違いなく何よりも監視でした。私は2回目のループでそれを修正しました。そして私はそれを見落としたと思います。 –

関連する問題