I am writing c++ program and i need help with it and i am asking this quiestion
ID: 3572324 • Letter: I
Question
I am writing c++ program and i need help with it and i am asking this quiestion third time. please help me. and please attach output.
write a grading program for a class wth the following grading policies, using a structure to store the data and there are two quizzes, each graded on the basis of 10 points. b. There is one midterm exam and one final exam, each graded on the basis of 100 points. This progam should have at least three functions: eading, writing and calculations and it will read data form file.
This is what, i have.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
int quiz1, quiz2;
float midterm, final, grade;
char letter;
string name;
ifstream in;
ofstream out;
in.open("gradesIn.txt");
if (in.fail())
{
cout << "input file did not open please check it ";
system("pause");
return 1;
}
out.open("out.txt");
out << "name Quiz 1 Quiz 2 midterm final Grade Letter ";
in >> name;
while (in)
{
in >> quiz1 >> quiz2 >> midterm >> final;
grade =11:40 PM 12/3/2016 (quiz1 / 10.*100. + quiz2 / 10.*100.) / 2.*.25 + midterm*.25 + final*.5;
if (grade >= 90)
letter = 'A';
else if (grade >= 80)
letter = 'B';
else if (grade >= 70)
letter = 'C';
else if (grade >= 60)
letter = 'D';
else
letter = 'F';
out << name << " " << quiz1 << " " << quiz2 << " " << midterm << " " << final << " "
<< grade << " " << letter << endl;
in >> name;
}
out.close();
in.close();
return 0;
}
//
Txt file.
Franks 10 9 96.0 88.5
Jones 8 9 88.5 83.0
Merrill 8 7 66.0 69.5
Smith 9 10 95.2 93.6
Wilson 7 8 76.0 74.5
Explanation / Answer
gradesIn.txt (Save this file under D Drive .Then the path of the file pointing to it is D:\gradesIn.txt)
Franks 10 9 96.0 88.5
Jones 8 9 88.5 83.0
Merrill 8 7 66.0 69.5
Smith 9 10 95.2 93.6
Wilson 7 8 76.0 74.5
_______________________
code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//Creating Structure
struct Student {
//Declaring variables
string name;
int quiz1;
int quiz2;
float midterm;
float final ;
}stud[5];
//Declaring arrays
char grad_letter[5];
float grad[5];
//Function declarations
void readData();
void calculate();
void display();
int main()
{
//Calling the function
readData();
calculate();
display();
return 0;
}
/* function to read the data from the file
* and populate them into struct array
*/
void readData()
{
//Declaring variables
int quiz1, quiz2;
float midterm,grade,final;
string name;
//Creating the input stream
ifstream in;
//Open the txt file
in.open("D:\gradesIn.txt");
//If the file is not available dispaly error
if (in.fail())
{
//Displaying the error message
cout << "input file did not open please check it ";
system("pause");
exit(1);
}
else
{
/* This for loop will read the data from
* the file and populate it into struct array
*/
for(int i=0;i<5;i++)
{
//Reading line by line from data file
in >> name >> quiz1 >> quiz2 >> midterm >> final;
stud[i].name=name;
stud[i].quiz1=quiz1;
stud[i].quiz2=quiz2;
stud[i].midterm=midterm;
stud[i].final=final;
}
}
//Closing the input stream
in.close();
}
/* This function will calculate the grade and based
* on grade find grade letter and populate them into an array
*/
void calculate()
{
//Declaring variables
char letter;
float grade;
//This for loop will calculate the grade and grade letter
for(int i=0;i<5;i++)
{
grade =(stud[i].quiz1/ 10.*100. + stud[i].quiz2 / 10.*100.) / 2.*.25 + stud[i].midterm*.25 + stud[i].final*.5;
grad[i]=grade;
if (grade >= 90)
letter = 'A';
else if (grade >= 80)
letter = 'B';
else if (grade >= 70)
letter = 'C';
else if (grade >= 60)
letter = 'D';
else
letter = 'F';
grad_letter[i]=letter;
}
}
//This function will display the results to the output file
void display()
{
//Creating the output stream
ofstream out;
//Opening the output file
out.open("D:\out.txt");
out << "Name Quiz 1 Quiz 2 midterm final Grade Letter ";
for(int i=0;i<5;i++)
{
out << stud[i].name << " " << stud[i].quiz1 << " " << stud[i].quiz2 << " " << stud[i].midterm << " " << stud[i].final << " "
<< grad[i] << " " << grad_letter[i] << endl;
}
out.close();
}
_______________________
Output:
out.txt (We can find this file under D Drive.As we specified the output fie path D:\out.txt)
Name Quiz 1 Quiz 2 midterm final Grade Letter
Franks 10 9 96 88.5 92 A
Jones 8 9 88.5 83 84.875 B
Merrill 8 7 66 69.5 70 C
Smith 9 10 95.2 93.6 94.35 A
Wilson 7 8 76 74.5 75 C
_____________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.