Advertisement
fcamuso

Corso recupero C++ - video 24

Nov 28th, 2022
1,006
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 KB | None | 0 0
  1. #include <iostream>
  2. #include<fstream>
  3.  
  4. using namespace std;
  5.  
  6. struct Manga
  7. {
  8.   string titolo="";
  9.   string categoria="";
  10.  
  11.   int costo=0;
  12.  
  13. };
  14.  
  15. void stampa(Manga *p, int letti)
  16. {
  17.   for (int i=0; i<letti; i++)
  18.     cout << "Titolo: " << p[i].titolo << "  "
  19.          << "Categoria: " << p[i].categoria << "  "
  20.          << "Costo: " << p[i].costo << endl;
  21. }
  22.  
  23. int main()
  24. {
  25. //    Manga *p = new Manga;
  26. //
  27. //    p[0].titolo = "Dragonball";
  28. //
  29. //    (*p).categoria = "Shounen";
  30. //
  31. //     p->costo = 12;
  32. //
  33.  
  34.   ifstream leggi("dati_manga.txt");
  35.  
  36.   //recuperiamo dalla prima riga il numero dei manga
  37.   string riga="";
  38.   getline(leggi, riga);
  39.   int quanti = stoi(riga);
  40.  
  41.   //allochiamo un array di struct adeguato al numero letto
  42.   Manga *p = new Manga[quanti];
  43.  
  44.   int letti=0;
  45.   while ( letti<quanti && getline(leggi, p[letti].titolo) )
  46.   {
  47.     getline(leggi, p[letti].categoria);
  48.  
  49.     getline(leggi, riga);
  50.     p[letti].costo = stoi(riga);
  51.  
  52.     letti++;
  53.   }
  54.  
  55.   stampa(p, letti);
  56.  
  57.   leggi.close(); leggi.clear();
  58.  
  59.   delete[] p;
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.     return 0;
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement