Advertisement
MeehoweCK

Untitled

May 28th, 2023
679
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. // struktura - składnia:
  8. struct Student     // Student od tego momentu jest nazwą naszego własnego typu zmiennej
  9. {
  10.     string imie;
  11.     string nazwisko;
  12.     int wiek;
  13.     string e_mail;
  14.     double srednia_ocen;
  15. };
  16.  
  17. ostream& operator<<(ostream& out, const Student& student)
  18. {
  19.     out << student.imie << ' ' << student.nazwisko << " (adres e-mail: " << student.e_mail << "), wiek " << student.wiek << " lat, srednia ocen: " << student.srednia_ocen;
  20.     return out;
  21. }
  22.  
  23. istream& operator>>(istream& in, Student& student)
  24. {
  25.     in >> student.imie >> student.nazwisko >> student.wiek >> student.e_mail >> student.srednia_ocen;
  26.     return in;
  27. }
  28.  
  29. bool pobranie_danych(vector<Student>& tablica)
  30. {
  31.     ifstream plik_in;
  32.     plik_in.open("dane_studentow.txt");
  33.     if (plik_in.fail())
  34.         return 1;       // zwrócenie błędu
  35.  
  36.     do
  37.     {
  38.         Student temp;
  39.         plik_in >> temp;
  40.         tablica.push_back(temp);        // dodanie elementu na koniec wektora
  41.     } while (!plik_in.eof());           // eof - end of file
  42.  
  43.     plik_in.close();
  44.     return 0;       // zwrócenie braku błędu
  45. }
  46.  
  47. int main()
  48. {
  49.     vector<Student> t;
  50.     if (pobranie_danych(t))
  51.     {
  52.         cout << "Nie udalo sie odczytac danych z pliku. Nastapi zamkniecie programu.\n";
  53.         return 0;
  54.     }
  55.  
  56.     for (auto student : t)      // zakresowa pętla for
  57.     {
  58.         cout << student << endl;
  59.     }
  60.  
  61.  
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement