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");
}
The flow of your reading code is weird. You start by reading
EnrolmentNo,moduleCodeandMarks. Then, if it’s the module you’re interested in, you start readingMarksfrom the same spot in the file, until a non-integer is found.With the example file you gave (and assuming user enters
IS1S01asmodule), the read proceeds like this:EnrolmentNogets the first 11moduleCodegets IS1S01Marksgets 25Then the inner loop kicks in and:
Marksgets the next 11 (from line 2)Markstries to read “SE1S02”, and failsGiven the file you’ve shown, you should probably get rid of the inner
forloop and do the sum/averaging in the outer loop.