A programming Assigment Iive been given

I was given this as a programming assignment and honestly I am quite lost was hoping someone could help

Program Description:
You will open an input file “students.dat” that will contain a list of student names,
classification, and grade in the class. (All student info is completely made up ) You
should read through the entire input file using a loop- you will not know how many
students are in the file. After reading in all information, close the input file and print out
the following information with labels:
● Highest grade in the class
● Lowest grade in the class
● Class average grade (rounded to one decimal place)
● Number of freshmen students
● Number of sophomore students
● Number of junior students
● Number of senior students

Sample input:
Holly Berry freshman 88
Red Johnson sophomore 74
Jeff Bozo freshman 91
Pebble Johnston senior 82
Thomas BradyBunch freshman 63
Eddy Sheen junior 97
Sample output:

Mika Morgan
Highest grade: 97
Lowest grade : 63
Class average: 82.5
Freshmen: 3
Sophomores: 1
Juniors: 1
Seniors: 1
So what's your question?

- can you open a file?
- can you read one line from a file?
- can you read all the lines in a file?
In other words, just how far can you get into the problem before you get stuck.


If you want actual help, you need to make an actual effort to show where you can get to by yourself.


Note we're not here to just hand you a complete answer on a plate.
I am quite lost


About what? What don't you understand? How far have you got so far? Post the code you have so far.

Does the student always have first and last names?
As a starter, this will read and display the contents of the file (assuming a name always consists of first and last parts):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <utility>
#include <iomanip>

struct Student {
	std::string fname;
	std::string lname;
	std::string sType;
	int grade {};
};

std::istream& operator>>(std::istream& ifs, Student& s) {
	return ifs >> s.fname >> s.lname >> s.sType >> s.grade;
}

std::ostream& operator<<(std::ostream& os, const Student& s) {
	return os << std::setw(25) << std::left << (s.fname + ' ' + s.lname) << std::setw(15) << s.sType << " " << s.grade;
}

int main() {
	std::ifstream ifs("students.dat");

	if (!ifs)
		return (std::cout << "Cannot open file\n"), 1;

	std::vector<Student> studs;

	for (Student s; ifs >> s; studs.push_back(std::move(s)));

	for (const auto& s : studs)
		std::cout << s << '\n';
}



Holly Berry              freshman        88
Red Johnson              sophomore       74
Jeff Bozo                freshman        91
Pebble Johnston          senior          82
Thomas BradyBunch        freshman        63
Eddy Sheen               junior          97

Last edited on
Topic archived. No new replies allowed.