I am writing a c++ program and i need help with this program. When i read data f
ID: 3576845 • Letter: I
Question
I am writing a c++ program and i need help with this program. When i read data from keyboard then it give me error with class average , when I serach with name name then it give me wrong letter grades. Please help me with this program...
#include<iostream>
#include<iomanip>
#include<string>
#include<fstream>
using namespace std;
const int NUM_Exams = 3;
const int NUM_Students = 1;
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];
float 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
#include "stdafx.h"
#include<iostream>
#include<iomanip>
#include<string>
#include<fstream>
using namespace std;
const int NUM_Exams = 3;
const int NUM_Students = 1;
struct studata
{
float exams[NUM_Exams];
float average;
string firstName;
string lastName;
char letterGrade;
};
void menu();
void letterGrade(studata Exams[]);
void average(studata Exams[]);
double averageFromDisplay(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];
float 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 - 1; i++)
{
classAverage += averageFromDisplay(Exams);
}
classAverage /= studentCount - 1;
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';
}
}
char letterGradeToDisplay(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';
return Exams[i].letterGrade;
}
}
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);
}
double averageToDisplay(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;
return Exams[i].average;
}
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;
double averageCalculate = 0.0;
char letterGradeDisplay = '';
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;
}
averageCalculate = averageFromDisplay(Exams);
letterGradeDisplay = letterGradeToDisplay(Exams);
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) <<
averageCalculate << setw(10) << letterGradeDisplay << endl;
}
}
}
void searchwithFirst(studata Exams[], string targetaName)
{
bool firstFound = true;
double averageCalculate = 0.0;
char letterGradeDisplay = '';
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;
}
averageCalculate = averageFromDisplay(Exams);
letterGradeDisplay = letterGradeToDisplay(Exams);
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) <<
averageCalculate << setw(10) << letterGradeDisplay << endl;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.