Question

Write the functions needed to complete the following program as described in the comments. Use the...

  1. Write the functions needed to complete the following program as described in the comments. Use the input file course.txt and change the mark of student number 54812 to 80.

/* File: course.cpp

A student's mark in a certain course is stored as a structure (struct student as defined below) consisting of first and last name, student id and mark in the course.  The functions read() and write() are defined for the structure student.  

Information about a course is stored as a structure (struct course as defined below) consisting of the course instructor, course name, course enrollment and an array of students.  The functions read() and write() are defined for the structure course.

   

The user is allowed to change the marks of students using the function update().

*/

   

#include

#include

#include

using namespace std;

struct student

{

   string first;  // first name

   string last;   // last name

   int id;        // student id number

   float mark;    // mark in course

};

void read(istream& in, student& s);

void write(ostream& out, const student& s);

struct course

{

   string instructor;   // "Crooks", for example

   string name;         // "APSC2613", for example

   int size;            // number of students in the class

   student* classlist;  // array of students in the course

};

   

void read(istream& in, course& c);

void write(ostream& out, const course& c);

int update(course& c, int idnumber, float newmark);

int main(void)

{   

   course c;

   int idnumber;    // student id number         

   float newmark;   // updated mark              

   int found;       // flag indicates idnumber found

   ifstream fin ("course.in");

   ofstream fout ("course.out");

   

   // read course information from the file

   read(fin, c);

   cout << c.size << " marks read\n";

   

   // update the marks

   do

   {

      cout << "\n\nEnter the student id followed by the new mark\n";

      cout << "Enter 0 for student number to quit\n";

      

      cin >> idnumber >> newmark;

        

      if(idnumber)

      {

         found = update(c, idnumber,newmark);

      

         if(!found)

            cout << "\nThat student is not in the list\n";

      }

   }while(idnumber != 0);

   write(fout, c);

   

   system("pause");

   return 0;

}

*** Text file components

Crooks

APSC2613

4

Jack White 101102 86

Jill Green 20093 95

Bob Grey 54812 78

Jean Black 55432 89

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts please comment below. Please give upvote if you like this.

Answer :

Please find the code below::

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct student
{
string first;
string last;
int id;
float mark; // first name
// last name
// student id number
// mark in course
};
void read(istream& in, student& s){
in>>s.first>>s.last>>s.id>>s.mark;
}
void write(ostream& out, const student& s){
out<<s.first<<" "<<s.last<<" "<<s.id<<" "<<s.mark<<endl;
}
struct course
{
string instructor;
string name;
int size;
student* classlist; // "Crooks", for example
// "APSC2613", for example
// number of students in the class
// array of students in the course
};
void read(istream& in, course& c){
in>>c.instructor;
in>>c.name;
in>>c.size;
c.classlist = new student[c.size];
for(int i=0;i<c.size;i++){
student s ;
read(in,s);
c.classlist[i] = s;
}
}

void write(ostream& out, const course& c){
out<<c.instructor<<endl;
out<<c.name<<endl;
out<<c.size<<endl;
for(int i=0;i<c.size;i++){
write(out,c.classlist[i]);
}

}
int update(course& c, int idnumber, float newmark){
for(int i=0;i<c.size;i++){
if(idnumber==c.classlist[i].id){
c.classlist[i].mark = newmark;
return 1;
}
}
return 0;
}
int main(void)
{
course c;
int idnumber;
float newmark;
int found; // student id number
// updated mark
// flag indicates idnumber found
ifstream fin ("course.txt");
ofstream fout ("courseOut.txt");
// read course information from the file
read(fin, c);
cout << c.size << " marks read\n";
// update the marks
do
{
cout << "\n\nEnter the student id followed by the new mark\n";
cout << "Enter 0 for student number to quit\n";
cin >> idnumber >> newmark;
if(idnumber)
{
found = update(c, idnumber,newmark);
if(!found)
cout << "\nThat student is not in the list\n";
}
}while(idnumber != 0);
write(fout, c);
fout.close();
return 0;
}

output:

course.txt

Crooks
APSC2613
4
Jack White 101102 86
Jill Green 20093 95
Bob Grey 54812 78
Jean Black 55432 89

courseOut.txt

Crooks
APSC2613
4
Jack White 101102 86
Jill Green 20093 95
Bob Grey 54812 80
Jean Black 55432 89

