|
-
December 8th, 2013, 08:02 PM
#1
Problem With Correctly Handling FirstName And Id In Struct And Input
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.
---------------------------------------------------------------------------------------------------------------------
Sample Output RUN (not a complete example. Menu should be redisplayed after each selection):
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!
------------------------------------------------------------------------------------------------------------
Code:
#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;
}
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|