so i'm working on a program for my cs 1 class, on structs. i need to input names and grades from a text file then create a switch to offer different options. i am having trouble turning the percent grades into letter grades(starts at line 75), and finding the average of the grades in a struct without adding each one individually(starting at line 146). any help would be much appreciated
this program is designed to read 20 studen names and test scores from a file and input them into
student structures. it will then assign a letter grade based on the test score and add that to
each structure. then a menu should be provide that offers options ranging from
- 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 students name with the highest test score
- print the top 5 students with the highest test scores
- print the average of all test scores.
algorithm
1. create struct
2. create output file with student names and test scores
3. create main function that pulls data from file and inputs them into the struct.
4. create function that assigns letter grade to test score.
5. create function that averages all test scores.
6. create switch to provide options.
*/
#include <iomanip>
#include <iostream>
#include <string>
#include <fstream>
usingnamespace std;
struct student
{
string first_name;
string last_name;
int test_score;
char grade;
};
void swapem(student &A, student &B)
{
student temp;
temp = A;
A = B;
B = temp;
}
int main()
{
char grade;
student s[20];
ifstream inf;
ofstream outf;
inf.open("gradeSheet.dat");
int j=0;
while(!inf.eof())
{
inf >> s[j].first_name >> s[j].last_name >> s[j].test_score;
j++;
}
for (int i = 0; i < 20; i++)
{
cout << s[i].first_name << s[i].last_name << s[i].test_score << endl;
}
//for (int i = 0; i < 20; i++)
/*if (s[i].test_score >= 90)
grade = 'A';
else if (s[i].test_score >= 80)
grade = 'B';
else if (s[i].test_score >= 70)
grade = 'C';
else if (s[i].test_score >= 60)
grade = 'D';
else grade = 'F'; */
int ch = 0;
while (ch != 6)
//if (ch == 1)
//ch = 1;
//else if (ch == 2)
//ch = 2;
{
char grade;
for (int i = 0; i < 20; i++)
{
if (s[i].test_score >= 90)
{
grade = 'A';
}
elseif (s[i].test_score >= 80)
{
grade = 'B';
}
elseif (s[i].test_score >= 70)
{
grade = 'C';
}
elseif (s[i].test_score >= 60)
{
grade = 'D';
}
else { grade = 'F'; }
}
int average = 0;
cin >> ch;
switch (ch) //creating a switch using data from input file
{
case 1: // lists students alphebetically by last name
for (int j = 0; j < 19; j++)
{
for (int i = 0; i < 19; i++)
{
(s[i].last_name, s[i + 1].last_name);
if (s[i].last_name > s[i + 1].last_name) swapem(s[i], s[i + 1]); // compares and swaps order of names by last name
}
}
for (int i = 0; i < 20; i++)
cout << s[i].first_name << " " << s[i].last_name << endl; //string compare // didn't use, created swapem function
break; // breaks loop between cases
case 2: // lists all students in order of grade
for (int j= 0; j < 19; j++)
{
for (int i = 0; i < 19; i++)
{
(s[i].test_score, s[i + 1].test_score);
if (s[i].test_score > s[i + 1].test_score) swapem(s[i], s[i + 1]); // compares and swaps test scores to order them
}
}
for (int i = 0; i < 20; i++)
cout << s[i].first_name << " " << s[i].last_name << " " << grade << endl; // fixme test scores need to be grades
break;
case 3:
for (int j = 0; j < 19; j++)
{
for (int i = 0; i < 19; i++)
{
(s[i].test_score, s[i + 1].test_score);
if (s[i].test_score > s[i + 1].test_score) swapem(s[i], s[i + 1]); // compares and swaps test scores to order them
}
}
//for (int i = 0; i < 20; i++)
cout << s[19].first_name << " " << s[19].last_name << " " << s[19].test_score << endl; // fixme test scores need to be grades
break;
case 4:
for (int j = 0; j < 19; j++)
{
for (int i = 0; i < 19; i++)
{
(s[i].test_score, s[i + 1].test_score);
if (s[i].test_score > s[i + 1].test_score) swapem(s[i], s[i + 1]); // compares and swaps test scores to order them
}
}
for (int i = 15; i < 20; i++)
cout << s[i].first_name << " " << s[i].last_name << " " << s[i].test_score << endl; // fixme test scores need to be grades
case 5:
for (int i = 0; i < 20; i++)
{
for (int i = 0; i < 20; i++)
{
grade = ((s[i].test_score) + (s[i + 1].test_score)/20);
cout << grade << endl;
}
}
}
}
system("pause");
return 0;