使用cin.getline时出现奇怪错误



任务要求是请求文本文件中输出的行数,然后逐行请求输入。每行包含两个并排的数字。示例文件如下:

475 6580 7590 8555 65

我试着写代码,然而,似乎有一行输入缺失。你们能帮帮我吗?感谢! !

int main (){

char file_name[81];
char buffer[81];
int lines;
cout << "input file name: ";
cin.getline(file_name,81);
cout << "input number of lines: ";
cin >> lines;
ofstream out_file(file_name);
if (!out_file)
{
    cout << "nerror, file already exists. choose another file name.";
    return 0;
}
while (lines != 0)
{
    cout << "enter first number: "; cin.getline(buffer, 81);
    out_file << buffer << "t";
    cout << "nenter second number: "; cin.getline(buffer ,81);
    out_file << buffer << endl;
    lines--;
}

out_file.close();
cout << "nprogram terminated";
cout << endl; return 0;

}

After

cout << "input number of lines: ";
cin >> lines;

添加
cin.ignore(1);

相关内容

  • 没有找到相关文章

最新更新