Getline 不让我输入,C++
getline won't let me type, c++
我试图获得用户选择的游戏名称,并将其存储在向量中。我使用getline以便用户可以使用空间。当我尝试键入一个新游戏来添加时,它不允许我。它自动显示我的游戏库。请告诉我我做错了什么。问题出现在如果(操作=="添加")
这是我的代码:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
vector<string>::const_iterator myIterator;
vector<string>::const_iterator iter;
vector<string> games;
games.push_back("Crysis 2");
games.push_back("GodOfWar 3");
games.push_back("FIFA 12");
cout <<"Welcome to your Games Library.n";
cout <<"nThese are your games:n";
for (iter = games.begin(); iter != games.end(); ++iter)
{
cout <<*iter <<endl;
}
//the loop!
string action;
string newGame;
cout <<"n-Type 'exit' if you want to quit.n-Type 'add' if you want to add a game.n-Type 'delete' if you want to delete a game.n-Type 'find' if you want to search a game.n-Type 'game' if you don't know what game to play.n-Type 'show' if you want to view your library.";
while (action != "exit")
{
cout <<"nnWhat do you want to do: ";
cin >> action;
//problem is here
if (action == "add")
{
cout <<"nType the name of the game you want to add: ";
getline (cin, newGame);
games.push_back(newGame);
for (iter = games.begin(); iter != games.end(); ++iter)
{
cout <<*iter <<endl;
}
continue;
}
else if (action == "show")
{
cout <<"nThese are your games:n";
for (iter = games.begin(); iter != games.end(); ++iter)
{
cout <<*iter <<endl;
}
}
else if (action == "delete")
{
cout <<"Type the name of the game you want to delete: ";
cin >> newGame;
getline (cin, newGame);
iter = find(games.begin(), games.end(), newGame);
if(iter != games.end())
{
games.erase(iter);
cout <<"nGame deleted!";
}
else
{
cout<<"nGame not found.";
}
continue;
}
else if (action == "find")
{
cout <<"Which game you want to look for in your library: ";
cin >> newGame;
getline (cin, newGame);
iter = find(games.begin(), games.end(), newGame);
if (iter != games.end())
{
cout << "Game found.n";
}
else
{
cout << "Game not found.n";
}
continue;
}
else if (action == "game")
{
srand(static_cast<unsigned int>(time(0)));
random_shuffle(games.begin(), games.end());
cout << "nWhy don't you play " << games[0];
continue;
}
else if (action == "quit")
{
cout <<"nRemember to have fun while gaming!!n";
break;
}
else
{
cout <<"nCommand not found";
}
}
return 0;
}
我不知道你到底写了什么,但:
getline
将在您的案例newGame
中的第二个参数中获取整行- 当您在getline上方调用
cin >> newGame;
时,您必须使用string
的istream operator >>
。它一直读到第一个分隔符。在您的情况下,这是Tomb
和Raider
之间的空间 - 使用
getline
覆盖用cin >> newGame;
读取的值。在getline
之前,其值为Tomb
,在后文中变为Raider
只需移除cin
:
cin >> newGame;
getline (cin, newGame);
->
getline (cin, newGame);
编辑现在,当您发布所有代码时,我相信您的案例正是我所想的。在cin >> action;
行中,提示用户选择一个操作。他输入并点击回车键,这样他就可以再次激活你的程序。但是,cin >>
将读取该值,但不会清除用户按下的输入。因此,您调用的第一个getline
将只读取这个回车,而不读取其他内容。如果你这样做:
cin >> newGame;
getline (cin, newGame);
->
cin.get();
getline (cin, newGame);
这会奏效的。我试过了。基本上第一个cin.get();
清除输入,getline
提示用户输入。
EDIT2在布局上再添加一个改进,即可处理后面的新行。这可能是处理它们的最正确的方法,但我故意没有提供这个解决方案,试图不将OP与复杂代码混淆:
if (cin.peek() == 'n' || cin.peek() == 'r') {
cin.get();
}
getline (cin, newGame);
混合
cin >> value1;
和
getline(cin, value2);
正在招致麻烦。问题是getline
读取到下一个"\n"(并消耗它),而>>
读取到下个空白(并不消耗它)。
这意味着,当您通过>>
读取value1
时,换行符仍保留在流中,然后您尝试读取整行并读取"nothing"(getline
将使用作为流的即时输入的换行符)。
将getline
加倍的建议将起作用,但只有当您通过另一个分支中的getline
读取时,它才会断开,因为getline
会消耗新行,而>>
不会。
如果需要进一步处理,我建议您通过getline
处理所有输入,然后标记读取的字符串。这样可以确保新行处理是一致的,并且可以正确读取空格。
你能试试这个吗:
cout <<"Type the name of the game you want to add: ";
而不是你的这个代码:
cout <<"nType the name of the game you want to add: ";
我不确定它是否有效。但请试一试。
问题可能是由于一个杂散'\n'。顾名思义,getline获取整行的值。那么你的编译器是如何知道你什么时候完成的呢。它使用字符n
。如果它读取到一个遗留的n
,它将假设该行已经结束,所以如果你删除它,我认为它应该可以工作。所以,只要找出getline之前的行,它可能留下了一个杂散。
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 2D数组来自文本输入,中间有空格
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 检查输入是否不是整数或数字
- 正在尝试了解输入验证循环
- 读取文件并输入到矢量中
- C++如何通过用户输入删除列表元素
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何使用用户输入在C++中正确填充2D数组
- C++MySQL C api用户输入行
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- 用户定义函数中的指针和输入
- 如何在C++中检查2D数组中负值的输入验证
- 如何只允许用户输入正整数
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- 输入中的字符串数未知(以字母表示)
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- 在一个模板函数中,若输入的类型是enum类,我该如何使用std::underlying_type