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

In C++ Write a C++ program that reads a file (students.txt) which contains names

ID: 3841325 • Letter: I

Question

In C++

Write a C++ program that reads a file (students.txt) which contains names of students(separated by a newline). It then creates a linked list of students. Each entry in the list must contain the student name, the final average, and a pointer to the next student record. The program initializes the list by first reading the students’ names from the text file and setting the final grades to 0.00. After building the list, the program then loops through it and prompts the user to enter the final average for each student. Finally, the program then writes all the information to another file (output.txt)

Explanation / Answer

#include<iostream.h>
#include<string>

struct student {
   string name;
   float final_average;
   student *next;
};


void main(){

   ifstream infile;
   ofstream outfile;
   infile.open("students.txt");
   string name;
   student *p, *start;
  
  
   start = NULL;

   infile.open("students.txt");
   while (infile >> name){
       if (start == NULL){
          start = new student();
          start->name = name;
          start->final_average = 0.00;
          start->next = NULL;
       }            
       else {
          p = start;
          while (p->next != NULL)
               p = p->next;
          p->next = new student();
          p = p->next;
          p->name = name;
          p->final_average = 0.00;
          p->next = NULL;
       }
   }
   infile.close();
   p = start;
   while (p != NULL){
        cout << "Enter final average for " << p->name << endl;
        cin >> p->final_average;
        p = p->next;
   }
   outfile.open("output.txt");
   p = start;
   while (p!=NULL){
       outfile << p->name << " " << p->final_average << endl;
       p = p->next;
   }
   outfile.close();   
}