2012-12-13 154 views
0

下面的代码应做到以下几点: 列出所有的学生采取模块,显示它做什么它想做好自己的商标和平均总分C++输出显示不正确的,“标记”不显示

然而,问题是,输出不显示的标记,反而显示注册号

我的标记文本文件

11 IS1S01 25 
11 SE1S02 50 
11 SE2S04 75 
12 CS3S08 15 
12 CS1S03 20 
13 CS1S03 25 
14 CS1S03 50 

,它应该显示如下内容:

11 IS1S01 25 

Average = 25 

当运行程序时,它反而显示以下内容:

11 IS1S01 11 

Average = 1.1 

它不是出于某种原因标记读取。我似乎无法找到为什么它这样做

void studentmark() 
    { 
     float Marks; 
     float Average; 
     float EnrolmentNo; 
     std::string module; 

    // Display message asking for the user input 
    std::cout << "\nList all students taking a module, showing their marks." << std::endl; 

    // List of options 
    std::cout << "\nInformation Resource Engineering: IS1S01" << std::endl; 
    std::cout << "C++ Programming:   SE2S552" << std::endl; 
    std::cout << "Data Structures and Algorithms:  CS2S504 " << std::endl; 
    std::cout << "AI for Game Developers:   CS3S08" << std::endl; 
    std::cout << "Cognitive Science:   MS3S28" << std::endl; 
    std::cout << "Game Modification:   CS1S03" << std::endl; 
    std::cout << "Building:    BE1S01" << std::endl; 
    std::cout << "Plumbing:    BE2S01" << std::endl; 
    std::cout << "Coaching:    SS1S02" << std::endl; 
    std::cout << "Psychology:    CC1S04" << std::endl; 
    std::cout << "Mental Health Care:   SS2S01" << std::endl; 
    std::cout << "Missing Module:    SE1S02" << std::endl; 

    std::cout << "\nEnter your preferred module:"; 

    // Read in from the user input 
    std::cin >> module; 

    // Read from text file and Display list of marks by each student in a particular module 

    std::ifstream infile;    // enable to open, read in and close a text file 

    infile.open("Mark.txt");   // open a text file called Mark 

    if (!infile)     
    { 
     std::cout << "List is empty" << std::endl;  // if the file is empty it output the message 
    } 
    else 
    { 
     std::cout << "\nList of marks for students: " << std::endl; 

     std::string moduleCode; 

     while (infile) 
     { 
      infile >> EnrolmentNo >> moduleCode >> Marks; 
      if (infile) 
      { 
       // strings read correctly 
       if (moduleCode == module) 
       { 
        //std::cout << " "; 
        std::cout << "\nEnrolmentNo" << "  " << "Module Code" << " " << "Marks" << std::endl; 

        int sum=0; 
        for(int i=0;i<10;i++) 
        { 
         if(infile>>Marks) 
         { 
          sum+=Marks; 
         } 
        } 

        std::cout << EnrolmentNo << "   " << moduleCode << "  " << Marks << std::endl; 

        float Average = (double)sum/10.0; 

        std::cout << "\nThe average mark of the module: " << Average << std::endl; 
       } 
      } 
     } 
    } 

    infile.close();   // close the text file 
    system ("PAUSE"); 
} 

回答

2

您的阅读代码的流程很奇怪。您首先阅读EnrolmentNo,moduleCodeMarks。然后,如果它是您感兴趣的模块,则从文件中的同一位置开始读取Marks,直到找到非整数。

随着你给的例子文件(并假设用户输入IS1S01module),读出进行这样的:

  1. EnrolmentNo获取第11
  2. moduleCode得到IS1S01
  3. Marks得到25

然后内环踢和:

  1. Marks获取下一个11(从2号线)
  2. 循环终止,因为Marks尝试读取 “SE1S02”,并未能

鉴于还有你的文件,你应该得到摆脱内部for循环,并在外部循环中进行求和/求平均值。