cin处理中的分割错误

时间:2018-12-26 07:29:03

标签: c++11

void Room::join(){
 try{
    check_input_arguments();
    bool enter_not_pressed = true;
    string name;
    char c;
    int counter = 0 ;
    while(enter_not_pressed){
        cin >> name ;
        counter ++ ;
        name_of_players.push_back(name);
        cin.get(c);
        enter_not_pressed = (c!='\n');
        cout << counter;
       }
    if(name_of_players.size() > get_num_of_roles()){
        for(int i=0 ; i < counter ; i++)
            name_of_players.pop_back();
        throw too_many_users();
       }
    if(name_of_players.size() == get_num_of_roles()){
        give_random_roles();
       }
    }
   catch(few_arguments ex){
        cerr << "You didn't enter enough arguments!" << endl;
    }
   catch(too_many_users ex){
    cerr << "many users" << endl;
    }
}

我为Room类编写此方法,当我想接收与get_num_of_roles()等效的字符串数时,遇到段错误错误,但是此代码对大于get_num_of_roles的字符串数有效(打印“太多”用户) ),且小于get_num_of_roles(什么也不做)。 这是check_input_arguments()函数

void check_input_arguments(){
char c;
cin.get(c);
if(c == '\n')
    throw few_arguments();
}

请帮助我,谢谢大家!

0 个答案:

没有答案