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;
}
}
Re: Problem With Correctly Handling FirstName And Id In Struct And Input
So what's the c++ question? What's the problem you are having handling first name and id?
Re: Problem With Correctly Handling FirstName And Id In Struct And Input
Quote:
Originally Posted by
2kaud
So what's the c++ question? What's the problem you are having handling first name and id?
I need a firstname variable in my struct, and to read the firstname and the id in my readStuData function. Not exactly sure how, for i am a beginner with C++
Re: Problem With Correctly Handling FirstName And Id In Struct And Input
Take a look at the line above. How do you think you would add a firstName ?
http://www.cplusplus.com/doc/tutorial/structures/
Re: Problem With Correctly Handling FirstName And Id In Struct And Input
Quote:
I need a firstname variable in my struct
Well, as the struct has a lastName element as type string, just add a firstName element as type string to the struct.
Quote:
read the firstname and the id in my readStuData function
What is the format of the data in the file for which you want to read? Is it first last id or id last first or what? Once you know just extend the infile extraction statement in the while loop. Something like this
Code:
while (i < NUM_STUDENTS && inFile >> Exams[i].firstName >> Exams[i].lastName >> Exams[i].ID)
(assuming the order is first last id)
Note for this to work, all elements must be present in the file and the names can't contain spaces or tabs (whitespace characters).
Re: Problem With Correctly Handling FirstName And Id In Struct And Input
Quote:
Originally Posted by
2kaud
Well, as the struct has a lastName element as type string, just add a firstName element as type string to the struct.
What is the format of the data in the file for which you want to read? Is it first last id or id last first or what? Once you know just extend the infile extraction statement in the while loop. Something like this
Code:
while (i < NUM_STUDENTS && inFile >> Exams[i].firstName >> Exams[i].lastName >> Exams[i].ID)
(assuming the order is first last id)
Note for this to work, all elements must be present in the file and the names can't contain spaces or tabs (whitespace characters).
Here is , my update program. shows no errors but can you take a look at it to see if I successfully completed all requirements of the program?
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 firstName;
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 firstName;
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].firstName >> Exams[i].lastName >> Exams[i].ID)
{
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;
}
}
Re: Problem With Correctly Handling FirstName And Id In Struct And Input
These Are The Requirements
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!
------------------------------------------------------------------------------------------------------------
Re: Problem With Correctly Handling FirstName And Id In Struct And Input
Quote:
if I successfully completed all requirements of the program?
Well does it? You should know - its your program design and code. Have you tested all the options? Does the output correspond to what would be expected? Does it write the expected results to the file? If you have tested all options and the results are as expected, then there's a good chance the program does what's required.