Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Having some coding issues. I can\'t seem to figure out why my function, readGrad

ID: 3884051 • Letter: H

Question

Having some coding issues.

I can't seem to figure out why my function, readGrades is not displaying on the screen just like the formatted file is. I only seem to keep getting a the first two digits to display from the file. The rows in the file are the numOfStudents and the columns in the file are the numberOfExams in the function readGrades. Please see screenshots provided.

Please explain what I am doing wrong, and do not just give me a code without any explanation. Please do not modify the variable names, nor the parameters in the void function.

#include <iostream>
#include <fstream>
#include <ctime>
#include <cstdlib>
#include <string>
using namespace std;

void readGrades(string, int, int);

int main()
{

ifstream grades;
string fileName;
int numberOfExams;
int numOfStudents;

grades.open("C:\Users\Home\Desktop\studentGrades.txt");
grades >> numOfStudents >> numberOfExams;

if (grades.fail())
{
  cout << "File not found";
  exit(1);
}

ifstream file(fileName.c_str());


readGrades(fileName, numberOfExams, numOfStudents);

grades.close();

system("pause");
return 0;
}

void readGrades(string fileName, int numberOfExams, int numOfStudents)
{
// This function should read in the grades from the file, and store it in a double-dimensional array, which should be a GLOBAL VARIABLE

// PLEASE DO NOT MODIFY THE PARAMETERS OF FUNCTION, MUST REMAIN AS SO.

ifstream file(fileName.c_str() );

for (int i = 0; i < numOfStudents; i++)
{
  file >> numOfStudents;
  file >> numberOfExams;
  cout << numOfStudents << ' ' << numberOfExams << endl;
}

}

100 70 100 70 100 7 100 70 1ee 70 100 70 100 70 1ee 70 10 7 100 70 100 70 100 70 100 70 100 70 100 70 100 7 100 70 100 70 100 7 100 70 10 70 100 70 100 70 100 70 100 7 100 70 100 70 100 7 100 70 100 70

Explanation / Answer

#include <iostream>
#include <fstream>
#include <ctime>
#include <cstdlib>
#include <string>
using namespace std;

void readGrades(string, int, int);

int main()
{

ifstream grades;
string fileName;
int numberOfExams;
int numOfStudents;

//grades.open("studentGrades.txt"); //<<-- studentGrades.txt does not content number of student and number of exam

fileName = "studentGrades.txt"
cout >> "Enter numOfStudents & numberOfExams: "
cin >> numOfStudents >> numberOfExams;
//     ^-------Enter them manually
if (grades.fail())
{
cout << "File not found";
exit(1);
}

//ifstream file(fileName.c_str());
// ^-----this line is of no use

readGrades(fileName, numberOfExams, numOfStudents);

// grades.close();

system("pause");
return 0;
}

void readGrades(string fileName, int numberOfExams, int numOfStudents)
{
// This function should read in the grades from the file, and store it in a double-dimensional array, which should be a GLOBAL VARIABLE

// PLEASE DO NOT MODIFY THE PARAMETERS OF FUNCTION, MUST REMAIN AS SO.

ifstream file(fileName.c_str());

for (int i = 0; i < numOfStudents; i++)
{
   for (int j = 0; j < numberOfExams; j++) {
       file >> marks[i][j]; // <<---- this is your global array of marks
   }
}
file.close();

}

Am sorry, but your code is not well stuctured, so couldnt fix it in a clean manner. I tried my best to fix it. If you need changes, please comment below.

Also NOTE that you cannot read number of student and number of exam from studentsGrade text file because the first line of the code does not contain that data.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote