we will be taking data in as a file. you cannot use the data values in your source file but you must read in everything into variables and use it from there.
First we need to include <fstream> at the top of our file.
We will need to create an input file stream to work and ifstream. ifstream is a datatype. Create a variable with the datatype being ifstream. Variable is drfine by using the member accessor operator to call the open function and passing in “testGrades.txt”, which is where our data will be.
Example: [whatever the variable name is].open(“testGrades.txt”);
Your program must be setup to open “testGrades.txt” as the file. For testing you can add your own testGrades.txt file into visual studios as a resource (right click resource in solution explorer -> go to add new -> utility -> text file).
The file you will be imputing to your program is a record of the 3 test grade from a class of students. You will need to read in the information for the student number first. This data will be used later to calculate some of the output. “Students:” in the file is only a decoration/label but you will still need to read it in (or use the stream ignore function) so that you can get to the student number. The maximum amount of students these classes will ever have is 8. To read in the first line your code file could look something like this:
ifstream file;
file.open("testGrades.txt");
int students;
string studentLabel;
file >> studentLabel;
file >> students;
We need to read in the total number of attempts on this test. This number will tell us how many grades will follow on the line below and thus how many numbers we will need to read in to compute the test average.
Example input:
Students: 7
Quiz/Homework Average: 66.85
test 1
Attempts: 7
81 90 55 67 101 77 91
test 2
Attempts: 5
24 66 61 98 71
test 3
Attempts: 6
54 87 100 79 67 93
Your output will be the average for each test as well as whether the student did better or worse than the quizzes and homework average. Additionally, output the number of attempts above and below the quizzes and homework average per test.
For the example file above the output should be:
test 1:
Attempts: 7
Average: 80.2857
test 1 average above quiz/homework average
Number above quiz average: 6
Number below quiz average: 1
test 2:
Attempts: 5
Average: 64
test 2 average below quiz/homework average
Number above quiz average: 2
Number below quiz average: 3
test 3:
Attempts: 6
Average: 80
test 3 average above quiz/homework average
Number above quiz average: 5
Number below quiz average: 1
#include<iostream>
#include<sstream>
#include<fstream>
using namespace std;
int main()
{
ifstream file;
//open the input file
file.open("testGrades.txt");
string line;
//read the first line
getline(file, line);
stringstream s(line);
int students;
string studentLabel;
s >> studentLabel;
//read number of students
s >> students;
//read the second line
getline(file, line);
stringstream ss(line);
string avglabel1;
string avglabel2;
double avg;
ss >> avglabel1;
ss >> avglabel2;
//read average
ss >> avg;
string line1, line2, line3;
//read three lines at a time from file
while(getline(file, line1))
{
getline(file, line2);
getline(file, line3);
//create string stream object for
each line
stringstream ss1(line1);
stringstream ss2(line2);
stringstream ss3(line3);
string label;
ss1>>label;
int num;
//read the test number
ss1>>num;
ss2>>label;
int attempts;
//read the number of attempts
ss2>>attempts;
//create an array to store the
marks
double arr[attempts];
double average = 0;
for(int i = 0;i <
attempts;i++)
{
int marks;
//read the
marks
ss3 >>
marks;
//store the
marks into array
arr[i] =
marks;
//add the marks
to average
average +=
marks;
}
//calculate the average
average /= attempts;
//initialize high average and low
average to 0
int high_avg = 0, low_avg =
0;
for(int i = 0;i <
attempts;i++)
{
//check and
increment the respective variable
if(arr[i] >
avg)
{
high_avg += 1;
}
else
{
low_avg += 1;
}
}
//display the output
cout<<"Test
"<<num<<":"<<endl;
cout<<"Attempts: "<<
attempts<<endl;
cout<<"Average: "<<
average<<endl;
cout<<"test
"<<num<<" average above quiz/homework
average"<<endl;
cout<<"Number above quiz
average: "<<high_avg<<endl;
cout<<"Number below quiz
average: "<< low_avg<<endl;
}
}
If you have any doubts please comment and please don't dislike.
Get Answers For Free
Most questions answered within 1 hours.