Advertisement
MeehoweCK

Untitled

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