Skip to content

Instantly share code, notes, and snippets.

@boki1
Last active June 24, 2019 09:24
Show Gist options
  • Save boki1/52618dc266ee25703ed6c209cc5f3ee1 to your computer and use it in GitHub Desktop.
Save boki1/52618dc266ee25703ed6c209cc5f3ee1 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <fstream>
using std::string;
#define MARKS_COUNT 5
struct student
{
student(unsigned, string, int *);
student();
friend std::ostream &operator<<(std::ostream &l, const student &s);
friend int operator>>(std::istream &l, student &s);
private:
unsigned number;
string name;
int *marks;
};
student::student() {}
student::student(unsigned _number, string _name, int *_marks) : number(_number), name(_name), marks(_marks) {}
std::ostream &operator<<(std::ostream &l, const student &s)
{
l << s.number << ';';
l << s.name << ';';
for (int i = 0; i < MARKS_COUNT - 1; ++i) l << s.marks[i] << ";";
l << s.marks[MARKS_COUNT - 1] << '\n';
return l;
}
int operator>>(std::istream &l, student &s)
{
unsigned _number;
string _name;
int *_marks = new int[MARKS_COUNT];
std::cout << "Enter num: \n";
l >> _number;
if (!_number)
return 1;
std::cout << "Enter name:\n";
l >> _name;
std::cout << "Grades:\n";
for (int i = 0; i < MARKS_COUNT; ++i) l >> _marks[i];
s = student(_number, _name, _marks);
return 0;
}
int main()
{
std::fstream table;
table.open("students.csv", std::ios::out);
student s;
do {
if (std::cin >> s) break;
table << s;
} while (true);
table.close();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment