1
#include <fstream>
#include <string>
#include <limits>

using namespace std;

ifstream fin ("tv.in");
ofstream fout("tv.out");

struct time {
    int PublicityTime;
    int hh, mm, ss;
};

void getTime(const string& s, time& t) {
    int x = 0;
    int i = 0;

    for (;s[i] != ' ';i ++)
        x = x * 10 + s[i] - '0';

    i ++;

    t.PublicityTime = x;

    x = 0;

    for (;s[i] != ':';i ++)
        x = x * 10 + s[i] - '0';

    i ++;

    t.hh = x;

    x = 0;

    for (;s[i] != ':';i ++)
        x = x * 10 + s[i] - '0';

    i ++;

    t.mm = x;

    x = 0;

    for (;i < s.size();i ++)
        x = x * 10 + s[i] - '0';

    t.ss = x;
}

int main() {
    int q, n;
    char c;
    string s;
    time arr[100];

    fin >> q;
    fin >> n;

    for (int i = 1;i <= n;i ++) {
        getline(fin, s);

        fout << s << '\n';
        getTime(s, arr[i]);
    }   

    return 0;
}

I want to read from a file that looks something like this

6
1
120 12:00:00
200 12:01:50
1000 13:00:00
2000 13:01:00
100 14:05:05
10 23:59:49

if i run the above code for the this file,i will get something like this

94217584 -1388646703:121:0
120 12:0:0
200 12:1:50
1000 13:0:0
2000 13:1:0
100 14:5:5

Can someone help undestand what is happening ? I've searched on google in order to find what would cause such an error and found that the problem would be that there are some '\n' that remain in the buffer, because the file looks something like this: "6\n1\n120 12:00:00\n200 12:01:50\n1000 13:00:00\n2000 13:01:00\n100 14:05:05\n10 23:59:49" I tried extracting both '\n' but the problem still isn't solved.

fin >> n;
fin >> c;
fin >> q;
fin >> c;
Rob
  • 2,447
  • 2
  • 18
  • 27
royer
  • 35
  • 1
  • 5
  • It's a well known trap for the unwary. See the duplicate question – john Apr 16 '20 at 17:37
  • I'm not sure where you get `both '\n'` from. There's only one '\n' that needs extracting. Plus `fin >> c;` doesn't extract '\n'. – john Apr 16 '20 at 17:41
  • `fin >> q; fin >> n; fin.ignore(std::numeric_limits::max(), '\n');` The call to ignore extracts the only `\n` that needs extracting (the one after `1` and before the first time). – john Apr 16 '20 at 17:43
  • I mean if i write only fin >> n >> q >> c; then my code works fine.I thought there are 2 "\n". – royer Apr 16 '20 at 17:45
  • But the first `'\n'` is extracted anyway, how would the second number be read if it wasn't? – john Apr 16 '20 at 17:45
  • I didn't state this clearly, the problem with `fin >> c;` is not that it doesn't extract `\n`, it does. The problem is that it reads the first character of the time, which means it won't be available to your `getTime` routine. – john Apr 16 '20 at 17:47
  • So you are basically saying that when i wrote fin >> q; first "\n" is discarded by default.right ? then i need to write explicitely fin >> c; to discard the second "\n" – royer Apr 16 '20 at 17:47
  • No, The first \n is extracted by the `fin >> n;` When you read a number, any leading whitespace is extracted and ignored. The mistake beginners make is that they think the trailing whitespace is ignored as well. – john Apr 16 '20 at 17:48
  • And don't write `fin >> c;` as explained that does not do what you want. It might solve the `\n` problem, but it just gives you a different problem. – john Apr 16 '20 at 17:50
  • Sorry if i ask so many dumb questions by i didn't find this problem to be explicitly discussed on internet and i really want to understand how these things work. so when i read the very first number with fin >> n the first \n is extracted and ignored. So by this logic shouldn't fin >> q have same effect (like the next "\n" being extracted and ignored). – royer Apr 16 '20 at 18:02
  • The first number is read with `fin >> q` – john Apr 16 '20 at 18:02
  • Reading the first number does not extract any `\n`. Reading the **second** number extracts the **first** `\n` because how could the reading reach the second number without going past the first `\n`? – john Apr 16 '20 at 18:04
  • It really is very simple, when you read a number, the reading skips any spaces, tabs, newlines etc. until it reaches a digit. Then it reads all the digits and then it stops immediately. That's all there is to it. – john Apr 16 '20 at 18:06
  • OHHH. i think now i get it. When i write fin >> n;. it gets the first number. Then when i write fin >> q; now my cursor points to the second number because he discarded my "\n". so my file looks something like "\n120......" – royer Apr 16 '20 at 18:09
  • That's exactly it. Although there seems to be some confusion over whether `n` or `q` is the first number. In the code you posted `q` is the first number. – john Apr 16 '20 at 18:10
  • Thank you for spending so much time explaining to me.Im such a dummy. – royer Apr 16 '20 at 18:11
  • It's just details, unfortunately in programming you have to get the details right. – john Apr 16 '20 at 18:12
  • Looking forward to get better at it :) – royer Apr 16 '20 at 18:14

0 Answers0