This is my code:

for (i = 0; i < date.size(); i++)
      {
      str_date << date[i]; 
      str_date >> newDay;
      str_date >> newMonth;
      str_date >> newYear;
      apptDay.push_back(newDay);
      apptMonth.push_back(intMonth);
      apptYear.push_back(newYear);
      }

My problem is that the first date[1] gets saved nicely, however the second round of the loop it all goes haywire. I tried clearing the stringstream each time after a loop but it does nothing..

int i = 0;
  vector <string> date;
  stringstream str_date;
  int newDay, newYear, intMonth;
  vector <int> apptDay;
  vector <int> apptMonth;
  vector <int> apptYear;

for (i = 0; i < date.size(); i++)
    {
    str_date << date[i];
    str_date >> newDay;
    str_date >> newMonth;
    str_date >> newYear;
    apptDay.push_back(newDay);
    apptMonth.push_back(intMonth);
    apptYear.push_back(newYear);
    str_date.clear();
    }

I have checked and everything else is working fine, for example, the date vector is filled with strings that can be extracted fine with date. where i is any integer.

Haha sorry,

ifstream Appointments;
Appointments.open(DATAFILE);
  if (!Appointments.is_open())
    {    
    cout << "ERROR: Could not open the datafile: " << DATAFILE << endl;
    exit(1);
    }

    // Read the file until it's finished, displaying lines as we go.
    while (!Appointments.eof())
      {
      getline(Appointments, line, '\n'); // getline reads a line at a time
      date.push_back(line);
      }

Edited 5 Years Ago by ksm092: n/a

I've gotten lines from a datafile, the lines in the datafile are in the format :12 August 2011 Description

Oh I have to add that I converted the month, e.g. August to an integer

I bet it's the description part mucking things up. Note that clear() only clears the error flags, it doesn't discard unread characters. You can truly clear the stringstream by saying str_date.str("") :

for (i = 0; i < date.size(); i++)
      {
      str_date << date[i]; 
      str_date >> newDay;
      str_date >> newMonth;
      str_date >> newYear;
      apptDay.push_back(newDay);
      apptMonth.push_back(intMonth);
      apptYear.push_back(newYear);
      str_date.clear(); // For good measure
      str_date.str("");
      }

Oh my God looks like that str("") fixed my problem... been bugging me for quite a bit :) Thanks!

This question has already been answered. Start a new discussion instead.