Question

we will be taking data in as a file. you cannot use the data values in...

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

Homework Answers

Answer #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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a C++ program. 1) Write a program that writes the grades for 3 students. Declare...
Write a C++ program. 1) Write a program that writes the grades for 3 students. Declare a variable of type ofstream which is used to output a stream into a file: ofstream output; // output is the name of the variable Prompt the user to input values for the grades of 3 students. Use the output operator (<<) to write the grades into grades.txt: output << grade1 << " " << grade2 << " " << grade3 << endl; You...
JAVA Question a) Read a file named testScoreIn.txt representing test scores for a class of students....
JAVA Question a) Read a file named testScoreIn.txt representing test scores for a class of students. Each record contains an id number and three test scores (Note: the test scores are integers). For example, a student record might look like: BK1234 87 72 91 b) Without using an array, read each student’s information, one record at a time and compute the student’s average (as a double). c) Write to an output file, testScoreOut.txt, as a neatly formatted table with column...
Create a program that generates a file of random numbers, and then prints them in neat...
Create a program that generates a file of random numbers, and then prints them in neat fashion to another file, and also saves to that file the average and standard deviation of those numbers. I) First, you would need to generate a file of random numbers that consists of N random numbers (100 < N < 1000). Each random digit should be a real number (type double) between 0 and 50. This file and its digits would now serve as...
In the book_store.cpp file, add the code for the process_orders method. The argument that is passed...
In the book_store.cpp file, add the code for the process_orders method. The argument that is passed to this method is the name of a file that will be read in the method. Each line in the file contains an order number (integer), isbn number (character array), and amount ordered (integer). If an order number is on a line, it is guaranteed that there will be an isbn number and amount ordered to go along with it. Create an input file...
You are to write a C program that will read from a file, one or more...
You are to write a C program that will read from a file, one or more sets of x,y coordinates. Each set of coordinates is part of a Cartesian system. A Cartesian coordinate system is a system that specifies each point uniquely in a plane by a pair of numerical coordinates. Your program will determine which quadrant each set belong. - Quadrants are often numbered 1st - 4th and denoted by Roman numerals: I(+,+), II (−,+), III (−,−), and IV...
1.What is the difference between class/object? 2.Did you think about classes/objects around you since the last...
1.What is the difference between class/object? 2.Did you think about classes/objects around you since the last session? 3.What library do we need for processing file I/O? 4.What is the class for the input file stream? Give an example 5.What is the class for the output file stream? Give an example 6.Why do you want to use files instead of using input? 7.How do you read from a file? give an example 8.How do you write to a file? give an...
The following code to run as the described program on python. The extra test file isn't...
The following code to run as the described program on python. The extra test file isn't included here, assume it is a text file named "TXT" with a series of numbers for this purpose. In this lab you will need to take a test file Your program must include: Write a python program to open the test file provided. You will read in the numbers contained in the file and provide a total for the numbers as well as the...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
Create a program that filters the data in a CSV file of product data based on...
Create a program that filters the data in a CSV file of product data based on some search word and prints the resulting output to a new file. Additionally, the program will print the number of items filtered to stdout. • Your program should take three arguments: an input file to process, an output file to save the results, and a search word. • If the output file already exists or cannot be opened, warn the user by printing "CANNOT...
C++ Programming   You are to develop a program to read Baseball player statistics from an input...
C++ Programming   You are to develop a program to read Baseball player statistics from an input file. Each player should bestored in a Player object. Therefore, you need to define the Player class. Each player will have a firstname, last name, a position (strings) and a batting average (floating point number). Your class needs to provide all the methods needed to read, write, initialize the object. Your data needs to be stored in an array of player objects. The maximum...