2013-02-10 83 views
-3

我想使它所以如果一個字符輸入在開始,而不是1,2,3或4那麼它將循環回輪,除了說ç簡單的計算器錯誤

cout << "You have entered a incorrect operator" << endl; 

我已經嘗試了包括default:的情況,但似乎並沒有影響它的數量。

任何人都可以擺脫任何光線?

http://pastebin.com/9U7sEEzL

#include <iostream> 
#include <string> 

void start() 
{ 
    using namespace std; 

    cout << "Welcome to my Basic Mini-Calculator!" << endl; 
} 

int choice() 
{ 
    using namespace std; 

    int uChoice; 

    cout << endl << "What do you want to do?" << endl; 
    cout << "1) Add" << endl; 
    cout << "2) Subtract" << endl; 
    cout << "3) Multiply" << endl; 
    cout << "4) Divide" << endl; 
    cout << endl << "Waiting for input... (enter a number): "; 
    cin >> uChoice; 
    cout << endl; 

    while(uChoice != 1 && uChoice != 2 && uChoice != 3 && uChoice != 4); 

    switch (uChoice) 
    { 
     case 1: 
      cout << endl << "You chose addition." << endl; 
      break; 

     case 2: 
      cout << endl << "You chose subtraction." << endl; 
      break; 

     case 3: 
      cout << endl << "You chose multiplication." << endl; 
      break; 

     case 4: 
      cout << endl << "You chose division." << endl; 
      break; 
    } 

    return uChoice; 



} 

int input(bool i = false) 
{ 
    using namespace std; 

    string text; 

    text = (i == true) ? "Enter another number: " : "Enter a number: "; 
    cout << endl << text; 

    float number; 
    cin >> number; 

    return number; 
} 

int work(int one, int two, int todo) 
{ 
    using namespace std; 

    float answer; 

    switch (todo) 
    { 
     case 1: 
      answer = one + two; 
      break; 

     case 2: 
      answer = one - two; 
      break; 

     case 3: 
      answer = one * two; 
      break; 

     case 4: 
      answer = one/two; 
      break; 

     default: cout << "Please choose a proper number (1-4)" << endl; 
    } 


    return answer; 
} 

void answer(int theanswer) 
{ 
    using namespace std; 

    cout << endl << "The answer is " << theanswer << "." << endl; 
    cout << endl << "Hit Return to exit."; 

    cin.clear(); 
    cin.ignore(255, '\n'); 
    cin.get(); 
} 

int main() 
{ 
    using namespace std; 

    start(); 

    int todo = choice(); 

    float one = input(); 
    float two = input(true); 

    float theanswer = work(one, two, todo); 


    answer(theanswer); 

    return 0; 
} 
+0

將代碼放入問題_(在創建_short_示例程序之後,因爲129行太多)。 SO是一個問答檔案,你的問題沒有問題。 – 2013-02-10 11:51:25

回答

2

只需添加一個do

do 
{ 
cout << endl << "What do you want to do?" << endl; 
cout << "1) Add" << endl; 
cout << "2) Subtract" << endl; 
cout << "3) Multiply" << endl; 
cout << "4) Divide" << endl; 
cout << endl << "Waiting for input... (enter a number): "; 
cin >> uChoice; 
cout << endl; 

} while(uChoice != 1 && uChoice != 2 && uChoice != 3 && uChoice != 4); 
1

當您嘗試讀取了號碼,但您會收到無效的輸入爲,流進入一個「失敗國家」。您可以檢查這個「cin.fail()」,或者你使用整個流作爲參數:

// try reading 
while(!(cin >> n)) 
{ 
    cin.clear(); // reset fail state 
    string s; 
    getline(cin, s); // discard remaining line 
} 

這就是說,請降低你的代碼下一次小例子。

+0

最小示例...? 我在最後把這個放在我無效的答案中?我認爲那是它應該去的地方,但是當我在開始時輸入一個字符的時候它並沒有消除永遠循環的問題。 – user2058678 2013-02-10 18:10:05

+0

查看http://sscce.org/瞭解最小示例的含義。我不知道你指的是「最後的無效答案」,所以我不清楚你的問題在哪裏。你試過運行這段代碼嗎? – 2013-02-10 19:29:59

+0

試過了。沒有工作。 – user2058678 2013-02-10 20:32:14