I am asking this question four time. Can you help me with this question. This is
ID: 3576753 • Letter: I
Question
I am asking this question four time. Can you help me with this question. This is a c++ programming code. In this program, I got most of the part but when i try to read data from file, it does not display class average and letter grade. And also, when i search with first or last name, it does not display letter grades and class average. Please include sample out as correct code. (Please, include sample out).
Text file....
Argun Puri 66 77 66 69.6667 D
John Bill 88 33 33 51.3333 F
Bill Ciltion 88 33 33 51.3333 F
/////////////////////////////////////////////////////////
#include<iostream>
#include<iomanip>
#include<string>
#include<fstream>
using namespace std;
const int NUM_Exams = 3;
const int NUM_Students = 4;
struct studata
{
float exams[NUM_Exams];
float average;
string firstName;
string lastName;
char letterGrade;
};
void menu();
void letterGrade(studata Exams[]);
void average(studata Exams[]);
void displayAverage(studata Exams[]);
void readData(studata Exams[], ifstream&inFile, string fileName);
void writeData(studata Exams[], ofstream&outFile, string fileName);
void sortDataHighesttoLowest(studata Exams[]);
void sortLowest(studata Exams[]);
void searchwithLastName(studata Exams[], string targetaName);
void searchwithFirst(studata Exams[], string targetaName);
int main()
{
int choice;
ifstream inFile;
string fileName, firstName, lastName;
studata Exams[NUM_Students];
double classAverage;
int studentCount=1;
bool again = false;
bool hasGrade = false;
ofstream outFile;
string targetaName;
while (!again)
{
menu();
cout << "Please choose an options: ";
cin >> choice;
cout << endl;
switch (choice)
{
case 1:
//this case will ask user to enter the file name
cout << "What is the name of the text file. ";
cin >> fileName;
readData(Exams, inFile, fileName);
hasGrade = true;
continue;
case 2:
//this case will ask user to enter the grades
cout << "Please enter the following informations." << endl;
//using for loop for student name
for (int i = 0; i < NUM_Students; i++)
{
cout << " Student " << i + 1 << " grades:" << endl;
cout << "First name: ";
cin >> Exams[i].firstName;
cout << "Last name: ";
cin >> Exams[i].lastName;
//using for loop to get grades
for (int j = 0; j < NUM_Exams; j++)
{
cout << "Exam " << j + 1 << " grades: ";
cin >> Exams[i].exams[j];
studentCount++;
}
}
hasGrade = true;
continue;
cout << "Your estudent exam grade has been add successfully. " << endl;
case 3:
//this case will display the student letter grades and average
while (!hasGrade)
{
cout << "Grades does not exist yet." << endl;
}
average(Exams);
displayAverage(Exams);
continue;
case 4:
//this case will display the class average
while (!hasGrade)
{
cout << "Grades does not exist yet." << endl;
}
classAverage = 0;
for (int i = 0; i < studentCount; i++)
{
classAverage += Exams[i].average;
}
classAverage /= studentCount;
cout << setprecision(2) << fixed << endl;
cout << "The class average is " << classAverage << endl;
continue;
case 5:
//this case will ask user to enter the exact name for output file
while (!hasGrade)
{
cout << "Grades does not exist yet." << endl;
}
cout << "What is the exact file name, where you would like to write data. ";
cin >> fileName;
writeData(Exams, outFile, fileName);
continue;
case 6:
//this case will sort data from highest to lowest
while (!hasGrade)
{
cout << "Grades does not exist yet." << endl;
}
sortDataHighesttoLowest(Exams);
continue;
case 7:
//this case will display the gardes high
sortLowest(Exams);
continue;
case 8:
//this case will let user to serach student with their last name.
cout << "Please enter last student last name: ";
cin >> targetaName;
searchwithLastName(Exams, targetaName);
continue;
case 9:
cout << "Please nter first name: ";
cin >> targetaName;
searchwithFirst(Exams, targetaName);
default:
again = true;
break;
}
}
system("pause");
return 0;
}
void menu()
{
cout << "1. To read data from text file." << endl;
cout << "2. To read data from keyboard. " << endl;
cout << "3. To display all students data, including letterGrade and average. " << endl;
cout << "4. To display the class average." << endl;
cout << "5. To write data on text file within user given name." << endl;
cout << "6. To Sort data highest to lowest." << endl;
cout << "7. To Sort data lowest to highest." << endl;
cout << "8. To serach student with their last name." << endl;
cout << "9. To search with first name." << endl;
}
void letterGrade(studata Exams[])
{
//using if else to calculote the class letter grades and using for loop
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 average(studata Exams[])
{
//using for loop to calculte the class average
for (int i = 0; i < NUM_Students; i++)
{
cout << showpoint << fixed << setprecision(2);
Exams[i].average = (Exams[i].exams[0] + Exams[i].exams[1]
+ Exams[i].exams[2]) / 3;
}
letterGrade(Exams);
}
//Modified display function
void displayAverage(studata Exams[])
{
cout <<"--------------------------------------------------------------------------------" << endl;
cout <<setw(10)<<"FirstName"
<<setw(10)<<"LastName"
<< setw(10)<<"Exam 1"
<< setw(10)<<"Exam 2"
<< setw(10)<<"Exam 3"
<< setw(10)<<"Average"
<< setw(13)<<"letter Grade" << endl;
cout << "--------------------------------------------------------------------------------" << endl;
//using for loop to display the grades
for (int i = 0; i < NUM_Students; i++)
{
cout << left << setw(15);
cout << Exams[i].firstName << setw(10) << Exams[i].lastName <<
setw(10) << Exams[i].exams[0] << setw(10) << Exams[i].exams[1]
<< setw(10) << Exams[i].exams[2] << setw(10) <<
Exams[i].average << setw(13) << Exams[i].letterGrade << endl;
}
}
//Modified readData method
void readData(studata Exams[], ifstream&inFile, string fileName)
{
inFile.open(fileName);
string firstLine;
int i = 0;
if (inFile.fail())
{
cout << "Error occur while opening file." << endl;
system("pause");
exit(1);
}
//read using the file input stream inFile iteself until end of the file
//and store in the studata structure
while (inFile >> Exams[i].firstName >> Exams[i].lastName
>> Exams[i].exams[0] >> Exams[i].exams[1] >>
Exams[i].exams[2] >> Exams[i].average >> Exams[i].letterGrade)
{
i++;
}
inFile.close();
}
void writeData(studata Exams[], ofstream&outFile, string fileName)
{
outFile.open(fileName);
//using for loop
for (int i = 0; i < NUM_Students; i++)
{
outFile<< left << setw(15);
outFile << Exams[i].firstName << setw(10) << Exams[i].lastName <<
setw(10) << Exams[i].exams[0] << setw(10) << Exams[i].exams[1]
<< setw(10) << Exams[i].exams[2] << setw(10) <<
Exams[i].average << setw(10) << Exams[i].letterGrade << endl;
}
outFile.close();
}
void sortDataHighesttoLowest(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) //If you want to sort high to low instead of low to high, change the < sign to a > sign
{
temp = Exams[i];
Exams[i] = Exams[j];
Exams[j] = temp;
}
}
}
void sortLowest(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) //If you want to sort high to low instead of low to high, change the < sign to a > sign
{
temp = Exams[i];
Exams[i] = Exams[j];
Exams[j] = temp;
}
}
}
void searchwithLastName(studata Exams[], string targetaName)
{
bool firstFound = true;
for (int i = 0; i < NUM_Students; i++)
{
if (Exams[i].lastName.compare(targetaName) == 0)
{
if (firstFound)
{
cout << "-----------------------------------------------------------------------------------------" << endl;
cout << "FirstName" << setw(10) << "LastName" << setw(10) << "Exam 1"
<< setw(10) << "Exam 2" << setw(10) << "Exam 3" << setw(10)
<< "Average" << setw(10) << "letter Grade" << endl;
cout << "----------------------------------------------------------------------------------------" << endl;
firstFound = false;
}
cout << left << setw(15);
cout << Exams[i].firstName << setw(10) << Exams[i].lastName <<
setw(10) << Exams[i].exams[0] << setw(10) << Exams[i].exams[1]
<< setw(10) << Exams[i].exams[2] << setw(10) <<
Exams[i].average << setw(10) << Exams[i].letterGrade << endl;
}
}
}
void searchwithFirst(studata Exams[], string targetaName)
{
bool firstFound = true;
for (int i = 0; i < NUM_Students; i++)
{
if (Exams[i].firstName.compare(targetaName) == 0)
{
if (firstFound)
{
cout << "-----------------------------------------------------------------------------------------" << endl;
cout << "FirstName" << setw(10) << "LastName" << setw(10) << "Exam 1"
<< setw(10) << "Exam 2" << setw(10) << "Exam 3" << setw(10)
<< "Average" << setw(10) << "letter Grade" << endl;
cout << "----------------------------------------------------------------------------------------" << endl;
firstFound = false;
}
cout << left << setw(15);
cout << Exams[i].firstName << setw(10) << Exams[i].lastName <<
setw(10) << Exams[i].exams[0] << setw(10) << Exams[i].exams[1]
<< setw(10) << Exams[i].exams[2] << setw(10) <<
Exams[i].average << setw(10) << Exams[i].letterGrade << endl;
}
}
}
Explanation / Answer
Changes you need to make to get the code running
1) add header #include <cstdlib>
2) change the first line of readData function to
inFile.open (fileName.c_str());
because it open function takes constant char pointer as argument not string.
3. Add one extra line in input.txt file and remove any extra spaces.
Argun Puri 66 77 66 69.6667 D
John Bill 88 33 33 51.3333 F
Bill Ciltion 88 33 33 51.3333 F
Nil madhab 90 90 90 90.0000 A
Thats it your code should be running fine.
Here is sample output
nilmadhab@nilmadhab-Inspiron-3542:~/Desktop/chegg/answers/cpp$ ./a.out
1. To read data from text file.
2. To read data from keyboard.
3. To display all students data, including letterGrade and average.
4. To display the class average.
5. To write data on text file within user given name.
6. To Sort data highest to lowest.
7. To Sort data lowest to highest.
8. To serach student with their last name.
9. To search with first name.
Please choose an options: 1
What is the name of the text file. input.txt
1. To read data from text file.
2. To read data from keyboard.
3. To display all students data, including letterGrade and average.
4. To display the class average.
5. To write data on text file within user given name.
6. To Sort data highest to lowest.
7. To Sort data lowest to highest.
8. To serach student with their last name.
9. To search with first name.
Please choose an options: 3
--------------------------------------------------------------------------------
FirstName LastName Exam 1 Exam 2 Exam 3 Average letter Grade
--------------------------------------------------------------------------------
Argun Puri 66.00 77.00 66.00 69.67 D
John Bill 88.00 33.00 33.00 51.33 F
Bill Ciltion 88.00 33.00 33.00 51.33 F
Nil madhab 90.00 90.00 90.00 90.00 A
1. To read data from text file.
2. To read data from keyboard.
3. To display all students data, including letterGrade and average.
4. To display the class average.
5. To write data on text file within user given name.
6. To Sort data highest to lowest.
7. To Sort data lowest to highest.
8. To serach student with their last name.
9. To search with first name.
Please choose an options: 9
Please nter first name: Bill
Bill is targer
-----------------------------------------------------------------------------------------
FirstNameLastName Exam 1 Exam 2 Exam 3 Average letter Grade
----------------------------------------------------------------------------------------
Bill Ciltion 88.00 33.00 33.00 51.33 F
Here is the complate code
/*
I am asking this question four time. Can you help me with this question. This is a c++ programming code. In this program, I got most of the part but when i try to read data from file, it does not display class average and letter grade. And also, when i search with first or last name, it does not display letter grades and class average. Please include sample out as correct code. (Please, include sample out).
Text file....
Argun Puri 66 77 66 69.6667 D
John Bill 88 33 33 51.3333 F
Bill Ciltion 88 33 33 51.3333 F
/////////////////////////////////////////////////////////
*/
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
const int NUM_Exams = 3;
const int NUM_Students = 4;
struct studata
{
float exams[NUM_Exams];
float average;
string firstName;
string lastName;
char letterGrade;
};
void menu();
void letterGrade(studata Exams[]);
void average(studata Exams[]);
void displayAverage(studata Exams[]);
void readData(studata Exams[], ifstream&inFile, string fileName);
void writeData(studata Exams[], ofstream&outFile, string fileName);
void sortDataHighesttoLowest(studata Exams[]);
void sortLowest(studata Exams[]);
void searchwithLastName(studata Exams[], string targetaName);
void searchwithFirst(studata Exams[], string targetaName);
int main()
{
int choice;
ifstream inFile;
string fileName, firstName, lastName;
studata Exams[NUM_Students];
double classAverage;
int studentCount=1;
bool again = false;
bool hasGrade = false;
ofstream outFile;
string targetaName;
while (!again)
{
menu();
cout << "Please choose an options: ";
cin >> choice;
cout << endl;
switch (choice)
{
case 1:
//this case will ask user to enter the file name
cout << "What is the name of the text file. ";
cin >> fileName;
readData(Exams, inFile, fileName);
hasGrade = true;
continue;
case 2:
//this case will ask user to enter the grades
cout << "Please enter the following informations." << endl;
//using for loop for student name
for (int i = 0; i < NUM_Students; i++)
{
cout << " Student " << i + 1 << " grades:" << endl;
cout << "First name: ";
cin >> Exams[i].firstName;
cout << "Last name: ";
cin >> Exams[i].lastName;
//using for loop to get grades
for (int j = 0; j < NUM_Exams; j++)
{
cout << "Exam " << j + 1 << " grades: ";
cin >> Exams[i].exams[j];
studentCount++;
}
}
hasGrade = true;
continue;
cout << "Your estudent exam grade has been add successfully. " << endl;
case 3:
//this case will display the student letter grades and average
while (!hasGrade)
{
cout << "Grades does not exist yet." << endl;
}
average(Exams);
displayAverage(Exams);
continue;
case 4:
//this case will display the class average
while (!hasGrade)
{
cout << "Grades does not exist yet." << endl;
}
classAverage = 0;
for (int i = 0; i < studentCount; i++)
{
classAverage += Exams[i].average;
}
classAverage /= studentCount;
cout << setprecision(2) << fixed << endl;
cout << "The class average is " << classAverage << endl;
continue;
case 5:
//this case will ask user to enter the exact name for output file
while (!hasGrade)
{
cout << "Grades does not exist yet." << endl;
}
cout << "What is the exact file name, where you would like to write data. ";
cin >> fileName;
writeData(Exams, outFile, fileName);
continue;
case 6:
//this case will sort data from highest to lowest
while (!hasGrade)
{
cout << "Grades does not exist yet." << endl;
}
sortDataHighesttoLowest(Exams);
continue;
case 7:
//this case will display the gardes high
sortLowest(Exams);
continue;
case 8:
//this case will let user to serach student with their last name.
cout << "Please enter last student last name: ";
cin >> targetaName;
searchwithLastName(Exams, targetaName);
continue;
case 9:
cout << "Please nter first name: ";
cin >> targetaName;
searchwithFirst(Exams, targetaName);
continue;
default:
again = true;
break;
}
}
//system("pause");
return 0;
}
void menu()
{
cout << "1. To read data from text file." << endl;
cout << "2. To read data from keyboard. " << endl;
cout << "3. To display all students data, including letterGrade and average. " << endl;
cout << "4. To display the class average." << endl;
cout << "5. To write data on text file within user given name." << endl;
cout << "6. To Sort data highest to lowest." << endl;
cout << "7. To Sort data lowest to highest." << endl;
cout << "8. To serach student with their last name." << endl;
cout << "9. To search with first name." << endl;
}
void letterGrade(studata Exams[])
{
//using if else to calculote the class letter grades and using for loop
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 average(studata Exams[])
{
//using for loop to calculte the class average
for (int i = 0; i < NUM_Students; i++)
{
cout << showpoint << fixed << setprecision(2);
Exams[i].average = (Exams[i].exams[0] + Exams[i].exams[1]
+ Exams[i].exams[2]) / 3;
}
letterGrade(Exams);
}
//Modified display function
void displayAverage(studata Exams[])
{
cout <<"--------------------------------------------------------------------------------" << endl;
cout <<setw(10)<<"FirstName"
<<setw(10)<<"LastName"
<< setw(10)<<"Exam 1"
<< setw(10)<<"Exam 2"
<< setw(10)<<"Exam 3"
<< setw(10)<<"Average"
<< setw(13)<<"letter Grade" << endl;
cout << "--------------------------------------------------------------------------------" << endl;
//using for loop to display the grades
for (int i = 0; i < NUM_Students; i++)
{
cout << left << setw(15);
cout << Exams[i].firstName << setw(10) << Exams[i].lastName <<
setw(10) << Exams[i].exams[0] << setw(10) << Exams[i].exams[1]
<< setw(10) << Exams[i].exams[2] << setw(10) <<
Exams[i].average << setw(13) << Exams[i].letterGrade << endl;
}
}
//Modified readData method
void readData(studata Exams[], ifstream& inFile, string fileName)
{
inFile.open (fileName.c_str());
string firstLine;
int i = 0;
if (inFile.fail())
{
cout << "Error occur while opening file." << endl;
system("pause");
exit(1);
}
//read using the file input stream inFile iteself until end of the file
//and store in the studata structure
while (inFile >> Exams[i].firstName >> Exams[i].lastName
>> Exams[i].exams[0] >> Exams[i].exams[1] >>
Exams[i].exams[2] >> Exams[i].average >> Exams[i].letterGrade)
{
i++;
}
inFile.close();
}
void writeData(studata Exams[], ofstream&outFile, string fileName)
{
outFile.open(fileName.c_str());
//using for loop
for (int i = 0; i < NUM_Students; i++)
{
outFile<< left << setw(15);
outFile << Exams[i].firstName << setw(10) << Exams[i].lastName <<
setw(10) << Exams[i].exams[0] << setw(10) << Exams[i].exams[1]
<< setw(10) << Exams[i].exams[2] << setw(10) <<
Exams[i].average << setw(10) << Exams[i].letterGrade << endl;
}
outFile.close();
}
void sortDataHighesttoLowest(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) //If you want to sort high to low instead of low to high, change the < sign to a > sign
{
temp = Exams[i];
Exams[i] = Exams[j];
Exams[j] = temp;
}
}
}
void sortLowest(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) //If you want to sort high to low instead of low to high, change the < sign to a > sign
{
temp = Exams[i];
Exams[i] = Exams[j];
Exams[j] = temp;
}
}
}
void searchwithLastName(studata Exams[], string targetaName)
{
bool firstFound = true;
cout << targetaName << " is targer " << endl;
for (int i = 0; i < NUM_Students; i++)
{
if (Exams[i].lastName.compare(targetaName) == 0)
{
if (firstFound)
{
cout << "-----------------------------------------------------------------------------------------" << endl;
cout << "FirstName" << setw(10) << "LastName" << setw(10) << "Exam 1"
<< setw(10) << "Exam 2" << setw(10) << "Exam 3" << setw(10)
<< "Average" << setw(10) << "letter Grade" << endl;
cout << "----------------------------------------------------------------------------------------" << endl;
firstFound = false;
}
cout << left << setw(15);
cout << Exams[i].firstName << setw(10) << Exams[i].lastName <<
setw(10) << Exams[i].exams[0] << setw(10) << Exams[i].exams[1]
<< setw(10) << Exams[i].exams[2] << setw(10) <<
Exams[i].average << setw(10) << Exams[i].letterGrade << endl;
}
}
}
void searchwithFirst(studata Exams[], string targetaName)
{
bool firstFound = true;
cout << targetaName << " is targer " << endl;
for (int i = 0; i < NUM_Students; i++)
{
if (Exams[i].firstName.compare(targetaName) == 0)
{
if (firstFound)
{
cout << "-----------------------------------------------------------------------------------------" << endl;
cout << "FirstName" << setw(10) << "LastName" << setw(10) << "Exam 1"
<< setw(10) << "Exam 2" << setw(10) << "Exam 3" << setw(10)
<< "Average" << setw(10) << "letter Grade" << endl;
cout << "----------------------------------------------------------------------------------------" << endl;
firstFound = false;
}
cout << left << setw(15);
cout << Exams[i].firstName << setw(10) << Exams[i].lastName <<
setw(10) << Exams[i].exams[0] << setw(10) << Exams[i].exams[1]
<< setw(10) << Exams[i].exams[2] << setw(10) <<
Exams[i].average << setw(10) << Exams[i].letterGrade << endl;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.