C++ C++;输出显示不正确,';标记';不显示

C++ C++;输出显示不正确,';标记';不显示,c++,visual-studio-2010,visual-c++,C++,Visual Studio 2010,Visual 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

以下代码应执行以下操作: 列出所有参加模块学习的学生,显示他们的分数和平均总分数

但是,问题是输出没有显示标记,而是显示注册号

我的标记文本文件

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");
}
void studentmark()
{
浮标;
浮动平均;
浮动注册号;
std::字符串模块;
//显示请求用户输入的消息

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

对于您给出的示例文件(假设用户输入
IS1S01
作为
module
),读取过程如下:

  • EnrolmentNo
    获取前11名
  • moduleCode
    获取IS1S01
  • 标记
    获得25分
  • 然后内循环开始,并:

  • 标记
    获取下一个11(来自第2行)
  • 循环终止,因为
    标记
    尝试读取“SE1S02”,但失败
  • 给定所显示的文件,您可能应该去掉内部
    for
    循环,并在外部循环中进行求和/平均