Advertisement
Guest User

LOOP PROBLEM HELP I HAVE 2 HOURS LEFT XD

a guest
Jan 17th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.28 KB | None | 0 0
  1. using visual 2017, cmd
  2. I have txt file with some lines of text.
  3. i need a loop that will print this text and stop.
  4. i want that loop to work even if i add more lines of text.
  5. #include "stdafx.h"
  6. #include <iostream>
  7. #include <string>
  8. #include <fstream>
  9. #include <cstdlib>
  10.  
  11.  
  12. using namespace std;
  13. string nazwisko[1000], marka[1000], a[1000], b[1000], c[1000], d[1000];
  14.  
  15. char wybor;
  16. int main()
  17. {
  18.     fstream plik;
  19.    
  20.    
  21.    
  22.     string linia;
  23.     int nr_linii = 1;
  24.     int zbior = 0;
  25.  
  26.     plik.open("praca.txt", ios::in);
  27.  
  28.     while (getline(plik, linia)) {
  29.  
  30.         switch (nr_linii)
  31.         {
  32.  
  33.         case 1:nazwisko[zbior] = linia; break;
  34.         case 2: marka[zbior] = linia; break;
  35.         case 3:a[zbior] = linia; break;
  36.         case 4: b[zbior] = linia; break;
  37.         case 5: c[zbior] = linia; break;
  38.         case 6: d[zbior] = linia; break;
  39.         }
  40.         if (nr_linii == 6) {
  41.             nr_linii = 2; zbior++;  }
  42.  
  43.         nr_linii++;
  44.     }
  45.     plik.close();
  46.     while (!plik.eof()){ <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<LOOP I NEED, THIS DOESNT WORK
  47.         (getline(plik, linia));
  48.         int y = 0;
  49.         y++;
  50.         cout << nazwisko[y] << endl;
  51.         cout << marka[y] << endl;
  52.         cout << a[y] << endl;
  53.         cout << b[y] << endl;
  54.         cout << c[y] << endl;
  55.         cout << d[y] << endl;
  56.     }
  57.    
  58.     plik.open("praca.txt", ios::out | ios::app);
  59.    
  60.     for (int i = 0; i < 10000; i++) {
  61.  
  62.         cout << "kontynuuj - nacisnij 1 i enter!" << endl;
  63.         cout << "wyczysc plik - nacisnij 2 i enter!" << endl;
  64.         cout << "wyjdz - nacisnij 3 i enter!" << endl;
  65.         wybor = getchar();
  66.         switch (wybor) {
  67.  
  68.        
  69.         case '1': {
  70.             cout << "nazwisko: ";
  71.             cin >> nazwisko[i];
  72.             plik <<"nazwisko: "<< nazwisko[i] << endl;
  73.            
  74.             cout << "marka: ";
  75.             cin >> marka[i];
  76.             plik <<"marka: "<< marka[i] << endl;
  77.  
  78.             cout << "rocznik: ";
  79.             cin >> a[i];
  80.             plik <<"rocznik: "<< a[i] << endl;
  81.  
  82.             cout << "max predkosc: ";
  83.             cin >> b[i];
  84.             plik << "max predkosc: "<< b[i] << endl;
  85.  
  86.             cout << "przebieg: ";
  87.             cin >> c[i];
  88.             plik << "przebieg: "<< c[i] << endl;
  89.  
  90.             cout << "spalanie: ";
  91.             cin >> d[i];
  92.             plik <<"spalanie: "<< d[i] << endl;
  93.  
  94.         }break;
  95.        
  96.         case '3':
  97.             exit(0);
  98.         break;
  99.        
  100.         case '2':
  101.             ofstream ofs;
  102.             ofs.open("praca.txt", std::ofstream::out | std::ofstream::trunc);
  103.             ofs.close();
  104.        
  105.         }
  106.     }
  107.    
  108.  
  109.     plik.close();
  110.  
  111.     return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement