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;
}
|