C++ String getline(cin,变量)函数正在跳过一些代码行

C++ String getline(cin,变量)函数正在跳过一些代码行,c++,C++,当我使用getline(cin,variablehere)函数时,我的程序跳过了一些代码。我不知道代码有什么问题。请参见下面的输出 #include <iostream> #include <string> using namespace std; int main() { string getfirstname; string lastname; string address; int contactnumber; cout &

当我使用getline(cin,variablehere)函数时,我的程序跳过了一些代码。我不知道代码有什么问题。请参见下面的输出

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string getfirstname;
    string lastname;
    string address;
    int contactnumber;
    cout << "Enter First name : ";
    getline(cin, getfirstname);
    cin.ignore();
    cout << "Enter Last name : ";
    getline(cin, lastname);
    cin.ignore();
    cout << "Enter Address : ";
    getline(cin, address);
    cin.ignore();
    cout << "Enter Contact number : ";
    cin >> contactnumber;
    cin.ignore();

    CurrentNumberOfContacts += 1;
    cout << "Successfully added to contact list!" << endl << endl;

    cout << "Would you like to add another contact ? [Y/N] ";
    cin >> response;

    //more lines of codes below
    return 0;
}
#包括
#包括
使用名称空间std;
int main()
{
字符串getfirstname;
字符串lastname;
字符串地址;
国际联络号码;

cout
cin>
getline
没有很好的配合。他们有不同的处理空白的策略。
getline
删除换行符,但
cin>
保留它。这意味着在您使用
cin>
读取某些内容后,将有一个换行符留在等待我n下一个
getline
要“使用”的输入流。这意味着它将在字符串中读取一个空行。

2件事。首先,在这种情况下,您不需要使用cin.ignore()

getline(). 
以前

cin >> variable
第二,我不知道你的程序为什么不运行,但我建议使用

getline() 

打电话查看是否有效。但我看不出代码不起作用的原因。

我建议删除
cin.ignore()
命令

用户输入的一个问题是
>
操作符没有将返回字符从流中取出,因此如果使用
getline()
跟随返回字符,则
getline()
将读取返回字符,而不是您要键入的字符

因此,我将您所有的
getline()
更改为:

// cin >> ws will skip any RETURN characters
// that may be left in the stream
getline(cin >> ws, lastname); 
同时删除所有
cin.ignore()
命令。当在
getline()
命令之后使用时,这些命令没有任何用处,如果您更改了
getline()
命令,如我所示,这些命令应该根本不需要

所以这应该有效:

int main()
{
    string getfirstname;
    string lastname;
    string address;
    char response;
    int contactnumber;
    int CurrentNumberOfContacts = 0;

    cout << "Enter First name : ";
    getline(cin >> ws, getfirstname);

    cout << "Enter Last name : ";
    getline(cin >> ws, lastname);

    cout << "Enter Address : ";
    getline(cin >> ws, address);

    cout << "Enter Contact number : ";
    cin >> contactnumber;

    CurrentNumberOfContacts += 1;
    cout << "Successfully added to contact list!" << endl << endl;

    cout << "Would you like to add another contact ? [Y/N] ";
    cin >> response;

    //more lines of codes below
    return 0;
}
否则,只需使用:

std::getline(cin, line);

@Galic提供的答案非常好,但如果您想在不丢弃前导空格的情况下读取一行字符,则需要另一种解决方案

你可以做:

char a='\n';
while (a=='\n')
{
    cin.get(a);
}
cin.unget();

在执行第一个getline之前。这假设没有由前一个cin产生的尾随空格,并且您的第一个输入行不是空的。

删除屏幕截图并将输出张贴在此处电话号码不是整数。您使用的是cin>>联系人号码而不是getline(cin,contactnumber)那就更不正确了。我确信
cin.ignore()是有原因的
在每次调用
getline
之后。我想不出它可能是什么。也许另一种方法是实际检查IO操作的成功/失败结果,而不是假设它们成功。只有在他使用
std::getline()时才这样做
直接在使用格式化输入之后。在这种情况下,它与格式化/非格式化输入无关。最终他将在一个循环中调用它,否则他不会费心使用他忘记包含声明的
CurrentNumberOfContacts
变量。@Wug当然可能会进入一个循环…someday,也就是这个答案,不准确,因为当使用
std::getline
时,它在某种程度上与其他文本模式流不同(它不是),可能是回答下一个问题的一个不错的起点;而不是这个问题。我不认为他暗示的是
cin
在某种程度上表现不同,而是混合格式化和非格式化输入通常是笨拙的。只使用
cin>>lastname
,会自动忽略e空格和函数对于典型的输入大小写是一样的(每行一个单词)。@Wug一些名字有空格(例如Mc-patteries或Du-Val)@Galik哦,是的。我把它和
scanf(“%c”
)混在一起了。
char a='\n';
while (a=='\n')
{
    cin.get(a);
}
cin.unget();