简体   繁体   English

从文件和 output 平均读取成绩到另一个文件 C++

[英]Read grades from file and output average to another file C++

Hi I have been trying to work on a projedct to read grades from an input file and output the same grades and the average based on the first line which contains the total to another file and I am completely lost.嗨,我一直在尝试一个项目来从输入文件和 output 中读取等级,并且基于第一行的平均值,其中包含另一个文件的总数,我完全迷失了。 I have tried using getline() function to read in the line of grades displayed as name 7 8 9 10 then use the istringstream to read each value.我尝试使用 getline() function 读取显示为名称 7 8 9 10 的等级行,然后使用 istringstream 读取每个值。 I can't seem to ge tthe syntax right and my code will only getline every other line of grades and the string outputs all zeros....我似乎无法正确理解语法,我的代码只会每隔一行获得一次成绩,并且字符串输出全零......

this is one variation of what i've tried to read in the scores and at least print them out individually as integers这是我尝试在分数中读取并至少将它们单独打印为整数的一种变体

while (getline(fin,grades))
{
   getline(fin,grades);
   std::cout << grades;
   std::istringstream str(grades);
   int score;
   while (str>>score)
   {
      int s;
      str >> s;
      std::cout << score<< '\n';
   }

thanks so much for any help非常感谢您的帮助

Based on your description of the input file, I think what you have is correct except you don't need the getline again after the while loop, you also seem to be reading score twice on your second while loop, the int score should probably be changed to a double and you should change your couts to ofstream output.根据您对输入文件的描述,我认为您所拥有的是正确的,除非您在 while 循环之后不再需要 getline,您似乎也在第二个 while 循环中读取分数两次,int 分数可能应该是更改为双倍,您应该将 couts 更改为 ofstream output。 Using ofstream is just like using couts, it is like this:使用 ofstream 就像使用 couts 一样,是这样的:

 ofstream out(outputFile); // open the output file 
                            // for output average

out << grades << average(str) << "\n"; // output your stuff to the file in the while loop like this 
out.close(); // closing the file 

Then since you are confused with istringstream, I would use istringstream to read each number and compute the average using another function like this.然后,由于您对 istringstream 感到困惑,因此我将使用 istringstream 读取每个数字并使用另一个像这样的 function 计算平均值。 Here is my istringstream function to compute the average assuming the last thing being read is the total.这是我的 istringstream function 来计算平均值,假设最后读取的是总数。 Another approach would be since the total is already given to you, I would store all the inputs for the line in a vector or an array, and then get the last element divided by the size - 1 which should give you the average.另一种方法是,因为已经给了你总数,我将把所有输入的行存储在一个向量或一个数组中,然后得到最后一个元素除以大小 - 1 这应该给你平均值。

double average(istringstream& str){ // compute the average based on the line stream  
    double sum = 0; // sum of all the numbers in a line 
    int count = 0; // number of numbers in a line 
    double score; // score that is being read in the line 
    while(str>>score){ // can read a score
      sum += score; // add the scores to a sum 
      count++; // count the number of numbers in the line
    }
    sum = sum - score; // the last thing is the line is the total ? 
    count = count - 1; // assuming the last thing in the line is the total ? 
    return sum/count; 
}

Hope that will provide enough hints to do this.希望这将提供足够的提示来做到这一点。 Oh and don't forget to close the input streams and output streams.哦,别忘了关闭输入流和 output 流。 Thanks.谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM