c++读取文本文件并输出等级和平均值到另一个文件(输出混乱)
c++ reading text file and output grades and average to another file (messed up output)
可能重复:
增强程序完全失败
我要求编写一个程序来读取文本文件的内容,格式为:
Jones Tom 94 99 96 74 56 33 65 89 87 85
Thompson Frank 67 58 86 95 47 86 79 64 76 45
Jackson Tom 95 97 94 87 67 84 99 45 99 87
Jackson Michael 43 23 34 77 64 35 89 56 75 85
Johnson Sara 84 93 64 57 89 99 74 64 75 35 91
并将每个学生的平均值以的形式输出到另一个文件中
Jones Tom 94 99 96 74 56 33 65 89 87 85 77.8
Thompson Frank 67 58 86 95 47 86 79 64 76 45 70.3
Jackson Tom 95 97 94 87 67 84 99 45 99 87 85.4
Jackson Michael 43 23 34 77 64 35 89 56 75 85 58.1
Johnson Sara 84 93 64 57 89 99 74 64 75 35 73.4
我成功地使用以下代码做到了这一点:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
using namespace std;
int main()
{
fstream infile("grades.txt",ios::in);
if(!infile){cerr<<"file could not be found!";exit(1);}
fstream outfile("average.txt",ios::out);
if(!outfile){cerr<<"file could not be created!";exit(1);}
char fname[20];
char lname[20];
int grades;
char c;
int lines=1;
double avg=0;
while(infile.get(c))
{if(c=='n') lines++;}
infile.clear();
infile.seekg(0);
for(int k=0;k<lines;k++)
{
infile>>fname;
infile>>lname;
outfile<<fname<<" "<<lname<<" ";
int sum=0;
for(int i=0;i<10;i++)
{
if(infile>>grades)
{sum+=grades;
outfile<<grades<<" ";}
}
outfile<<(double)sum/10.0<<endl;
}
system("pause");
return 0;
}
但我有个问题。当文本文件的第一行包含小于10的等级时。例如:
Jones Tom 94 99 96 74 56 33 65 89 87
我得到了一个混乱的输出,这正在毁掉一切。我得到以下输出:
Jones Tom 94 99 96 74 56 33 65 89 87 69.3
0
0
0
0
我该如何解决这个问题?如果每行的分数不到十个,我如何使程序输出零?例如,如果我在第一行有:
Jones Tom 94 99 96 74 56 33 65 89
我希望程序计算平均值,并将以下内容输出到另一个文件。
Jones Tom 94 99 96 74 56 33 65 89 0 0 64.3
注意64.3是学生的平均分。
谢谢。亲切问候。
好吧,解决方案真的很简单。首先,将代码分为两部分,第一部分读取成绩,进行平均并将成绩写入输出文件,第二部分写入0和平均值。
让读取分数的部分将其累积在sum
中。现在,您可以使用总和和计数器来计算平均值。
让我们看看读取分数的代码是如何工作的
//Repeat till all grades are read and then move on to the next student
num_grades_read = 0;
while //grades are available on the line
{
//Read in a new grade
sum += grades;
//Write the read grade to the output file.
outfile << grades;
num_grades_read++;
}
最后,在写入输出文件的第二部分中,您将希望执行以下操作:
int limit = 10 - num_grades_read;
for(int j = 0; j < num_grades_read; ++j)
{
//Write zero to the file
outfile << 0 << " ";
}
//Write the computed average to the file
outfile << (double)(sum) / num_grades_read << endl;
现在,上面的伪代码不是很好,因为它没有检查之类的。例如,你要确保你的阅读成绩不超过10个年级。
由于您在第一部分中遇到了问题,因此这里有一些代码可以帮助您:
while(!infile.fail())
{
infile >> grade;
sum += grade;
outfile<<grade<<" ";
num_grades_read++;
}
infile.clear();
我希望这能有所帮助。
您计算每行平均值的算法不正确,并且对clear()
和seekg()
有不必要的调用,因此您需要逐步移动文件。
此外,没有必要对分数进行硬编码。你可以计数这些!
我建议你用std::getline()
一次读一整行,用std::istringstream
读每个年级。这是从纯文本文件中读取数字的惯用方法,比一次读取一个字符要少得多!
由于这看起来像是家庭作业,或者至少是一个练习,所以我不会提供真正的代码。。。
while (std::getline(infile, line))
{
std::istringstream iss(line);
// Reset values for the number of grades and the total score here
if (iss >> fname >> lname) // Ensure that we've read in these values...
{
// Output the name and then iterate over the scores...
while (iss >> grade)
{
// Recalculate the total based on the new grade
// Increment the number of grades
// Output the current grade
}
// End of the line
// Mean = total / number of grades
// Output mean
}
}
- 有没有办法简单地从 GPU 调用多个 cpp 输出文件?
- VI工作室代码错误无法打开输出文件主.exe
- 在代码块中查找输出文件
- 将未签名的字符写入输出文件
- 为什么我的代码不写入输出文件?
- 完成搜索任务时为空输出文件
- 如何将地图<列表<string>、矢量<string>>输出到输出文件?
- 无法在代码块 IDE 中打开输出文件.exe
- C++:更改 IF 语句中的输出文件
- 编译 gsoap 输出文件 (onvif) 时出现问题
- C++击球平均值程序使用输入/输出文件打印名称,平均值,最高/最低平均值时遇到麻烦
- while 循环中的流输出文件() 通常生成空文件
- 如何使用 Docker 访问输出文件
- 提升进程间共享内存删除、权限和输出文件
- 查找素数,然后将质数放入输出文件中
- GDB / C++ - 根据循环条件将多个变量记录到输出文件中
- 带有文件结束函数的 while 循环重复输出文件中的最后一个数字两次
- 我的输出文件未显示解决方案
- 输入文件中的第一个字符是输出文件中的最后一个字符,反之亦然
- C++,我正在尝试创建一个有效的.bmp输出文件