Write a program to analyze different student data using structures

Here are my assignment instructions

Write a program to

read 20 student names and test score from a file and input them into student structures. You will then assign a letter grade based on the test score and add that to each structure. You should provide a menu that offers a user the options of:

Printing all student names (in alphabetical order) and letter grades
Print all students names in grade order (i.e. all A’s, all B’s, etc)
Print the student's name with the highest test score
Print the top 5 students with the highest test scores
Print the average of all test scores


I have been working on this for a little while and ran into a problem. I cannot seem to write a function to find the average of the test scores. I used my same code from a previous assignment and it is not working here. I believe that the names in my input file are causing my issues and I do not know how to write a function that goes around the characters and only uses numbers. Do I craft another input with just numbers?


Here is my input file:

Duckey Donald 85
Goof Goofy 89
Brave Balto 93
Snow Smitn 93
Alice Wonderful 89
Samina Akthar 85
Simba Green 95
Donald Egger 90
Brown Deer 86
Johny Jackson 95
Greg Gupta 75
Samuel Happy 80
Danny Arora 80
Sleepy June 70
Amy Cheng 83
Shelly Malik 95
Chelsea Tomek 95
Angela Clodfelter 100
Allison Nields 87
Lance Norman 88

I will answer any replies ASAP and help you understand my code. I commented out the function for my average. Here is my code, and I am extremely thankful for your help. Keep in mind that I am brand new to coding and my knowledge is very limited. Thank you.




[code]

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <numeric>
using namespace std;

struct studentType
{
string studentFName;
string studentLName;
int testScore;
char grade;
};

void getData(ifstream& inFile, studentType sList[], int listSize);
void calculateGrade(studentType sList[], int listSize);
int highestScore(const studentType sList[], int listSize);
void printResult(const studentType sList[], int listSize);
//int arrayAverage(const studentType sList[], int listSize); this is my prototype for the average

int main()
{
ifstream in;
in.open("inData.txt");
if (in.fail()) //is it ok?
{
cout << "file did not open please check it\n";
system("pause");
system("exit");
}
studentType sList[20];
getData(in, sList, 20);
calculateGrade(sList, 20);
printResult(sList, 20);

in.close();
system("pause");
return 0;
}

void getData(ifstream& inFile, studentType sList[], int listSize)
{
int n = 0;
while (n < listSize)
{
inFile >> sList[n].studentLName >> sList[n].studentFName >> sList[n].testScore;
n++;
}
}

void calculateGrade(studentType sList[], int listSize)
{
int i;
for (i = 0; i < listSize; i++)
if (sList[i].testScore < 60)
sList[i].grade = 'F';
else if (sList[i].testScore < 70)
sList[i].grade = 'D';
else if (sList[i].testScore < 80)
sList[i].grade = 'C';
else if (sList[i].testScore < 90)
sList[i].grade = 'B';
else
sList[i].grade = 'A';
}

int highestScore(const studentType sList[], int listSize)
{
int high = 0, i;
for (i = 0; i < listSize; i++)
if (high < sList[i].testScore)
high = sList[i].testScore;

return high;
}

int highest(const studentType sList[], int listSize)
{
int high = 0, i;
for (i = 0; i < listSize; i++)
if (high < sList[i].testScore)
high = sList[i].testScore;

return high;
cout << high;
}

// Function that return average of an array. This is what I have for my average function
/*int arraySum(const studentType sList[], int listSize)
{
{
int initial_sum = 0;
return accumulate(sList, sList + listSize, initial_sum);
}
int sum = 0;
}
*/


void printResult(const studentType sList[], int listSize)
{
cout << left << setw(30) << "Student Name" << right << setw(10) << "TestScore" << right << setw(7) << "Grade" << endl;
string name;
int high, i;
for (i = 0; i < listSize; i++)
{
name = sList[i].studentLName + ", " + sList[i].studentFName;
cout << left << setw(30) << name << right << setw(10) << sList[i].testScore << right << setw(7) << sList[i].grade << endl;
}
cout << endl;
high = highestScore(sList, listSize);
cout << "Highest Test Score: " << high << endl;
cout << "Students having the highest test score: " << endl;
for (int i = 0; i < listSize; i++)
if (sList[i].testScore == high)
cout << sList[i].studentLName << ", " << sList[i].studentFName << endl;
}







(1) USE CODE TAGS

(2) To find an average, add up testScores and divide by the number of students.
That's ... add up SCORES, not attempt to add up STUDENTS.
Last edited on
Remove function highest(). You don't call it and it does the same thing as highestScore().
Topic archived. No new replies allowed.