Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 程序正在跳过Getline(),而不接受用户输入_C++_Iostream_Getline - Fatal编程技术网

C++ 程序正在跳过Getline(),而不接受用户输入

C++ 程序正在跳过Getline(),而不接受用户输入,c++,iostream,getline,C++,Iostream,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(answe

这是一个非常奇怪的问题,当我的程序向用户询问地址时,它似乎没有等待输入,而是完全跳过了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;
    }
getline()和getline()都做同样的事情

我正在使用MVC++2008


记录数组中的所有字段都是结构,记录[Entrynumber]。地址是字符数组。

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');
std::cin.ignore(std::numeric_limits::max(),'\n');

它可以工作!谢谢为什么剩余的输入会进入这个getline()函数?@jwaffe请看更新的答案这对我来说很有效。在我的例子中,我提示用户将数据输入ADT数组。非常令人沮丧,因为在它之前的函数中实际上没有其他东西-它只是跳过了第一个输入。我把它作为一个使用开关的菜单选项,所以它选择了输入选择0所需的回车键。o对我来说似乎是一个明显的缺陷。。。更新:虽然在我的例子中,似乎只需要cin.ignore();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');