Class Averages Problem help

Hello! I was given this problem for my programming class, and I can already tell it's very detailed. I'm new to C++, so I'm hoping that I can get some help step by step, as just with getting started I'm really lost. I'm hoping someone can help me along the way! Here is the problem I was given:

Write a C++ program that will prompt the user for the name of a data file. This data file contains a list of student names (unknown number) in a class. The names are listed, one per line, in the following format: lastName firstName middleInitial and each part of the name is separated by a single space.

Each student has a data file which contains an unknown number of test scores. The name of each student’s data file is created by concatenating the student's first, middle initial and last name and then appending the suffix “.dat”. For example, if the student's name is Bobby B Baylor, the data file with this student's grades is called BobbieBBaylor.dat. If the input file cannot be found, then display "Cannot open " + filename.

Your program should open each student’s data file, read in the scores and calculate the student’s average. In addition to calculating the average for each student, the program should report the following statistics for the class : total number of students, average, maximum and minimum of the students' averages. If a student grade file is missing, then print "Cannot open " + student file name" and do not include the student's info in the class statistics.

Note: All statistics should be rounded to the nearest hundredth.

cout << fixed << setprecision(2);

Example:

Data File: students.dat

Flintstone Fred D

Rubble Barney E

Slate Walter A

FredDFlintstone.dat

85

88

92

86

77

32

BarneyERubble.dat

95

72

88

89

WalterASlate.dat

68

75

83

Sample Program Output #1 (bad file name):

Enter name of data file: stduent.dat

Cannot open stduent.dat


Sample Program Output #2 (all student files present):



Enter name of data file: students.dat<enter>

Student Statistics

Fred D. Flintstone : 76.67

Barney E. Rubble : 86.00

Walter A. Slate : 75.33

Class Statistics

Total Students : 3

Class Average : 79.23

Max Average : 86.00

Min Average : 75.33

Sample Program Output #3 (one student file missing):

Enter name of data file: students.dat

Student Statistics

Fred D. Flintstone : 76.67

Barney E. Rubble : 86.00

Walter A. Slate : Cannot open WalterASlate.dat

Class Statistics

Total Students : 2

Class Average : 80.40

Max Average : 86.00

Min Average : 76.67

Output formatting help:

1) create a constant called HEADER_WIDTH, assign the value to 20 and use it to set the width.

2) example for class statistics

cout << "\t" << setw(HEADER_WIDTH) << "Total Students" << ": " << totalStudents << endl;

3) Displaying the student's full name requires setting a local variable to build the name (including the "." for the MI) and then using it a cout statement like this.

cout << "\t" << left << setw(HEADER_WIDTH)

<< studentDisplayName << ": "

<< fixed << setw(2) << setprecision(2) << currentAverage

<< endl;




My teacher even gave us what she thinks is a good way to outline this problem:

1. Initialize counters / accumulators
a. Number of students
b. Summary of class grades
c. Number of class grades
2. Prompt user for file name for the file with the list of student names
3. If file cannot be opened, then display “Cannot open “ message
4. Else
a. Loop through each line in input file until all students are processed
i. Format file name for student
ii. Print student name (no new line yet)
iii. Attempt to open student grade file
iv. If file cannot be opened, then display “cannot open” message
v. Else
1. Increment number of students
2. Reset student grade sum and counter to 0
3. Loop through student’s grades until all grades are processed
a. Sum the grades
b. Count the number of grades
4. Calculate student average
5. Display student average
6. Assign maximum average if 1st student or current average is higher
7. Assign minimum average if 1st student or current average is lower
8. Increment summary of class grades
9. Increment number of class grades
b. Calculate class average
i. If grade count > 0, calculate average ( sum of all grades / total number of grades)
c. Display the class statistics
i. If grade count = 0, display “No students”
ii. Else display the statistics
1. Number of students
2. Class average
3. Maximum average
4. Minimum average

I've only gotten started, but I'm already getting an error message when trying to input the file-any help is appreciated! Here's what I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>

//Initialize counters / accumulators
int nunberofstudents;
int summaryclassgrades;
int numberclassgrades;

using namespace std;

int main()
{
    ifstream filename;
    ifstream infile;

    //Prompt user for file name for the file with the list of student names
    cout >> "Enter name of data file:";
    cin << filename << endl;
    cout >> filename >> endl;

    return 0;
}
A file name is not the same as the file object. filename can be a string or char[20] or char*
When I change 'filename' to a string, I still get this error on line 17, 18 and 19.

