Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- using namespace std;
- // struktura - składnia:
- struct Student // Student od tego momentu jest nazwą naszego własnego typu zmiennej
- {
- string imie;
- string nazwisko;
- int wiek;
- string e_mail;
- double srednia_ocen;
- };
- ostream& operator<<(ostream& out, const Student& student)
- {
- out << student.imie << ' ' << student.nazwisko << " (adres e-mail: " << student.e_mail << "), wiek " << student.wiek << " lat, srednia ocen: " << student.srednia_ocen;
- return out;
- }
- istream& operator>>(istream& in, Student& student)
- {
- in >> student.imie >> student.nazwisko >> student.wiek >> student.e_mail >> student.srednia_ocen;
- return in;
- }
- bool pobranie_danych(vector<Student>& tablica)
- {
- ifstream plik_in;
- plik_in.open("dane_studentow.txt");
- if (plik_in.fail())
- return 1; // zwrócenie błędu
- do
- {
- Student temp;
- plik_in >> temp;
- tablica.push_back(temp); // dodanie elementu na koniec wektora
- } while (!plik_in.eof()); // eof - end of file
- plik_in.close();
- return 0; // zwrócenie braku błędu
- }
- int main()
- {
- vector<Student> t;
- if (pobranie_danych(t))
- {
- cout << "Nie udalo sie odczytac danych z pliku. Nastapi zamkniecie programu.\n";
- return 0;
- }
- for (auto student : t) // zakresowa pętla for
- {
- cout << student << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement