Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this assignment, you will write a program in C++ which uses files and nested

ID: 3737200 • Letter: I

Question

In this assignment, you will write a program in C++ which uses files and nested loops to create a file from the quiz grades entered by the user, then reads the grades from the file and calculates each student’s average grade and the average quiz grade for the class.

Each student takes 6 quizzes (unknown number of students). Use a nested loop to write each student’s quiz grades to a file. Then read the data from the file in order to display the student’s average score and the class average.

Use the data below when executing your program. Enter 7 items for each student. Column 1 is the student ID and Column 2-7 are the quiz grades.

Student id

Quiz 1

Quiz 2

Quiz 3

Quiz 4

Quiz 5

Quiz 6

1000

90

100

81

100

70

81

1001

85

95

86

100

87

96

1002

50

60

74

100

75

89

1008

100

93

87

100

91

88

Requirements:

Use a nested loop structure to input the data and write the data to a text file (have the user enter the text file name):

The outer loop will be a while loop; the inner loop will be a for loop (6 quizzes).

Validate whether the quiz scores are in range 0 – 100, inclusive.

Since you do not know how many students will be entered, determine a way to quit the loop.

Add spaces in between each item added to the text file, except add a new line after each complete set of student data.

The text file that your program creates should look like the format in the example below, but the actual data may be different:

Use a nested loop structure to read the data from the text file and calculate the student’s average grade (validate that the file is available):

The outer loop will be a while loop; the inner loop will be a for loop

To calculate each student’s average score, use a total variable initialized to 0 before the for loop, then calculate the student’s average after the loop.

To calculate the class average, initialize a classTotal variable to 0 before the while loop, add each student’s total into the classTotal following the for loop, then calculate the classAverage after the while loop.

Only display 1 decimal position for the averages.

BEWARE of integer division.

Use cout to display the values of the variables. Your cout statements MUST use the variables to display the values

Student id

Quiz 1

Quiz 2

Quiz 3

Quiz 4

Quiz 5

Quiz 6

1000

90

100

81

100

70

81

1001

85

95

86

100

87

96

1002

50

60

74

100

75

89

1008

100

93

87

100

91

88

Explanation / Answer

here is your program : ------------->>>>>>>>>>>>

#include<iostream>
#include<fstream>

using namespace std;

void readAndWrite(string file){
ofstream ofs;
ofs.open(file.c_str());
if(ofs.is_open()){
  string ch;
  int id;
  int score;
  //first while loop
  while(true){
   cout<<" Enter Student ID : ";
   cin>>id;
   ofs<<id<<" ";
   //second loop
   for(int i = 0;i<6;i++){
    cout<<" Enter Quiz"<<(i+1)<<" Score : ";
    cin>>score;
    //for checking the score is between 0-100
    if(score < 0 || score > 100){
     i--;
     cout<<" Score Must in between 0-100 .";
     continue;
    }
    ofs<<score<<" ";
   }
   ofs<<" ";
   cout<<" Want to enter more student data ? (y/n)";
   cin>>ch;
   if(ch == "n" || ch == "N"){
    break;
   }
  }
  ofs.close();
}else{
  cout<<" Output File Creating error";
  exit(-1);
}
}

void readAndProcess(string file){
ifstream ifs;
ifs.open(file.c_str());
int id;
int score;
if(ifs.is_open()){
  ifs>>id;
  int classTotal = 0;
  int studentTotal = 0;
  int numStu = 0;
  //first loop
  while(!ifs.eof()){
   //second loop for reading score of quiz
   studentTotal = 0;
   for(int i = 0;i<6;i++){
    ifs>>score;
    studentTotal += score;
   }
   cout<<" ID = "<<id<<" Avarage Score = "<<(double)(studentTotal/6.0);
   classTotal += studentTotal;
   numStu++;
   ifs>>id;
  }
  cout<<" Class Avarage = "<<(double)(classTotal/(double)numStu);
  cout<<" GOOD BYE";
  ifs.close();
}else{
  cout<<" File opening error !!! ";
  exit(0);
}
}

int main(){
string file;
cout<<" Enter filename for process the Quiz data ";
cin>>file;
readAndWrite(file);
readAndProcess(file);

return 0;
}