如何获取不以空格结尾的字符串的输入

How to get input to a string that does not terminate with a space?

本文关键字:结尾 空格 字符串 输入 何获取 获取      更新时间:2023-10-16
#include <iostream>
#include<string>
#include<vector>
#include<algorithm>

using namespace std;
int main()
{
 vector<string>Games;
 vector<string>::iterator iter;
 string command;
 string name;

cin>>command;
if(command=="add"){
    cout<<"You have choosen to add a game to your backlog.n";
    cout<<"Enter the name of the Video Gamen";
    getline(cin,name); 
    Games.push_back(name);
    cout<<"The Game has been addedn";
}

}

是的,我知道这已被存档。而且我知道您应该使用 getline() 函数将 cin 和字符串名称传递给函数来保存字符,但是当我在键入"add"命令后编译并运行它时,该行getline(cin,name)被跳过,我无法输入任何内容。

之前

getline(cin,name); 

cin.ignore( std::numeric_limits<std::streamsize>::max(), 'n' );

要使用此语句,您需要包含标头<limits>