0

I have a csv file that has data like so: 03/10/2016 09:10:10 PM, Name, Genre

and I have loaded operators that read in the date as integers (DD/MM/YY) and time as integers (HH:MM:SS) and the PM as char, Name and Genre as strings. Here's my code:

In my Time class:

istream & operator >> (istream & is, Time & time)

{
    char colon;
    is >> time.hour >> colon >> time.minute >> colon >> time.second >> time.PM;

    return is;
}

and my Date class

    istream & operator >> (istream & is, Date & date)

{
    char slash;
    is >> date.day >> slash >> date.month >> slash >> date.year;
    return is;
}

I read in my file in another class like so:

string line; //declaration
Show show; //declaration

while (!inFile.eof())
{
    inFile >> date;

    cout << "Date = " << date.getDay() << "/" << date.getMonth() << "/" << date.getYear()<< endl;

    inFile >> time;

    cout << "Time = " << time.getHour() << ":" << time.getMinute() << ":" << time.getSecond() << " " << time.getPM() << " " << endl;


    getline(inFile, line, ',');
    show.setName(line);

    cout << "Name = " << line << endl;

    getline(inFile, line, ',');
    show.setGenre(line);

    cout << "Genre = " << line << endl;

    showVector.push_back(show) //pushback the objects into a vector<Show> showVector
}

So basically, as you can see, I printed out what the program reads in to test and there's just a small issue:

Date = 16/11/2004
Time = 9:30:20 P 
Name = M
Genre = House, Medical Drama

Why is the M in PM being skipped over and assigned to the Name?

ssskh12
  • 23
  • 4
  • 1
    `ostream & operator >>` -- Is this a typo? If it is, you should copy and paste your actual code, and not try to type it in from scratch. – PaulMcKenzie Jul 19 '16 at 21:44
  • 1
    You should show your time class definition, but it looks like you said the PM field is a char. You can't read 2 characters into that. Maybe you should make it a string. – Retired Ninja Jul 19 '16 at 21:48
  • PaulMcKenzie - Yes, sorry. Fixed it. – ssskh12 Jul 19 '16 at 21:49
  • Retired Ninja - When I make it a string, the Time = 9:30:20 PM, House, Medical Drama – ssskh12 Jul 19 '16 at 21:50
  • @ssskh12 I would recommend using an array instead of a string. –  Jul 19 '16 at 21:55
  • 2
    Post the definition of `Time`. It may have something to do with the type of `Time::PM`. – R Sahu Jul 19 '16 at 21:55
  • @ R Sahu PM is a char, as I've stated above. – ssskh12 Jul 19 '16 at 21:57
  • Then simply read the second char into `colon` as you did with the colons... – Aconcagua Jul 19 '16 at 21:58
  • Oh, and you might need to read in the next comma, too. – Aconcagua Jul 19 '16 at 21:59
  • 3
    Off topic: `while (!inFile.eof())` is a common source of errors that you'll stumble across shortly. More here: http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – user4581301 Jul 19 '16 at 21:59
  • @Aconcagua oh cool! I did `is >> time.hour >> colon >> time.minute >> colon >> time.second >> time.AMPM >> colon >> colon;` and that solved it. It was a really simple fix. Thanks a ton! – ssskh12 Jul 19 '16 at 22:02
  • @user4581301 okay thanks, i'll go for something else then – ssskh12 Jul 19 '16 at 22:03
  • I recommend something along the lines of option 2 in Kerrek SB's answer here: http://stackoverflow.com/questions/7868936/read-file-line-by-line/7868998#7868998 – user4581301 Jul 19 '16 at 22:07

1 Answers1

3

The problem is this line, which isn't consuming enough characters:

is >> time.hour >> colon >> time.minute >> colon >> time.second >> time.PM;

Before running the line, your input stream contains 09:10:10 PM, Name, Genre. The fields are then read as follows:

"09" >> time.hour (int)
":"  >> colon (char)
"10" >> time.minute (int)
":"  >> colon (char)
"10" >> time.second (int)
"P"  >> time.PM (char)

After reading these characters, the remaining stream is M, Name, Genre. The getline() call reads from the beginning of this to the next comma, storing the string "M" in Name.

In order to remove the full string "PM" from the stream, you need to read two characters. One way to do this is to read and discard one extra character at the end.

is >> time.hour >> colon >> time.minute >> colon >> time.second >> time.PM >> colon;
Ryan Bemrose
  • 8,148
  • 35
  • 51