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

swww.drop s/m08dgnsaikeoafn/cs109-Practice-Exam-02.pdf?di-0 cs109-Practice-Exam-

ID: 3724999 • Letter: S

Question

swww.drop s/m08dgnsaikeoafn/cs109-Practice-Exam-02.pdf?di-0 cs109-Practice-Exam-02.pdf Does this file preview look okay? Yes No C++ Program (30 points) 3) There exists an input file named "students.txt" that contains student NetiDs and GPAs. An example is shown on the right; the format is one student per line, with one ormalbri 3.43 more spaces between the NetID and GPA. Write a complete C++ program that opens rmaddo this file, inputs the data, and outputs the NetID and GPA of every student with a GPA

Explanation / Answer

#include<iostream>
#include<fstream>
using namespace std;

// define a class to store student data
class student
{
char netid[30];
float gpa
public:
student() { }
float getgpa(){
return gpa;
}
void getData(); // get student data from user
void displayData(); // display data
};

void student :: getData() {
cout << " Enter net id: ";
cin.getline(netid, 30);
cout << " Enter gpa: ";
cin >> gpa;
}

void student :: displayData() {
cout << " Netid : " <<netid << endl;
cout << " gpa : " << gpa << endl;
}

int main()
{
student s[10]; // array of 10 student objects
fstream file;
int i;

file.open("students.txt", ios :: out); // open file for writing
cout << " Writing Student information to the file :- " << endl;
cout << " Enter 10 students Details to the File :- " << endl;

for (i = 0; i < 3; i++)
{
s[i].getData();
// write the object to a file
file.write((char *)&s[i], sizeof(s[i]));
}

file.close(); // close the file

file.open("students.txt", ios :: in); // open file for reading
cout << " Reading Student information from the file :- " << endl;

for (i = 0; i < 10; i++)
{
// read an object from a file
file.read((char *)&s[i], sizeof(s[i]));
if(s[i].getgpa() < 2.0)
s[i].displayData();
}

file.close(); // close the file

return 0;
}