gpt4 book ai didi

c++ - 程序在不接受用户输入的情况下跳过 Getline()

转载 作者:搜寻专家 更新时间:2023-10-31 00:22:05 25 4
gpt4 key购买 nike

<分区>

这是一个很奇怪的问题,当我的程序向用户询问地址时,它没有等待输入,而是完全跳过了 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 数组中的所有字段都是结构体,Record[Entrynumber].Address 是一个 char 数组。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com