- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我疯狂地用谷歌搜索了这个问题,找到了许多“解决方案”,以及 2001-2007 年的论坛帖子,说这是 Visual Studio 中的一个“众所周知的错误”......所以为什么不呢?现在修好了吗?我使用的是 VisualStudio 2013(版本 12)。
这是我的代码:
#include <iostream>
#include <iomanip>
#include <string>
#include <Windows.h>
#include <vector>
#include <conio.h>
#include <string>
#undef max
using namespace std;
/*
TEST CASES:
*/
// START FUNCTION - Add Restaurant
void resto(vector<string> &restaurant)
{
std::string resto_name;
cin.ignore();
//cin.clear();
//cin.sync();
std::getline (std::cin, resto_name,'\n');
restaurant.push_back(resto_name);
}
// END FUNCTION - Add Restaurant
// START FUNCTION - Print Vector
void print_vector(vector<string> &restaurant)
{
cout << endl << "Your Current Restaurants:" << endl;
for (int i = 0; i < restaurant.size(); i++)
{
cout << i + 1 << ". " << restaurant[i] << endl;
}
}
// END FUNCTION - Print Vector
int main(void)
{
srand(time(0));
int menu;
vector<string> restaurant; //Initialize Vector
do {
system("cls");
cout << " -----------------------------------------------------------" << endl;
cout << "| -:|:- Welcome to Restaurant Battle Royale! -:|:- |" << endl;
cout << "|-----------------------------------------------------------|" << endl;
cout << "| Please select an option by pressing 1, 2, 3, 4, 5, or 6 |" << endl;
cout << "|\t\t\t then press ENTER.\t\t |" << endl;
cout << "|-----------------------------------------------------------|" << endl;
cout << "| 1 - Display all restaurants\t\t\t\t |" << endl;
cout << "| 2 - Add a restaurant\t\t\t\t\t |" << endl;
cout << "| 3 - Remove a restaurant\t\t\t\t |" << endl;
cout << "| 4 - Shuffle the vector\t\t\t\t |" << endl;
cout << "| 5 - Begin the tournament\t\t\t\t |" << endl;
cout << "| 6 - Quit the program\t\t\t\t\t |" << endl;
cout << " -----------------------------------------------------------" << endl;
cout << "Option: ";
cin >> menu;
//OPTION 1
switch (menu)
{
case 1:
print_vector(restaurant);
cout << endl;
printf("Press any key to return to the main menu...");
system("pause > nul");
break;
//OPTION 2
case 2:
cout << "\nPlease enter the name of the restaurant you'd like to add." << endl;
resto(restaurant);
break;
//OPTION 3
case 3:
break;
case 4:
return 0;
}
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
} while (menu != 6);
return 0;
}
我遇到的问题是在“添加餐厅”功能中。输入 getline 后,您必须按两次 Enter 键。我尝试过添加“\n”和“endl”之类的方法,但没有成功。我很想认为这是编译器的一个错误,但经过这么多年的开发,这个错误持续这么长时间,这太可悲了,对吧?
所以这一定是我的代码。你能帮我找出问题所在吗?
我已经在这个问题上坚持了几个小时了,我无法继续前进,因为它太困扰我了。
================================================== =====
引用文献:
微软对此的解决方案来自谁知道何时: http://support2.microsoft.com/default.aspx?scid=KB;EN-US;q240015&ID=KB;EN-US;q240015(查看字符串文件后,看起来他们已将此建议的更改应用到当前版本)
我尝试过的解决方案对其他人有效,但对我无效: http://www.dreamincode.net/forums/topic/35262-visual-c-getline-bug/
最佳答案
@sajas 是正确的。您需要 cin.ignore()
的原因在resto里面是因为cin >> menu
之后,流中会留下杂散换行符。后续getline
提取换行符并立即终止。 cin.ignore()
将提取并丢弃杂散换行符,从而允许 getline
才能正常工作。
实际上,导致问题的行是:
cin.ignore(numeric_limits<streamsize>::max(), '\n');
这一切只是提取并丢弃换行符之前的所有字符,因此完全没有必要。
<小时/>为了捕获错误的输入,您可以这样做:
while (!(cin >> menu))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
std::cout << "Please enter a number.\nOption: ";
}
如果提取失败(因为用户没有输入数字),则清除错误标志,丢弃流中的所有错误字符,然后再次要求输入。它将循环直到用户输入数字。
<小时/>每@The Paramagnetic Croissant's comment , using namespace std;
is considered bad practice.我注意到你与你放置的位置不一致 std::
。一般来说,如果您选择使用 using 指令,请将其放置在函数内,这样它就不会污染全局命名空间。最好养成输入std::
的习惯。到处都是,因为输入的次数并不多。
您包含了非标准 Windows header 、Windows.h 和 conio.h。由于您没有利用任何 API 功能,因此您不需要它们。
删除printf
。你不需要它。
system()
是非常特定于平台的并且不受欢迎(尽管我没有任何来源来支持这一点。)更喜欢 cin.get()
至system("pause")
并删除 system("cls")
因为这可能会激怒用户。
内部print_vector
,您正在使用int
当你应该使用std::size_t
时或者最好std::vector<string>::size_type
。您的编译器可能会警告您:
main.cpp:56:23: warning: comparison of integers of different signs: 'int'
and 'size_type' (aka 'unsigned long') [-Wsign-compare]
for (int i = 0; i < restaurant.size(); i++)
关于c++ - 在 C++ 中的 getline 之后必须按 "Enter"两次吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26581793/
我已经禁用了回车键,以便可以执行 ajax 提交,但我想确保如果用户在服务器响应返回之前按两次回车键,则表单不会提交两次。 这里我禁用了回车键并为其分配了一个名为 submitForm() 的函数:
我正在尝试将 $animate 服务合并到我自己的指令中。我无法进入并离开以实际设置动画。 奇怪的是,使用$animate.enter,元素附加到DOM,回调函数触发。但似乎从未添加 ng-anima
如果 ,我如何检查 Bash输入 键被按下了? 我正在使用读取命令: read -p "Please press ENTER" var 最佳答案 首先检查退出状态是否正常($?应该为0)。 其次,检查
在我的 PreviewKeyDown() handler 如何区分数字键盘上的 ENTER 键和主板上的 ENTER 键? 两个键返回相同的值 Key.Enter为 KeyEventArgs.Key
这个问题已经有答案了: How do I detect "shift+enter" and generate a new line in Textarea? (19 个回答) 已关闭10 年前。 我正
我的问题与这个 question 有点相关.但是,我没有奢侈去监听 submit 事件,所以这个问题的答案对我来说不起作用。 我的问题:是否可以在 Javascript 中检测日文/中文建议菜单何时打
我希望能够在不需要实现新类的情况下修改 wpf 文本框的行为。 我想要一个类似 Enter/Alt+Enter 行为的 Excel,当用户点击“Enter”时,文本框被验证(movefocus ...
在 Chrome 中的 contenteditable 中按 Enter 时,会插入一个 div。这会干扰我的标记,我需要它成为 br。 我知道shift-enter是一个br。解决问题的最佳方法是什
我正在使用$('.inputs').keydown(function (e) { if (e.which === 13) {到达下一个输入字段。但是每次我按 enter/return 转到下一个输入字
我有一个 textarea ,其中当您单独按 Enter 时,jquery ajax 会启动,但我想添加一个功能,即当您按 Enter + Shift 时仅转到新行并且不启动ajax。你能指导一下吗?
场景如下: 有一个密码字段,我可以在其中输入文本(这是使用 sendKeys 完成的) 现在,没有提交/输入按钮 所以我必须按 Enter 键并登录应用程序 以下是我搜索并尝试但没有成功的几种方法:
这是我尝试过的,但显然失败了: ed.on('keyup', function(e){ console.log(e.keyCode) if(e.keyCode == 13 && !e.
所以我有一个带有 jQuery 的表单,可以阻止用户使用 Enter 键提交表单。 这是代码: $(document).ready(function() { // code prevent
有人知道如何在Javascript中检测按键代码“+1 Enter”和“-1 Enter”吗?我想检测何时一一按下键码而不是一次按下键码。 但是当我编写以下代码时,没有给出任何效果。 // +1 en
我想添加 shift + enter 键转到下一行,只需 enter 键 即可使用 JAVASCRIPT 触发提交按钮仅。 如何实现? 我也想提交多行文本......提前致谢 function typ
int DisplaySchedule() { int nDisplaySchedule_Choice; system("cls"); printf("----- DISPLA
在 textarea 中,当用户按下 Shift+Enter 时,它应该在下一个新行继续,当他简单地按下 Enter 时,它应该提交表单而不使用提交按钮。 这是 Fiddle!! 我浏览了很多但对我没
我有以下简单的 我还有以下 jQuery/JavaScript 代码块: $('textarea#streamWriter').keydown(function (e) { if (e.k
我有许多将转换为HTML的Word文档。要求单词文档中的段落应转换为元素。 在使用Microsoft Office API的SaveAs方法进行了一些测试以将文档转换为HTML之后,我意识到带有手动换
终端是否可以检测 ⇧ Shift+Enter↵ 或 Ctrl+Enter↵ 按键? 我正在尝试配置 vim 来执行使用这些序列的键映射,虽然它们在 gvim 中工作正常,但它们似乎在任何终端控制台中都
我是一名优秀的程序员,十分优秀!