2017-09-07 237 views
2

我試圖讓一個程序在用戶放入某些愚蠢的東西時不接受代碼錯誤。比如爲一個整數輸入一個字符串,我不知道現在要做什麼。我寫了隨機的東西,因爲它說我沒有足夠的細節,儘管我已經寫了一整段。C++ cin.fail()while循環

void scoretoletter::letter() { 
int a = 0; 
int question; 
while (a == 0) { 
    cout << "1.Rectangle" << endl; 
    cout << "2.Triangle" << endl; 
    cout << "3.Circle" << endl; 
    cout << "4.Exit" << endl; 
    float area; 
    float Width; 
    float Length; 
    float Height; 
    float base; 
    float radius; 
    int l; 
    cin >> question; 
    if (question == 1) { 
     cout << "Whats your length?" << endl; 
     cin >> Length; 
     if (cin.fail()) { 
      cout << "That is not valid try again" << endl; 
     } 
     else { 
      cout << "Whats your width?" << endl; 
      cin >> Width; 
      if (cin.fail()) { 
       cout << "That is not valid try again" << endl; 

      } 
      else { 
       if (Length == 0 || Width == 0) { 
        cout << "That is not valid try again." << endl; 
        system("pause"); 
       } 

       else { 
        area = Length * Width; 
        cout << "The area is: " << area << endl; 
       } 
      } 
     } 
    } 

    else if (question == 2) { 
     cout << "What is the Base?" << endl; 
     cin >> base; 
     if (cin.fail()) { 
      cout << "That is not valid try again." << endl; 


     } 
     else { 
      cout << "What is the Height?" << endl; 
      cin >> Height; 
      if (cin.fail()) { 
       cout << "That is not valid try again." << endl; 

      } 
      else { 
       if (base == 0 || Height == 0 || cin.fail()) { 
        cout << "That is not valid try again." << endl; 
        system("pause"); 

       } 
       else { 
        area = base * Height * .5; 
        cout << "The area is: " << area << endl; 
       } 
      } 
     } 
    } 
    else if (question == 3) { 
     cout << "What is the radius?" << endl; 
     cin >> radius; 
     if (radius == 0 || cin.fail()) { 
      cout << "That is not valid try again." << endl; 
      system("pause"); 


     } 
     else { 


      area = radius * radius * 3.14; 
      cout << "The area is: " << area << endl; 
     } 
    } 
    else if (question == 4) { 
     a = 1; 
    } 
    else { 
     cout << "That is not valid try again." << endl; 
    } 
    system("pause"); 








} 

}

+1

考慮定義一個函數來執行「int」的輸入。它可以有一個內部循環,要求用戶再次嘗試。重要的是不要讓'cin'處於失敗狀態(因爲它忽略了進一步的輸入),所以在這個函數中,我推薦使用''頭中的'getline',以一個字符串的形式讀入一行,然後' stoi'或任何要轉換爲'int'的東西。 –

+0

或者,可以用'std :: cin.clear()清除錯誤' – spectras

回答

3

而是然後利用這種深嵌套醜陋的if-else語法,您可以使用return指令可以寫這個簡單得多。爲避免重複,您也可以返回狀態並循環,直到成功。

enum result_status 
{ 
    RESULT_STATUS_OK, 
    RESULT_STATUS_INVALID, 
    RESULT_STATUS_DONE 
} 

void scoretoletter::askQuestions() { 
    while(true) 
    switch(letter()) 
    { 
     RESULT_STATUS_OK: 
     system("pause"); 
     continue; 

     RESULT_STATUS_INVALID: 
     cout << "That is not valid try again." << endl; 
     system("pause"); 
     cin.clear();    
     continue; 

     RESULT_STATUS_DONE: 
     system("pause"); 
     return; 
    } 
} 

enum result_status scoretoletter::letter() { 
    int question; 

    cout << "1.Rectangle" << endl; 
    cout << "2.Triangle" << endl; 
    cout << "3.Circle" << endl; 
    cout << "4.Exit" << endl; 
    float area; 
    float Width; 
    float Length; 
    float Height; 
    float base; 
    float radius; 
    int l; 
    cin >> question; 

    if (question == 1) { 
     cout << "Whats your length?" << endl; 
     cin >> Length; 
     if (cin.fail()) 
      return RESULT_STATUS_INVALID; 

     cout << "Whats your width?" << endl; 
     cin >> Width; 
     if (cin.fail() || Length == 0 || Width == 0) 
      return RESULT_STATUS_INVALID; 

     area = Length * Width; 
     cout << "The area is: " << area << endl; 
     return RESULT_STATUS_OK; 
    } 

    if (question == 2) { 
     cout << "What is the Base?" << endl; 
     cin >> base; 
     if (cin.fail()) 
      return RESULT_STATUS_INVALID; 

     cout << "What is the Height?" << endl; 
     cin >> Height; 
     if (cin.fail()) 
      return RESULT_STATUS_INVALID; 

     if (base == 0 || Height == 0 || cin.fail()) { 
      return RESULT_STATUS_INVALID; 

     area = base * Height * .5; 
     cout << "The area is: " << area << endl; 
     return RESULT_STATUS_OK; 
    } 

    if (question == 3) { 
     cout << "What is the radius?" << endl; 
     cin >> radius; 
     if (radius == 0 || cin.fail()) { 
      return RESULT_STATUS_INVALID; 

     area = radius * radius * 3.14; 
     cout << "The area is: " << area << endl; 
     return RESULT_STATUS_OK; 
    } 

    if (question == 4) 
     return RESULT_STATUS_DONE; 

    return RESULT_STATUS_INVALID; 
} 

注意使用system("pause");是一個壞主意也,你真的應該寫自己Press any key to continue functionallity到你的程序。