struct issues

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

-The Jast

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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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>

using namespace 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';
			}
			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 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;
Last edited on
1
2
if (s[i].test_score >= 90)
	s[i].grade = 'A';

1
2
3
4
5
{
	float avg = 0;
	for (int i = 0; i < 20; i++) avg += s[i].test_score;
	cout << avg << endl;
}
Topic archived. No new replies allowed.