Add a comment
Know the answer?
Add Answer to:
Write the functions needed to complete the following program as described in the comments. Use the...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The task is to write Song.cpp to complete the implementation of the Song class, as defined...

    The task is to write Song.cpp to complete the implementation of the Song class, as defined in the provided header file, Song.h, and then to complete a program (called sales) that makes use of the class. As in Project 1, an input data file will be provided containing the song name and other information in the same format as in Program 1: Artist    Song_Title    Year    Sales    Medium As before, the program (sales) which are individual copies, will use this information to compute the gross...

  • C++ Redo PROG8, a previous program using functions and/or arrays. Complete as many levels as you...

    C++ Redo PROG8, a previous program using functions and/or arrays. Complete as many levels as you can. Level 1: (20 points) Write FUNCTIONS for each of the following: a) Validate #students, #scores. b) Compute letter grade based on average. c) Display student letter grade. d) Display course average. Level 2: (15 points) Use ARRAYS for each of the following. e) Read a student's scores into array. f) Calculate the student's average based on scores in array. g) Display the student's...

  • Write a C++ program that includes the following: Define the class Student in the header file Stu...

    Write a C++ program that includes the following: Define the class Student in the header file Student.h. #ifndef STUDENT_H #define STUDENT_H #include <string> #include <iostream> using namespace std; class Student { public: // Default constructor Student() { } // Creates a student with the specified id and name. Student(int id, const string& name) { } // Returns the student name. string get_name() const { } // Returns the student id. int get_id () const { } // Sets the student...

  • 2. In the following program an employee of a company is represented by an object of...

    2. In the following program an employee of a company is represented by an object of type employee consisting of a name, employee id, employee salary and the workday starting time. The starting time is a class time 24 object. Implement the class employee. Declaration of Employee and Time Classes /* File : employeetime.h Hlustrates composition of classes */ #ifndef EMPLOYEETIME.H #define EMPLOYEETIME.H #include <iostream> #include <string> using namespace std; class time 24 { private : int hour; int minute...

  • BACKGROUND Movie Review sites collect reviews and will often provide some sort of average review to...

    BACKGROUND Movie Review sites collect reviews and will often provide some sort of average review to sort movies by their quality. In this assignment, you will collect a list of movies and then a list of reviews for each movie. You will then take a simple average (total of reviews divided by number of reviews) for each movie and output a sorted list of movies with their average review. Here you are provided the shell of a program and asked...

  • Write a program in Java that prompts a user for Name and id number. and then...

    Write a program in Java that prompts a user for Name and id number. and then the program outputs students GPA MAIN import java.util.StringTokenizer; import javax.swing.JOptionPane; public class Main {    public static void main(String[] args) {                       String thedata = JOptionPane.showInputDialog(null, "Please type in Student Name. ", "Student OOP Program", JOptionPane.INFORMATION_MESSAGE);        String name = thedata;        Student pupil = new Student(name);                   //add code here       ...

  • I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student...

    I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student {    private String name;    private double gpa;    private int idNumber;    public Student() {        this.name = "";        this.gpa = 0;        this.idNumber = 0;    }    public Student(String name, double gpa, int idNumber) {        this.name = name;        this.gpa = gpa;        this.idNumber = idNumber;    }    public Student(Student s)...

  • Hello I need a small fix in my program. I need to display the youngest student...

    Hello I need a small fix in my program. I need to display the youngest student and the average age of all of the students. It is not working Thanks. #include <iostream> #include <iomanip> #include <fstream> #include <vector> #include <algorithm> using namespace std; struct Student { string firstName; char middleName; string lastName; char collegeCode; int locCode; int seqCode; int age; }; struct sort_by_age { inline bool operator() (const Student& s1, const Student& s2) { return (s1.age < s2.age); // sort...

  • How to complete these methods: /**    * Returns a reference to the course with title equals to the argument. This    * m...

    How to complete these methods: /**    * Returns a reference to the course with title equals to the argument. This    * method searches in the courses stored in the HashMap {@code courses} to find    * the course whose title equals to the argument {@code title}. If the course is    * not found, {@code null} is returned.    *    * @param title the title of the course    * @return a reference to the course, or {@code null} if the course is not   ...

  • The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib>...

    The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib> #include <iomanip> #define MAX 1000 using namespace std; //Function to Add a new inventory item to the data into the array in memory void addItem(string desc[],string idNum[], float prices[], int qty[],int &num) { cout<<"Enter the names:"; cin>>desc[num]; cout<<"Enter the item number:"; cin>>idNum[num]; cout<<"Enter the price of item:"; cin>>prices[num]; cout<<"Enter the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT