5 Replies - 697 Views - Last Post: 23 February 2013 - 12:53 PM Rate Topic: -----

#1 pfunkx64   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-February 13

problem filling an array of struct

Posted 22 February 2013 - 07:25 PM

I'm am trying fill an array of structs with a loop where the data is being read from a file. For some reason, the initialization of the array is not working.I really appreciate any help.



#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <iomanip>
#include <cassert>

using namespace std;

//Constants
const int MAX_BOOKS = 500;
const int MAX_LENGTH = 100;

//struct declarations
struct BookData {
    string title;
    string author;
    int date;
};

 //function declaritions
void fillBookList( int& numOfBooks,ifstream& libraryFile);

int main(int argc, char* argv[]) {
    int numOfBooks = 0;

    BookData bookList[MAX_BOOKS];
    string thisAuthor;
    ifstream libraryFile;

    if(argc<2) {
        cerr<<"Not enough Arguments"<<endl;
        exit(-1);
    }

    libraryFile.open(argv[1]);
    assert(libraryFile);

    //Loop to fill array
    while(libraryFile.peek()!=EOF && numOfBooks < MAX_BOOKS) {
        getline(libraryFile, bookList[numOfBooks].title);
        libraryFile.ignore(255,'\n');
        getline(libraryFile ,bookList[numOfBooks].author);
        libraryFile.ignore(255,'\n');
        libraryFile>>bookList[numOfBooks].date; 
        numOfBooks++;
    }

    //Display menu
    return 0;
}




file format example below:

The Autobiography of Malcolm X
Malcolm X and Alex Haley
1965
Beloved
Toni Morrison
1987
Bury My Heart at Wounded Knee
Dee Brown
1970



**note depending on your compiler i think you may have to change the header <cassert> to <assert.h>, as is compiles in g++

Is This A Good Question/Topic? 0
  • +

Replies To: problem filling an array of struct

#2 jimblumberg   User is offline

  • member icon

Reputation: 5780
  • View blog
  • Posts: 17,684
  • Joined: 25-December 09

Re: problem filling an array of struct

Posted 22 February 2013 - 08:19 PM

In the following snippet why are you skipping the second line?

    while(libraryFile.peek()!=EOF && numOfBooks < MAX_BOOKS) {
        getline(libraryFile, bookList[numOfBooks].title);
        libraryFile.ignore(255,'\n');  // Why? 
        getline(libraryFile ,bookList[numOfBooks].author);
        libraryFile.ignore(255,'\n'); // Why?
        libraryFile>>bookList[numOfBooks].date;
        numOfBooks++;
    }



When you use ignore() you throw away the information. Do you realize that getline() retrieves the string to the end of the line, including the end of line character ('\n' ) ? This end of line character is discarded after retrieval.

Jim
Was This Post Helpful? 1
  • +
  • -

#3 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3826
  • View blog
  • Posts: 13,945
  • Joined: 08-August 08

Re: problem filling an array of struct

Posted 22 February 2013 - 08:31 PM

...and why isn't there an ignore after reading the date?
Was This Post Helpful? 1
  • +
  • -

#4 pfunkx64   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-February 13

Re: problem filling an array of struct

Posted 22 February 2013 - 09:07 PM

Thanks guys,

I was actually rewriting this from a nested loop using a 2D array with get() instead of getline was using ignore to move the read marker. not sure why i left it in there( wasnt thinking)

I actually figured it out. changed the last to a getline for the date, and this worked. Im still not sure though why it wouldn't let me associate it to a int instead of a string. any thoughts? i'll leave it this way for now and use atoi with a c-string casting, so i can use it later when i pass it a later function. thanks guys.
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3826
  • View blog
  • Posts: 13,945
  • Joined: 08-August 08

Re: problem filling an array of struct

Posted 22 February 2013 - 10:13 PM

getline uses a string.
Was This Post Helpful? 0
  • +
  • -

#6 pfunkx64   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-February 13

Re: problem filling an array of struct

Posted 23 February 2013 - 12:53 PM

I wasn't using getline prior I was using what is listed in the follow snippet:

struct BookData{
string title;
string author;
int date;
};


BookData bookList[MAX_BOOKS];

libraryFile>>bookList[numOfBooks].date;






for some reason this was not working, in the loop. Once i switched to a getline() function the loop completes.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1