2012-01-15 46 views
-3

可能重复:
enhancing a program - complete failureC++阅读文本文件和输出等级和平均到另一个文件(搞砸输出)

IM要求写一个程序来读取的内容文本文件的形式为:

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 

并将每个学生的平均值输出到另一个fil中E在形式:

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是学生的平均值。

谢谢。 亲切的问候。

+2

我希望这不是功课!除此之外,我发现在阅读以'\ n'开头的行后你会打电话清楚。你会希望在检查空白行和空白行时更加小心。 – batbrat 2012-01-15 10:00:03

+0

@batbrat:是的,这是作业。这是[前一个问题]的后续(http://stackoverflow.com/questions/8862058/enhance-a-program-complete-failure)。 – Blastfurnace 2012-01-15 10:17:53

+0

@Blastfurnace谢谢你告诉我。我应该意识到它!也许我不应该发布这样一个详细的答案,并指出他更多的阅读,并推动他寻求解决方案。不幸的是,它已经完成了。 – batbrat 2012-01-15 10:25:28

回答

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(); 

我希望这可以帮助了。

+0

我从哪里得到的值? – LebTech 2012-01-15 10:06:07

+0

正如我所说的,当您从原始文件中读取元素时,您需要计算读取的值的数量。我会添加一个澄清。 – batbrat 2012-01-15 10:07:39

+0

好吧请挂在那里与我一起,因为我是一个完整的noob当涉及到循环 – LebTech 2012-01-15 10:10:41

1

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