Программа пропускается через GetLine (), не принимая пользовательский ввод [дубликат
Вопрос
Этот вопрос уже имеет ответ здесь:
Это очень странная проблема, когда моя программа просит пользователя по этому адресу, вместо того, чтобы ждать ввода, кажется, полностью пропускает функцию 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.cegline () и getline () делают то же самое.
Я использую MVC ++ 2008.
Все поля в массиве рекорда являются структурой, запись [applumber] .address - это символ Char.
Решение
CIN, вероятно, покидает возвращение каретки в буфер, который GetLine извлекает. Пытаться
cin.ignore(1000, '\n');
cin.getline(Record[Entrynumber].Address,70);
Оператор >> не удаляет новенький символ после получения данных, но игнорирует ведущее пробелование перед получением данных, в то время как GetLine просто извлекает все возможное, и удаляет « n» после прочтения, так как он отличается от линии, 'получающий'.
Другие советы
Видя как как в буфере могут быть введены в буфер, который ваш GetLine является чтение первым, я бы предложил вам очистить буфер, прежде чем пытаться ввести следующие данные:
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');