2013-02-18 88 views
2

当我运行下面的代码,并提示输入 高尔夫结构时插入一个新行(按Enter键),第二次调用该函数不会请求输入并完成,如果我再次输入。我读过:cin.get(),cin.clear(),cin.ignore(...),但似乎没有任何帮助。cin.get()问题与空行

我敢肯定,它与多个.cpp文件和标题无关,但我把代码原样。

我正在使用Visual Studio C++ 2010 - Express。

在此先感谢您的帮助!

头文件:golf.h

#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 ; 

} 

的main()

#include "stdafx.h" 
#include "golf.h" 
#include <iostream> 
#include <string> 


using namespace std; 

int main() 
{ 

    golf myGolf; 

    // check of int setgolf(golf & g); 
    int answ = setgolf(myGolf); //try to input empty string 
    showgolf(myGolf); 
    cout << "the number returned :" << answ << endl ; 

    answ = setgolf(myGolf); // normal string 
    showgolf(myGolf); 
    cout << "the number returned :" << answ << endl ; 

    return 0; 
} 
+3

您可能会考虑使用自由函数['std :: getline()'](http://en.cppreference.com/w/cpp/string/basic_string/getline)。 – WhozCraig 2013-02-18 19:25:32

+0

我认为cin.get()不会消耗当你输入时产生的换行符。所以无论何时再次调用它,它都会在那里消耗,不会输入任何内容。如果您想快速回答,请调用cin.get()两次以获取单个输入。 – mostruash 2013-02-18 19:35:21

+0

mostruash - >已经尝试了几次。没有运气。 WhozCraig - >我试图熟悉cin.get()。 ***忘了发起myGolf,myGolf = {“某个名字”,6};但在这里无关 – Stasv 2013-02-18 19:38:23

回答

1

当你只是按在第一个提示符下输入发生此问题。输入流被标记为eof,一个错误条件标志(这就是为什么它返回0)。输入流然后停止工作。

看来,你正在使用一种旧的 C++,ISO ISO 1998,而我不认为你需要这样做。但是,如果你要坚持你的方法,然后执行以下操作:后cin.getline()(不需要任何回报)写:cin.clear(); cin.sync();,如下:

void setGolf(Golf &g) 
{ 
    cout << "Enter a name for the golf structure:" << endl; 
    getline(cin, g.fullname)); 

    cin.clear(); 
    cin.sync(); 
} 

现在,关于现代化你的代码。首先,您可以使用标准库的类string,它可以存储字符串字面值,甚至在需要时增长,而不会给出字符的最大值。这有点令人困惑,因为你包括标题string,它将包含该类,但是你没有使用它。使用string还具有其他优点,例如自动纠正可能发生在您的Golf结构中的潜在缓冲区溢出。所以,我会改变你的结构是:

struct Golf{ 
    string fullname; 
    int handicap; 
}; 

现在你可以使用getline(),在utility,读取一整行,并将其存储在string,做所有的魔法为您服务。所以,你可以改变你的golf.cpp文件,到:

#include <utility> 

//... 

void setGolf(Golf &g) 
{ 
    cout << "Enter a name for the golf structure:" << endl; 
    getline(cin, g.fullname)); 
} 

现在你也可以改变返回类型为void。在使用getline()时不太可能遇到任何错误。无论如何,考虑到你可以返回bool(布尔类型),这是一个内置类型,文字为truefalse

我敢肯定,你可以改变你的main()现在,一个简单的样式:

int main() 
{ 

    Golf myGolf; 

    setGolf(myGolf); 
    showGolf(myGolf); 

    setGolf(myGolf); 
    showGolf(myGolf); 

    return 0; 
} 

最后,你可以考虑在一个类中封装的信息,而不是一个结构,但是这是一个完全不同的问题。

希望这会有所帮助。

+0

Baltasarq - >感谢您的精彩提示!是的#include 是重复的。不幸的是,我真的想用cin.get()来理解这个特定的问题。你会碰巧知道为什么会出现上述问题?我有一个全新的visual studio 2010安装,如果默认安装了过时的标准版,它会让我觉得奇怪 – Stasv 2013-02-18 20:07:30

+0

当您在第一个提示中按回车键时,会发生此问题。输入流被标记为eof和错误条件标志(这就是为什么它返回0)。输入流然后停止工作。如果你想坚持你的方法,那么做下面的事情:在cin.getline()之后(不需要返回任何东西)写:cin.clear(); cin.sync(); – Baltasarq 2013-02-18 20:18:46

+0

Baltasarq - >谢谢!我试过cin.get(); cin.clear()提前。但cin.sync();诀窍!我会读一些关于它。我会很乐意将您的答案标记为正确的,只要将您的评论发布为答案即可。 – Stasv 2013-02-18 20:27:14