Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8978815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:41:58+00:00 2026-06-15T19:41:58+00:00

The code below should do the following: List all students taking a module, showing

  • 0

The code below should do the following:
List all students taking a module, showing their marks and the average overall mark

It does what it suppose to do however, the problem is that the output doesn’t show the marks and instead show the enrollment number

My mark text file

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

Where it should display the following:

11   IS1S01   25

Average = 25

When it run the program, it display the following instead:

11   IS1S01    11

Average = 1.1

It not reading in marks for some reason. I can’t seem to find why it doing it

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");
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:41:59+00:00Added an answer on June 15, 2026 at 7:41 pm

    The flow of your reading code is weird. You start by reading EnrolmentNo, moduleCode and Marks. Then, if it’s the module you’re interested in, you start reading Marks from the same spot in the file, until a non-integer is found.

    With the example file you gave (and assuming user enters IS1S01 as module), the read proceeds like this:

    1. EnrolmentNo gets the first 11
    2. moduleCode gets IS1S01
    3. Marks gets 25

    Then the inner loop kicks in and:

    1. Marks gets the next 11 (from line 2)
    2. loop terminates, because Marks tries to read “SE1S02”, and fails

    Given the file you’ve shown, you should probably get rid of the inner for loop and do the sum/averaging in the outer loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The program I code below should List all payment made by each student, show
I am using code like the following to get a list of all documents
The code below takes an array value, if it's key exist it should echo
I suppose button should has Close title in the code below, but it has
Please see my pseudo-code below. The code comments should explain my problem. I'm new
The below code seems like it should work; however, the blob in the database
This code below allows me to find the word error in all my files
I written the following code to randomly select items n items from a list
I'm not sure how to map the following tables below in EF 4.1 code
See i have this following code below, at line no.9 i'm calculating some dates

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.