C++ cin.get()出现空行问题
当我运行以下代码并在提示输入时插入新行(按enter键)时 在golf结构中,对函数的第二次调用不请求输入,并完成,就像我再次按下enter键一样 我读过:cin.get(),cin.clear(),cin.ignore(…),但似乎没有任何帮助 我很确定它与多个.cpp文件和头文件无关,但我将代码保持原样 我使用VisualStudioC++ 2010—Express。< /P> 提前感谢您的帮助 头文件:golf.hC++ cin.get()出现空行问题,c++,string,line,C++,String,Line,当我运行以下代码并在提示输入时插入新行(按enter键)时 在golf结构中,对函数的第二次调用不请求输入,并完成,就像我再次按下enter键一样 我读过:cin.get(),cin.clear(),cin.ignore(…),但似乎没有任何帮助 我很确定它与多个.cpp文件和头文件无关,但我将代码保持原样 我使用VisualStudioC++ 2010—Express。< /P> 提前感谢您的帮助 头文件:golf.h #ifndef GOLF_H #define GOLF_H const
#ifndef GOLF_H
#define GOLF_H
const int Len = 40;
struct golf{
char fullname[Len];
int handicap;
};
int setgolf(golf & g );
void showgolf(const golf & g );
#endif
golf.cpp
#include "stdafx.h"
#include "golf.h"
#include <iostream>
#include <string>
using namespace std;
int setgolf(golf & g ){
cout << "Enter a name for the golf structure:" << endl;
if (cin.get(g.fullname,Len)) {
cin.get(); // deals with the '\n' incase the user inputs a valid string
return 1;
}
else{
return 0;
}
}
void showgolf(const golf & g ){
cout << "this golf structure contains the following information:" << endl;
cout << "name: " << g.fullname << endl ;
cout << "handicap: " << g.handicap << endl ;
}
#包括“stdafx.h”
#包括“golf.h”
#包括
#包括
使用名称空间std;
内特赛特高尔夫球场(高尔夫球场和高尔夫球场){
cout当您在第一个提示中按enter键时,就会出现此问题。输入流被标记为eof,这是一个错误条件标志(这就是它返回0的原因)。然后输入流停止工作
你似乎在使用一种旧的C++、预ISO 1998,但我不认为你需要这样。但是,如果你想坚持你的方法,那么就执行以下操作:在代码> CIN。GETLION()/<代码>(不需要返回任何东西)写:<代码> CIN。CULL();CIN。
void setGolf(Golf &g)
{
cout << "Enter a name for the golf structure:" << endl;
getline( cin, g.fullname ) );
cin.clear();
cin.sync();
}
现在,您可以在实用程序中使用getline()
,它读取整行内容并将其存储在字符串中,为您发挥所有的魔力。因此,您可以将golf.cpp
文件更改为:
#include <utility>
//...
void setGolf(Golf &g)
{
cout << "Enter a name for the golf structure:" << endl;
getline( cin, g.fullname ) );
}
<>最后,您可以考虑将信息封装在一个类中,而不是一个结构体,但这是一个完全不同的问题。
希望这有帮助。您也可以离开char[]
而不是用字符串替换它(我仍在学习,如果我错了,请纠正我)。我想当
std::cin.get(char*,Size)
无法加载字符,它将2位变为0,失败并出错,这是我的解决方案:
std::cin.get(g.fullname, Len);
if(!std::cin)
{
std::cin.clear();
std::cin.get();
std::cout << "You inserted empty line." << std::endl;
return 0;
}
std::cin.get(g.fullname,Len);
如果(!std::cin)
{
std::cin.clear();
std::cin.get();
Std::CUT您可以考虑在这个过程中的自由函数。我认为CIN。GET()不消耗在输入Enter时生成的换行符。因此,每当您再次调用它时,它就被消耗在那里,没有输入。如果您想快速回答,请调用CIN。两次接受一个输入。mostruash->已经尝试过几次。运气不好。WhozCraig->我正在尝试熟悉cin.get()**忘记启动myGolf,myGolf={“某个名字”,6};但这与此无关Baltasarq->感谢您提供的精彩提示!是的,#include是redandent。不幸的是,我真的很想了解cin.get()的这个具体问题。您知道出现上述问题的原因吗?我有一个新安装的visual studio 2010,如果默认安装时使用过时的标准,我会觉得很奇怪。当您在第一个提示中按enter键时,就会出现此问题。输入流被标记为eof,错误条件标志(这就是它返回0的原因)。然后输入流停止工作。如果您想继续使用您的方法,请执行以下操作:在cin.getline()之后(无需返回任何内容)写入:cin.clear();cin.sync();Baltasarq->谢谢!我以前尝试过cin.get();cin.clear()。但是cin.sync();成功了!我会读一些关于它的文章。我很乐意把你的答案标记为正确,只要把你的评论作为答案贴出来就行了。
#include <utility>
//...
void setGolf(Golf &g)
{
cout << "Enter a name for the golf structure:" << endl;
getline( cin, g.fullname ) );
}
int main()
{
Golf myGolf;
setGolf(myGolf);
showGolf(myGolf);
setGolf(myGolf);
showGolf(myGolf);
return 0;
}
std::cin.get(g.fullname, Len);
if(!std::cin)
{
std::cin.clear();
std::cin.get();
std::cout << "You inserted empty line." << std::endl;
return 0;
}