2017-10-07 21 views
-2

私のプログラムでは、各スイッチの最後に選択肢が表示されます:文字が'q'になると、プログラムはwhileループを終了してプログラムを終了し、しかし、私が手紙'q'を入力すると、正しく終了する代わりにプログラムがクラッシュします。なぜこうなった?それは無限ループですか?ループ中にC++カントを終了する

#include <iostream> 
#include <vector> 
#include <algorithm> 
using namespace std; 

int main() { 
    int rnum; 
    int jnum; 
    int i; 
    int lim = 5; 
    char choice='b'; 
    vector<int> jersey(lim); 
    vector<int> rating(lim); 

    cout << "MENU" << endl; 
    cout << "a - Add player" << endl; 
    cout << "d - Remove player" << endl; 
    cout << "u - Update player rating" << endl; 
    cout << "r - Output players above a rating" << endl; 
    cout << "o - Output roster" << endl; 
    cout << "q - Quit" << endl; 
    cout << "" << endl; 
    cout << "Choose an option:" << endl; 

    cin >> choice; 

    while(choice != 'q') { 

     switch(choice) { 

     case 'a' : 
      // addplayer 
      for(int i = 0; i<=lim-1; i++) 
      { 
       cout << "Enter a new player's jersey number:" << endl; 
       cin >> jersey.at(i); 
       cout <<"Enter the player's rating:" << endl; 
       cin >> rating.at(i); 

      } 

      cout << "Choose an option:" << endl; 
      cin >> choice; 
     break; 

     case 'u' : 
      // updat rating 
      cout << "Enter a jersey number:" << endl; 
      cin >> jnum; 

      for(int i = 0; i <= lim-1; i++) 
      { 
       if(jersey.at(i) == jnum) 
       { 
        cout << "Enter a new rating for player:" <<endl; 
        cin >> rnum; 
        rating.at(i) = rnum; 

        break; 
       } 
      } 

      cout << "Choose an option:" << endl; 
      cin >> choice; 
     break; 

     case 'o': 
      cout << "ROSTER" << endl; 
      for(int i = 0; i<lim; i++) 
      { 
       cout << "Player "<<i+1 <<" -- Jersey number:" << " " <<jersey.at(i) << ", " << "Rating: " << rating.at(i) << endl; 
      } 

      cout << "Choose an option:" << endl; 
      cin >> choice; 
     break; 

     case 'd': 
      cout << "Enter a jersey number:" << endl; 
      cin >> jnum; 

      for(std::vector<int>::iterator spot = jersey.begin(); spot != jersey.end(); ++spot) 
      { 
       if(*spot == jnum) 
       { 
        jersey.erase(spot); 
        rating.erase(spot); 
        lim = jersey.size(); 
       } 
      } 

      cout << "Choose an option:" << endl; 
      cin >> choice; 
     break; 

     case 'r': 
      cout << "Enter a rating:" << endl; 
      cin >> rnum; 

      for(int i = 0; i <= lim-1; i++) 
      { 
       if(rating.at(i) >= rnum) 
       { 
        cout << "Player "<<i+1 <<" -- Jersey number:" << " " <<jersey.at(i) << ", " << "Rating: " << rating.at(i) << endl; 
       } 
      } 

      cout << "Choose an option:" << endl; 
      cin >> choice; 
     break; 


     default: 
      cout << "Choose an option:" << endl; 
      cin >> choice; 
     break; 

     } 
    } 

    return 0; 
} 
+1

これに最適なツールはデバッガです... – Charles

+0

コードを書式設定して読めるようにしましたか? –

+0

あなたの書式設定があなたを倒してしまいます。そのままですが、私はエラーを見ません。あなたは単純な配列であるかのようにベクトルを誤用しています...「プログラムが正しく終了するのではなくクラッシュする」ということはどういう意味ですか? (つまり、失敗を複製する正確な入力を与えてください) –

答えて

0

case 'd'が正しく表示されません。 spotを使用すると、spotjerseyの要素を反復するだけなので、ratingから要素を消去することはできません。で

if(*spot == jnum) 
{ 
    jersey.erase(spot); 
    rating.erase(spot); 
    lim = jersey.size(); 
} 

:これを修正し、潜在的にあなたのクラッシュや無限ループの問題を解決するには、次のコードを置き換えることができます

ここ
if(*spot == jnum) 
{ 
    jersey.erase(spot); 
    rating.erase(rating.begin() + (spot - jersey.begin())); 
    lim = jersey.size(); 
} 

spot - jersey.begin()はちょうどあなたのジャージの指標を与えますそれをrating.begin()に追加すると、それに対応する評価が与えられ、ratingベクトルから適切に消去できるようになります。

さらに、コードで重複したジャージー番号が許可されているため、重複を削除しても必ずしもその番号のインスタンスがすべて削除されるわけではありません。これを修正したい場合は、lim = jersey.size();の後にspot--;を追加して、重複する要素をスキップしないようにすることができます。

関連する問題