簡體   English   中英

C++ 不能退出 while 循環

[英]C++ cant exit while loop

所以對於我的操作向量的程序,在每個switch case的末尾都會出現一個選擇:如果輸入字母'q' ,程序應該退出while循環並結束程序,但是當我輸入字母時'q' ,程序崩潰而不是正確退出。 為什么會這樣? 是無限循環嗎?

#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;
}

您的case 'd'看起來不正確。 您不能使用spotrating擦除元素,因為spot只迭代jersey的元素。 要解決此問題並可能解決崩潰或無限循環問題,您可以替換以下代碼:

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向量中正確刪除它。

此外,由於您的代碼允許重復的球衣號碼,刪除重復的號碼並不總是刪除該號碼的所有實例。 如果你想解決這個問題,你可以添加spot--; lim = jersey.size(); 以確保您不會跳過重復的元素。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM