【问题标题】:c++ reading text file and output grades and average to another file (messed up output) [duplicate]c ++读取文本文件和输出成绩并平均到另一个文件(输出混乱)[重复]
【发布时间】:2012-01-15 09:50:00
【问题描述】:

可能重复:
enhancing a program - complete failure

我要求编写一个程序来读取文本文件的内容,格式如下:

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 的成绩时。例如:

琼斯·汤姆 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 是学生的平均分。

谢谢。 亲切的问候。

【问题讨论】:

  • 我希望这不是家庭作业!除此之外,我发现你在阅读以 '\n' 开头的行后调用 clear 很奇怪。您需要更加小心地检查空行和空白行。
  • @batbrat:是的,这是作业。这是previous question的后续行动。
  • @Blastfurnace 感谢您告诉我。我应该意识到的!也许我不应该发布如此详细的答案并指出他更多阅读并推动他寻求解决方案。不幸的是,它已经完成了。

标签: c++ file text


【解决方案1】:

嗯,解决方案非常简单。首先,将代码分成两部分,第一部分读取成绩,进行平均并将成绩写入输出文件,第二部分写入零和平均值。

让读取分数的部分将它们累积到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();

我希望这会有所帮助。

【讨论】:

  • 我从哪里获得 的值?
  • 正如我所说,当您从原始文件中读取元素时,您需要计算读取的值的数量。我将补充说明。
  • 好的,请和我一起坚持下去,因为我在循环方面完全是个菜鸟
  • 这真的很明显,如果你想一想。无论如何,我已经添加了说明。
  • 我仍然不明白 代表什么?是多少学生还是多少年级?感谢您的帮助
【解决方案2】:

您计算每行平均值的算法不正确,对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
    }
}       

【讨论】:

    猜你喜欢
    • 2020-03-08
    • 2013-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-27
    • 1970-01-01
    • 2019-08-07
    • 1970-01-01
    相关资源
    最近更新 更多