error: invalid operands to binary expression ('ostream' (aka 'basic_ostream<char>') and 'const char [25]')
Something like this to start?
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <string>
#include <fstream>

class Student
{
  std::string         m_lastName;
  std::string         m_firstName;
  std::string         m_middleInitial;
  std::vector<double> m_scores;
  double              m_average;
  
public:
  Student( std::string line );
  void parse();
  
private:
  void readScores(std::ifstream& fin);
  void calculateAverage();
};

class Class
{
  std::string          m_filename;
  std::vector<Student> m_students; 
public:
  void promptFile();
  void parse();
  
private: 
  void addStudent( std::string line );
};


#include <iostream>
#include <sstream>

void Class::promptFile()
{
  std::cout << "Filename: ";
  std::cin >> m_filename;
}

void Class::parse()
{
  std::ifstream fin( m_filenmae );
  std::string line;
  
  std::getline( fin, line ); 
  while ( fin.good() )
  {
    addStudent( line );
	std::getline( fin, line );
  }
}

void Class::addStudent(std::string line)
{
  try
  {
    Student s(line);
    s.parse();
	m_students.push_back(s);
  }
  catch (... )  { }
}

Student::Student(std::string Line)
{
  std::stringstream iss(Line);
  std::getline( iss, m_lastName, ' ');
  std::getline( iss, m_firstName, ' ');
  std::getline( iss, m_middleInitial, ' ');
}

void Student::parse()
{
  std::string filename = m_firstName + m_middleInitial + m_lastName + ".dat";
  
  std::ifstream fin(filename);
  
  if (! fin.is_open() )
  {
    std::cout << "Cannot open " << filename << std::endl;
	throw 1;
  }
  
  readScores( fin );
  calculateAverage();
}

void Student::readScores(std::istream& fin)
{
  std::copy( std::istream_iterator<double>(fin), std::istream_iterator<double>(), std::back_inserter(m_scores) );
}

void Student::calculateAverage()
{
  m_average = 0.0;
  for (std::vector<double>::iterator it = m_scores.begin(); it != m_scores.end(); ++it)
    m_average += *it;
  m_average /= m_scores.size();
}
Thanks for the help, but what you submitted honestly makes no sense to me-I think I'd prefer if I just got some help on how I'm writing my code, so I can use the format my professor has taught us.

Still need some help because I'm getting that error message from what I posted above.
Well, I found the problem, my ">>s" were going the wrong way
Okay, I've managed to get through the next part, now I'm having some trouble with starting the loop-can anyone help? Here's what I have so far:

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
//Initialize counters / accumulators
int nunberofstudents;
int summaryclassgrades;
int numberclassgrades;

using namespace std;

int main()
{
    char fileName [50];
    ifstream inputFile;
    inputFile.open ("students.dat");
    //Prompt user for file name for the file with the list of student names
    cout << "Enter name of data file:" << endl;
    cin >> fileName;
    cout << fileName << endl;

    if (inputFile.fail())
    {
        cout << "Cannot open student.dat" << endl;

    }
    else
    {

    }


    return 0;
What's the point of asking for the filename that you already opened?
That's just the format my professor asked for-see the example in my first post.
If you read the list of steps, you do 3 then 2
Oh, good catch, thanks! I think where I need the most help is with the loop that comes next-I guess I'm just having trouble picturing what it'll look like in my head. Any advice?
Still looking for help on this, please~
If anyone can still help me with the problem above, I would appreciate it! I'm having the most trouble with the loop, this part of the outline:

a. Loop through each line in input file until all students are processed
i. Format file name for student
ii. Print student name (no new line yet)
iii. Attempt to open student grade file
iv. If file cannot be opened, then display “cannot open” message
v. Else
1. Increment number of students
2. Reset student grade sum and counter to 0
3. Loop through student’s grades until all grades are processed
a. Sum the grades
b. Count the number of grades
4. Calculate student average
5. Display student average
6. Assign maximum average if 1st student or current average is higher
7. Assign minimum average if 1st student or current average is lower
8. Increment summary of class grades
9. Increment number of class grades


My professor sent me an email that the first part of the loop should look like this:

students.open(studentsFileName.c_str());
...

while (students >> lName >> fName >> mInitial)
{
// format student grade file name
// open student grade file
while (studentGrade >> grade)
// process student grade file
}

students.close();

I just need someone to help me get started, if possible, because I'm pretty lost.
Topic archived. No new replies allowed.