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..

Recommended Answers

All 10 Replies

Sorry, the str_date is a stringstream.

How did you clear the stringstream? Can you post a more complete example?

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.

And how are the strings formatted? God, this is like pulling teeth. :icon_rolleyes:

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);
      }

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.