0

I have a problem when comparing user input to text file line, it goes all right until when i want to output that all line, it only outputs remaining part of the line, when finding match. This is small part of airport project that i need to finish for school, but i keep strugling on this problem! So i hope someone can help me. Text file is this

  1. LATVIJA/RIGA | BOEING-45 | RIGA - MASKAVA | 20.02.2018 / 15:55 | R-126 |
  2. LATVIJA/RIGA | BOEING-45 | RIGA - HELSINKI | 25.02.2018 / 15:55 | R-127 |
  3. LATVIJA/RIGA | BOEING-45 | RIGA - AMERIKA | 20.02.2018 / 15:55 | R-129 |
  4. LATVIJA/RIGA | BOEING-45 | RIGA - BERLINE | 20.02.2018 / 15:55 | R-134 |
  5. LATVIJA/RIGA | BOEING-45 | RIGA - MASKAVA | 25.02.2018 / 15:55 | R-166 |

Bold text is what it outputs as you can see in this image

enter image description here

but i need it to output these text lines like this when i input date 20.02.2018

  1. LATVIJA/RIGA | BOEING-45 | RIGA - MASKAVA | 20.02.2018 / 15:55 | R-126 |
  2. LATVIJA/RIGA | BOEING-45 | RIGA - AMERIKA | 20.02.2018 / 15:55 | R-129 |
  3. LATVIJA/RIGA | BOEING-45 | RIGA - BERLINE | 20.02.2018 / 15:55 | R-134 |

Heres the code

 ifstream myfile;
myfile.open("statistika.txt", ios::app);

string datums;
string item;
string teksts;
cout <<"Ievadi velamo lidosanas datumu: " << endl;
cin >> datums;
while(!myfile.eof()){
        myfile >> item;
        if(item==datums){
            getline(myfile, teksts);
            cout << teksts <<endl;
        }
    }
user4581301
  • 33,082
  • 7
  • 33
  • 54
  • 3
    Unrelated but will get you sooner or later: [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – user4581301 Feb 06 '18 at 18:21
  • Bad duplicate. The problem is the Asker read most of the line trying to find the date and then reads and outputs only the remainder of the line. Artūrs, use `getline` to read a whole line. Then tokenize the line (so you don't get fooled by the date string showing up somewhere else on the line) and check the the fourth token for the date. If you have a match, then print the whole line. – user4581301 Feb 06 '18 at 18:35
  • [link](http://prntscr.com/ib1eph) something like this? Im really new to c++, what i didnt understand exactly is what means tokenize the line. And it doesnt output anything in that example code, dont know what to do – Artūrs Mēnesis Feb 06 '18 at 19:04
  • More like https://ideone.com/IRhRV5 – user4581301 Feb 06 '18 at 19:22
  • Tokenizing is splitting up a message into parts, tokens, based on some sort of delimiter. In this case there is a convenient |, the delimiter, between each different piece of data. Search for the | and you know you have found the whole token. getline can get more than just lines, so I used it to do the splitting. – user4581301 Feb 06 '18 at 19:26
  • BIG THANK YOU, it now worked as needed :) – Artūrs Mēnesis Feb 06 '18 at 21:11

0 Answers0