C Getline首次通话后将跳过

C++ getline gets skipped after first call

本文关键字:Getline      更新时间:2023-10-16
char input_Handler(int& error,int user,const char satir[]){
    string cheyeni;
    int error_string = 0;
    char che;
    char test;
    cout << "Please enter your move with keyboard letters such as 'A' or 'a'n";
    //cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');
    //cin.ignore();
    //
    cin.ignore();
    getline(cin,cheyeni);
........rest is not here

我有此功能可以处理游戏的输入。该功能在游戏中多次被称为。在第一次调用中,正确处理输入。但是,当再次被调用时,它的行为愚蠢。从我在网络上的搜索中,人们总是说getline在我按Enter时不会分配newline,所以当我再次调用时,它不会从我这里得到输入,而是从我的字符串中获得' n'(cheyeni)自动。

我似乎无法解决此问题,我尝试了多种cin.sync(),cin.ignore()。虽然您可能认为我确实错误地使用了它,但我在GetLine等之后尝试了很多方法。但是,我仍然可以/可能在某些事情上错了。我似乎看不到我缺少的部分。

用户期望输入一些字符串或字母。

cin.ignore()放在 getline()

下方
getline(cin,cheyeni);
cin.ignore();