In C++ Write a program that manages eight student’s information. Prompt the user
ID: 3741060 • Letter: I
Question
In C++ Write a program that manages eight student’s information. Prompt the user for a data filename. Read the last name, first name, and 10 test scores for 8 students. Calculate each student’s average and grade. Determine the highest and lowest average for the class. Display information to the user.
Use a structure to hold all of the student information. The main function should be short and mainly consist of function calls.
Sample Run #1: (the highlighted text is what the user types) `
Filename? Struct_EightStudents.txt
Student Average Grade
Jones, Bob 80 B
Johnson, Joe 80 B
Williams, Ben 80 B
Brown, David 70 C
Taylor, Tom 80 B
Clark, John 80 B
Lee, Jane 80 B
Smith, Sue 90 A
Highest Avg 90
Lowest Avg 70
When printing the results:
name should be left-justified, 15 characters wide
average should be right-justified, 4 characters wide
print 8 spaces
print the grade
Explanation / Answer
// StudentScores.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <iomanip>
struct StudentDetails{
std::string first_name;
std::string last_name;
std::vector<int> test_scores;
};
StudentDetails GetDetails(std::string line)
{
StudentDetails details;
std::string delimiter = ",";
size_t pos = 0;
size_t count = 0;
std::string token;
while ((pos = line.find(delimiter)) != std::string::npos) {
token = line.substr(0, pos);
if (count == 0)
{
details.last_name = token;
}
else if (count == 1)
{
details.first_name = token;
}
else
{
details.test_scores.emplace_back(std::stoi(token));
}
line.erase(0, pos + delimiter.length());
count++;
}
return details;
}
int CalcAverage(std::vector<int> scores)
{
int total_score = 0;
for (int score : scores)
{
total_score += score;
}
return total_score / scores.size();
}
char GetGrade(int average)
{
if (average >= 90)
return 'A';
if (average >= 80 && average < 90)
return'B';
else
return 'C';
}
int _tmain(int argc, _TCHAR* argv[])
{
std::vector<StudentDetails> student_details;
std::cout << "Filename?";
std::string file_name;
std::cin >> file_name;
std::string line;
try
{
std::ifstream student_file(file_name);
while (std::getline(student_file, line))
{
student_details.emplace_back(GetDetails(line));
}
}
catch (std::exception ex)
{
std::cout << "Exception occured.";
}
int highest_avg = 0;
int lowest_avg = CalcAverage(student_details[0].test_scores);
std::cout << "Student Average Grade";
for (auto student : student_details)
{
int avg = CalcAverage(student.test_scores);
std::cout << std::setw(15) << student.last_name + " " + student.first_name << " " << std::setw(4) << avg << std::setfill(" ") << std::setw(8) << GetGrade(avg) << std::endl;
if (avg > highest_avg)
highest_avg = avg;
if (avg < lowest_avg)
lowest_avg = avg;
}
std::cout << std::endl << "Highest Avg " << highest_avg << std::endl;
std::cout << "Lowest Avg " << lowest_avg;
return 0;
}