简体   繁体   中英

How do I load the data from separate lines in this file?

I am trying to load values from a file into a dynamic array of a class called Friend. The Friend class has two private variables, one string for the name, and one Date class object. I overloaded the input operator to call this function, input for friend:

void Friend::input(std::istream& ins){
  char tmpname[50];

  bool x = false;
  std::cout << "Please enter a name: ";
  ins>>tmpname;
  std::string tmpstring(tmpname);
  name = tmpstring;
  ins.sync();
  std::cout << "\n\n" << "Please Enter a Birth date in MM/DD/YYYY format: ";
  ins.sync();
  ins >> bday;
  ins.sync();
  std::cout<<"\n\n";

}

also the input operator is overloaded for the Date class which has three ints for month, day and year. Here is that code:

// input operator, overloaded as a friend
istream& operator >>(istream& ins, Date& d){
   bool flag = false;
   string junk;

   ins>>d.month;
// if an invalid month is detected throw a bad_month
       if(d.month < 1 || d.month > 12){
                getline(ins,junk); // eat the rest of the line
                throw (bad_month(d.month));
        }
// if the read has failed because of eof exit funtion
  if(ins.eof()) return ins;

   if(ins.peek() == '/') ins.ignore();
       ins>>d.day;
// if an invalid day is detected throw a bad_day
        if(d.day < 1  || d.day > d.daysallowed[d.month]){
                getline(ins,junk); // eat the rest of the line
                throw (bad_day(d.month, d.day));
        }
  if(ins.eof()) return ins;
  if(ins.peek() == '/') ins.ignore();

   ins>>d.year;

   return ins;
}

In a class, FBFriends, I have a dynamic array of type Friend, and I need to load saved names and birthdates from a file that is loaded at start. The file MUST be in this format:

First Last
MM/DD/YYYY
First Last
MM/DD/YYYY
First Last
MM/DD/YYYY

The load function that I tried was:

void FBFriends::load(std::istream& ins){
  Friend f1;
  int i=0;
  while(ins >> f1){
    if(i % 2 == 0 && i != 0){insert(f1);};
    i++;
  }


  }


void FBFriends::insert(const Friend& f){

  if(used >= capacity){ resize();}

  for(int i = used; i>current_index; i--){
     data[i] = data[i - 1];
  }
  data[current_index] = f;
  used++;
}

I apologize for the long post, I needed to include all relevant code. The load function inserted invalid values into the date function causing a crash. How can I correctly load these values into the Friend array?

Since name takes up an entire line, use getline to read the name. Replace

ins>>tmpname;
std::string tmpstring(tmpname);
name = tmpstring;

by

getline(ins, name);

Update

In FBFriends::load , you have:

while(ins >> f1){

That will work only if you have a function

std::istream& operator>>(std::istream& in, Friend& f) { ... }

I suggest that you remove

void Friend::input(std::istream& ins) { ... }

and replace it with

std::istream& operator>>(std::istream& in, Friend& f) { ... }

Also, I don't understand the purpose of

if(i % 2 == 0 && i != 0){insert(f1);};

I trust that you know what you are doing there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM