程序正在跳过Getline()而不接受用户输入 [英] Program is skipping over Getline() without taking user input

查看:127
本文介绍了程序正在跳过Getline()而不接受用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is a very strange problem, when my program asks the user for the address, instead of waiting for input, it seems to skip the getline() function completely

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



输出到控制台

output to console

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()

Both cin.getline() and getline() do the same thing.

我正在使用MVC ++ 2008。

I'm using MVC++ 2008.

Record数组中的所有字段都是结构体, EntryInumber] .Address是一个字符数组。

All of the fields in the Record array are structs, Record[Entrynumber].Address is a char array.

推荐答案

Cin可能在getline检索缓冲区中返回。尝试

Cin is probably leaving the carriage return in the buffer which getline retrieves. Try

cin.ignore(1000, '\n');

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

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

The >> operator doesn't remove the newline character after retrieving data, but ignores leading whitespace before retrieving data, while getline just retrieves whatever is in there, and removes the '\n' after reading as it is apart of the line it is 'getting'.

这篇关于程序正在跳过Getline()而不接受用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