C ++不能在循环时退出

时间:2017-10-07 01:08:04

标签: c++ vector while-loop switch-statement

因此,对于我操作向量的程序,在每个切换案例的末尾都会显示一个选项:如果输入了字母'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;
}

1 个答案:

答案 0 :(得分:0)

您的case 'd'看起来不正确。您无法使用spot删除rating中的元素,因为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()会给您相应的评分,并允许您从{{1}正确删除它} vector。

此外,由于您的代码允许重复的球衣号码,删除副本不会删除该号码的所有实例。如果您想解决此问题,可以在rating之后添加spot--;,以确保不会跳过重复的元素。

相关问题