Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The client (i.e. main()) should read in the contents of the file. After each grade is read in, it should call the addGrade member function in the Student class to add the new grade (i.e. one grade at a time) to the vector. [Do not create a vector in main and pass the entire vector in to the addGrade function in the Student class. Pass in only one grade at a time and allow the addGrade function to add it to the vector of grades in the class.]
Main() should then produce a report that displays the student’s name and course, the total number of grades in the file, the lowest grade, the highest grade, the average of all the grades, and finally, a listing of all of the grades that were read in. The listing of all of the grades must be displayed in sorted order (ascending – from lowest to highest).
All output should be labeled appropriately, and validity checking should be done on input of the filename and also the grades that are read in.

What I have tried:

MyCode and other files.
C++
<pre>
#ifndef Student_H_
#define Student_H_
#include <iostream>
#include <vector>
#include <algorithm>

class Student // class

{

private: // data members 

	std::vector<int> grades;

	std::string studentName;

	std::string courseName;

public: // functions 

	Student(std::string studentName, std::string course); // constructor 

	~Student();

	void addGrade(int Grade);

	void sortGrade();

	float averageGrade();

	int lowestGrade();

	int highestGrade();

	int gradeCount();

	void displayGrades();

	const std::string& getstudentName() const
	{
		return studentName;
	}

	void setStudent(const std::string& student)
	{
		studentName = student;
	}

	const std::string& getCourse() const
	{
		return courseName;
	}

	void setCourse(const std::string& course)
	{
		courseName = course;
	}

};

#endif
#include "Student.h"

#include <iostream>

// constructor

Student::Student(std::string student, std::string course)
{
	studentName = student;
	courseName = course;
}

// destructor

Student::~Student()
{}


//Function to add a grade to vector

void Student::addGrade(int grade)
{
	// adding grade to vector
	this->grades.push_back(grade);
}


//Function to sort vector

void Student::sortGrade()
{
	sort(grades.begin(), grades.end());
}


float Student::averageGrade()
{
	float avg = 0.0;

	for (size_t i = 0; i < grades.size(); i++)
	{
		avg += grades[i];
	}

	if (grades.size() > 0)
		return (avg / grades.size());

	return 0;
}

// Find lowest grade of a student

int Student::lowestGrade()
{
	int lowGrd = 0;
	if (grades.size() > 0)
	{
		lowGrd = grades[0];

		for (size_t i = 1; i < grades.size(); i++)

		{

			if (lowGrd > grades[i])

			{
				lowGrd = grades[i];
			}
		}
	}
	else
	{
		std::cout << "No grades for " << studentName << std::endl;
	}

	return lowGrd;
}

int Student::highestGrade()
{
	int highGrd = 0;

	if (grades.size() > 0)
	{
		highGrd = grades[0];

		for (size_t i = 1; i < grades.size(); i++)
		{
			if (highGrd < grades[i])
			{
				highGrd = grades[i];
			}
		}
	}
	else
	{
		std::cout << "No grades for " << studentName << std::endl;
	}

	return highGrd;
}

int Student::gradeCount()
{
	return grades.size();
}


void Student::displayGrades()
{
	std::cout << "The grades for " << studentName << " - " << courseName << std::endl;

	sortGrade();

	for (size_t i = 0; i < grades.size(); i++)
	{
		std::cout << grades[i] << std::endl;
	}
}
#include "Student.h"
#include <iostream>
#include <fstream>

using namespace std;



int main()

{
	Student* grade = NULL;
	string fileName;
	string studentName, course;

	cout << "Enter input file name : ";
	cin >> fileName;

	ifstream infile;
	int num = 0;
	infile.open("integers.txt");
	if (infile.fail())
	{
		cout << "Unable to open file: " << fileName << "... Exiting program" << endl;
	}

	else
	{
		grade = new Student(studentName, course);
		while (!infile.eof())
		{
			grade->addGrade(num);
		}

		cout << grade->gradeCount() << " grades were recorded for this course" << endl << endl;

		cout << "Statistics" << endl;
		cout << "Average: " << grade->averageGrade() << endl;
		cout << "Lowest Grade : " << grade->lowestGrade() << endl;
		cout << "Highest Grade : " << grade->highestGrade() << endl << endl;



		grade->displayGrades();
	}

	system("pause");

	return 0;
}
Posted
Updated 9-Jul-21 21:53pm
v2
Comments
Rick York 9-Jul-21 19:57pm    
You omitted one very important piece of information. WHAT "shows recourse file is not available" ? In other words, what is IT?
bhushan7 9-Jul-21 21:01pm    
The file is contain
90 85 97 91 87 86 88 82 83
merano99 10-Jul-21 7:06am    
As I wrote hours ago, the file is not read at all. See my solution below.

1 solution

Does not compile.
Here are some places you should change.

missing:
C++
#include <string>

change:
C++
std::cout << "No grades for " << studentName.c_str() << std::endl;

A part is also missing:
C++
// ??
grade = new Student(studentName, course);
while (!infile.eof()) {
  // ??
  grade->addGrade(num);
  }

The studentName, course and num variables are empty and nothing is read from the file.

I would expect something like:
C++
while (infile >> num) {
  grade->addGrade(num);
  }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900