Created
October 8, 2021 04:51
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
#include <string> | |
using namespace std; | |
const int MAX_LEN = 20; | |
const int NUM_SCORES = 3; | |
struct Students { | |
int sid; | |
char sname[MAX_LEN]; | |
double scores[NUM_SCORES]; | |
double total_score; | |
}; | |
Students *makeStudents(int); | |
void printStudents(Students * const, int); | |
int main() | |
{ | |
const int N = 10; | |
//Students *ptr; | |
fstream f; | |
// Students new_students[N]; | |
Students *sptr = new Students [10]; | |
f.open("students.bin", ios::in | ios::binary); | |
if (f.is_open()) | |
{ | |
for ( int i =0; i<N; i++) { | |
f.read((char *)sptr+i, sizeof(Students)); | |
cout << (sptr+i)->sid << endl; | |
cout << (sptr+i)->sname << endl; | |
cout << "Scores: " << endl; | |
(sptr+i)->total_score = 0; | |
for(int j = 0; j < NUM_SCORES; j++){ | |
cout << (sptr+i)->scores[j] << endl; | |
(sptr+i)->total_score += (sptr+i)->scores[j]; | |
} | |
cout << "Total Score: " << (sptr+i)->total_score << endl; | |
} | |
// f.read(reinterpret_cast<char*>(new_students), N * sizeof(Students)); | |
f.close(); | |
} | |
else{ | |
cout << "Error opening binary file.\n" << endl; | |
} | |
// cout << sizeof(new_students[1]); | |
for (int i = 0; i < N-1; i++) | |
{ | |
if((sptr->total_score)/3 >= 50){ | |
cout << (sptr+i)->sid << endl; | |
cout << (sptr+i)->sname << endl; | |
for (int j = 0; j < N; j++) | |
{ | |
cout << (sptr+i)->scores[j] << endl; | |
} | |
cout << (sptr+i)->total_score << endl; | |
} | |
} | |
//printStudents(ptr, N); | |
} | |
void printStudents(Students * const ptr, int N) | |
{ | |
double temp_total; | |
for(int i=0; i < N; i++) | |
{ | |
cout << "ID: " << (ptr+i)->sid << endl; | |
cout << "Name: " << (ptr+i)->sname << endl; | |
cout << "Scores: " << endl; | |
for(int j = 0; j < NUM_SCORES; j++){ | |
cout << (ptr+i)->scores[j] << endl; | |
temp_total += (ptr+i)->scores[j]; | |
} | |
cout << "Total Score: " << endl; | |
cout << temp_total << endl; | |
(ptr+i)->total_score = temp_total; | |
temp_total = 0; | |
cout << endl; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment