【问题标题】:Program is skipping over Getline() without taking user input [duplicate]程序在不接受用户输入的情况下跳过 Getline() [重复]
【发布时间】:2011-04-13 11:46:46
【问题描述】:

这是一个很奇怪的问题,当我的程序向用户询问地址时,而不是等待输入,似乎完全跳过了getline()函数

Answerinput:

cout << "would you like to add another entry to the archive? (Y/N):";

cin >> answer;

cout << endl;
cout << endl;

answer = toupper(answer);


 switch(answer)
    {
    case 'Y':
        Entrynumber++;

        cout << "began record number " << Entrynumber << "+ 1." << endl;

        cout << "Enter the last name of the person to be entered" << endl;

        cin >> stringentry;
        cout << endl;

        stringlength = stringentry.length();

        strcpy(Record[Entrynumber].Last_Name, stringentry.c_str());


        Record[Entrynumber].Last_Name[stringlength] = '*';



        cout << "Enter the first name of the person" << endl;

        cin >> stringentry;
        cout << endl;

        stringlength = stringentry.length();

        strcpy(Record[Entrynumber].First_Name, stringentry.c_str());

        Record[Entrynumber].First_Name[stringlength] = '*';

        cout << "Enter the SSN of the person" << endl;
        cin >> Record[Entrynumber].SSN;
        cout << endl;

        cout << "Enter the age of the person" << endl;
        cin >> Record[Entrynumber].Age;
        cout << endl;

        cout << "Enter the address of the person" << endl;


        cin.getline(Record[Entrynumber].Address,70);


        cout << endl;


        stringentry = Record[Entrynumber].Address;

        stringlength = stringentry.length();



        Record[Entrynumber].Address[stringlength] = '*';

        cout << "you entered:" << endl;



        for(jim = 0 ; Record[Entrynumber].Last_Name[jim + 1] != '*' ; jim++)
        {
            cout << Record[Entrynumber].Last_Name[jim];
        }

        cout << ',' ;


        for(jim = 0 ; Record[Entrynumber].First_Name[jim + 1] != '*' ; jim++)
        {
            cout << Record[Entrynumber].First_Name[jim];
        }

        cout << endl;

        cout << Record[Entrynumber].SSN << endl;
        cout << Record[Entrynumber].Age << endl;

        for(jim = 0 ; Record[Entrynumber].Address[jim + 1] != '*' ; jim++)
        {
            cout << Record[Entrynumber].Address[jim];
        }
        cout << endl;
        cout << endl;


        goto Answerinput;
    case 'N':
        cout << "ok" << endl;
        break;
    default:
        cout << "invalid answer" << endl;
        goto Answerinput;
    }

输出到控制台

would you like to add another entry to
the archive? (Y/N):Y

began record number 6+ 1. 


 Enter the last name of the person to be entered 
 John


 Enter the first name of the person 
 John

 Enter the SSN of the person  22222222

 Enter the age of the person  22

 Enter the address of the person

 you entered: 
Joh,Joh 
22222222 
22
 *¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦
 //////////////22 more lines of'|'//////////////////////////////////////////////
 ... 
¦¦¦¦¦¦¦¦l3-j

 would you like to add another entry to the archive? (Y/N):

cin.getline() 和 getline() 都做同样的事情。

我正在使用 MVC++ 2008。

Record数组中的所有字段都是structs,Record[Entrynumber].Address是一个char数组。

【问题讨论】:

    标签: c++ iostream getline


    【解决方案1】:

    Cin 可能将回车留在 getline 检索的缓冲区中。试试看

    cin.ignore(1000, '\n');
    
    cin.getline(Record[Entrynumber].Address,70);
    

    >> 运算符在检索数据后不会删除换行符,但会在检索数据之前忽略前导空格,而 getline 只是检索其中的任何内容,并在读取后删除 '\n',因为它是线是“得到”。

    【讨论】:

    • 有效!谢谢!为什么剩余的输入会进入这个 getline() 函数?
    • @jwaffe 查看更新后的答案
    【解决方案2】:

    鉴于您的 getline 首先读取的缓冲区中可能存在剩余输入,我建议您在尝试输入下一个数据之前清除缓冲区:

    std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
    

    【讨论】:

    • 这对我来说效果很好。在我的例子中,我提示用户将数据输入到 ADT 数组中。非常令人沮丧,因为在它之前的那个函数中真的没有别的东西——它只是跳过了第一个输入。我使用开关将它作为菜单选项 - 所以它正在拾取输入选择 0.o 所需的回车键,这对我来说似乎是一个明显的缺陷......更新:虽然在我的情况下,似乎只需要 cin .ignore();
    猜你喜欢
    • 2016-01-13
    • 1970-01-01
    • 2021-11-26
    • 2016-02-05
    • 1970-01-01
    • 2017-02-13
    相关资源
    最近更新 更多