Write a C++ program that will display the following menu and work accordingly.

Write a C++ program that will display the following menu and work accordingly.

A menu which contains functionionality that will allow the following choices:

1. Enter the name of the file that contains students enrolled in the computer course.
2. Calculate and display the average and the letter grade for each student to the screen.
3. Sort data from highest to lowest grade.
4. Screen display of the sorted data.
5. Search for a student record by last name and display "Exam Average" and "Letter Grade" .
6. Write the sorted data to “result.txt” file.
7. Exit.

Enter 1-7 to select one of the following choices:

1. Enter the student data file name.
2. Display the average and the letter grade for each student.
3. Sort data from highest to lowest grade.
4. Display the sorted data.
5. Search for a student record by last name.
6. Write the sorted data to the “result.txt” file.
7. Exit.

>> 1
Enter the file name>> studInfo.txt
>> 2
>> 3
>> 4

First name Last Name Exams Average Letter Grade
---------- --------- ------------- ------------
Student one 92 A
Student two 80 B
Student three 70 C

>> 7
>> Bye!
----------------------------------------------------------------------------------------------------------------------------------------------------------------------

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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
  #include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

const int NUM_EXAMS = 3;
const int NUM_STUDENTS = 6;

struct StuData 
{
	string lastName;
	float exam[NUM_EXAMS], average;
	int ID;
	char letterGrade;
};

void programDescription();
void letterGrade(StuData[]);
void calcAverage(StuData[]);
void displayData(StuData[]);
void readStuData(ifstream&, StuData[]);
void writeStuData(ofstream&, StuData[]);
void sortAscending(StuData Exams[]);

int main()
{
	
	int choice;
	string lastName;
	string fileName;
	float examGrade;
	ofstream outFile;
	ifstream inFile;
	StuData Exams[NUM_STUDENTS];
	
	programDescription();

	do
	{
		cout << "Option to choose from\n\n"<<endl;
		cout << "Enter 1 to input the student data by file "<<endl;
		cout << "Enter 2 to input the student data by keyboard "<<endl;
		cout << "Enter 3 to sort the data by highest to lowest grade "<<endl;
		cout << "Enter 4 to display the student data "<<endl;
		cout << "Enter 5 to search for a students'name "<<endl;
		cout << "Enter 6 to write the data to a text file called result.txt "<<endl;
		cout << "Enter 7 to display the sorted data "<<endl;
		cout << "Enter 8 to exit"<<endl;
		cin >> choice;

		if(choice == 1)
		{
			cout << "Enter the file name"<<endl;
			cin >> fileName;
			inFile.open(fileName.c_str());
			if(inFile.fail())
			{
				cout<<" error"<<endl;
					exit(1);
			}
			readStuData(inFile,Exams);
			
		}
		else if(choice == 2)
		{
			cout <<endl;
			for(int i =0; i < NUM_STUDENTS; i++)
			{
			cout<<"Enter the students last name"<<endl;
			cin >> Exams[i].lastName;
			cout <<"Enter the students grade"<<endl;
			for(int j =0; j < NUM_EXAMS; j++)
			{
			cin >> Exams[i].exam[j];
			}
			}
			calcAverage(Exams);
		}
		else if(choice == 3)
		{
			sortAscending(Exams);
		}
		else if(choice == 4)
		{
			calcAverage(Exams);
		}
		else if(choice == 5)
		{
				cout<<"Enter the students that you're looking for"<<endl;
			cin>> lastName;
			int i =0;
			bool found = false;
			while ((!found)&&(i<=NUM_STUDENTS))
			{	
				if(lastName == Exams[i].lastName)
			{
				found = true;
				}
				else 
				i++;
			}
			
		cout <<"Last      Overall      Letter"<<endl;
		cout<< "Name      Average      Grade"<<endl;
		cout << "-----     -------      -----"<<endl;
		cout << setw(10) << Exams[i].lastName << setw(10) << Exams[i].average <<setw(10) << Exams[i].letterGrade<<endl;
		}	
		else if(choice == 6)
		{
			outFile.open("result.txt");
			if(outFile.fail())
			{
				cout<<"Error"<<endl;
					exit(1);
			}

			writeStuData(outFile, Exams);
			outFile.close();
		}
		else if(choice == 7)
		{
			displayData(Exams);
		}
		else if (choice == 8)
		{
			cout<<"Finish"<<endl;

		}
		else
		{
			cout << "Invaid number entered"<<endl;
		}
}while((choice >= 1) && (choice < 8));
	
	inFile.close();

	return 0;
}



void programDescription()
{
	cout << "This program is going to display you a list of options which yout will have to" <<endl;
	cout	<<"sort and diplay student grade" <<endl;
	cout	<<"calculate the averge and search by lastname."<<endl;
	

}


void letterGrade(StuData Exams[])
{
	for(int i=0; i <NUM_STUDENTS; i++)
	{
		if(Exams[i].average >= 90)
			Exams[i].letterGrade = 'A';
		else if(Exams[i].average >= 80)
			Exams[i].letterGrade = 'B';
		else if(Exams[i].average >= 70)
			Exams[i].letterGrade = 'C';
		else if(Exams[i].average >= 60)
			Exams[i].letterGrade = 'D';
		else
			Exams[i].letterGrade = 'F';
		
	}
}

void calcAverage(StuData Exams[])
{

	for(int i =0; i <NUM_STUDENTS; i++)
	{
		for(int j=0; j<NUM_EXAMS;j++)
		{
		Exams[i].average = ((Exams[i].exam[j] + Exams[i].exam[j] + Exams[i].exam[j])/3);
		}	
		}
	letterGrade(Exams);

	displayData(Exams);

}

void sortAscending(StuData Exams[])
{
	StuData temp;
	for(int i = 0; i < NUM_STUDENTS; i++)
		for(int j = 0; j <NUM_STUDENTS; j++)
		{
			if(Exams[i].average< Exams[j].average)
			{
				temp = Exams[i];
				Exams[i] = Exams[j];
				Exams[j]= temp;
			}
		}
}

void displayData(StuData Exams[])
{
	
	cout << endl << endl
		 << "Last          Exam      Exam      Exam      Overall" << endl
		 << "Name            1         2         3       Average   Grade" << endl
		 << "----          ----      ----      ----      -------   -----" << endl;
	
	for (int i = 0; i < NUM_STUDENTS; i++)
	{
		cout << left << setw(15);
		cout << Exams[i].lastName << setw(10) << Exams[i].exam[0] << setw(10)  
			 << Exams[i].exam[1] << setw(10)  << Exams[i].exam[2];
		cout << setw(10) << Exams[i].average << setw(10) 
			 << Exams[i].letterGrade << endl;
	}

}

void readStuData(ifstream& inFile, StuData Exams[])
{
	int i =0;
	while(i < NUM_STUDENTS && inFile >> Exams[i].lastName)
	{
		inFile >> Exams[i].exam[0] >> Exams[i].exam[1] >> Exams[i].exam[2] >> Exams[i].letterGrade;
		i++;
	}
	calcAverage(Exams);
	letterGrade(Exams);
}

void writeStuData(ofstream& outFile, StuData Exams[])
{
	for (int i = 0; i < NUM_STUDENTS; i++)
		{
			outFile << Exams[i].lastName;
		    outFile << setw(15) << Exams[i].average << setw(10);
			outFile << Exams[i].letterGrade << endl;
		}
}
So whats the problem?
cannot get code to handle firstName and id in struct and input, no lettergrade in input file.
Topic archived. No new replies allowed.