【发布时间】:2018-04-17 12:35:39
【问题描述】:
我正在制作一个简单的程序来找出不以 using 文件处理开头的行数。我无法理解为什么我的循环只运行了 4 次,而它显然应该比行的值少 0 到 1。这里有什么问题?
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
const int s = 80;
char str[s];
int lines = 0, a = 0, alines = 0;
ofstream fout("file6.txt", ios::out);
cout<<"Enter the number of lines you want to enter: ";
cin>>lines;
for(int i = 0; i < lines; i++)
{
cin.getline(str, 80);
fout<<str<<endl;
}
return 0;
}
【问题讨论】:
-
"不以使用文件处理开头的行数。" 用什么?看起来您在此处遗漏了一些重要信息。
-
格式化输入不读取行尾字符。第一个 getline 执行,然后下一个 getline 实际上等待您输入。我想这是你的问题?
标签: c++ loops file-handling