while(getline(bookFile,books[count].title,';'))
{
bookFile>>books[count].year;
bookFile.ignore(1);
bookFile>>books[count].author;
bookFile.ignore(1);
bookFile>>books[count].price;

count++;
}

the code seems to just ignore the deliminating character and enter the whole first line into the title member of the books struct. I can not figure out why, any help would be appreciated .. thanks

Recommended Answers

All 4 Replies

You're mixing formatted and unformatted input from the file, which isn't a very good idea. What does your file look like? Post 1 or 2 lines from it .

You're mixing formatted and unformatted input from the file, which isn't a very good idea. What does your file look like? Post 1 or 2 lines from it .

harry potter 3 ; 1998; J K R; 19.99
harry potter 1; 1995; J K R; 15.99
harry potter 2; 1998; J K R; 19.99
harry potter 5; 2002; J K R; 16.99
harry potter 4; 2001; J K R; 18.99
harry potter 6; 1998; J K R; 19.99
harry potter 9; 1995; J K R; 15.99

or 7 :)

Anyway, how I'd do it is:
- get one line/row (until \n )
- parse this row to get the columns. Do this by splitting on the ;

Here's an (untested) example using a stringstream:

std::string line = "";
while(getline(bookFile,line)) // get a complete line
{
    stringstream line_stream(line); //store the line in a stringstream
    getline(line_stream, books[count].title, ';'); 
    getline(line_stream, books[count].date, ';');
    getline(line_stream, books[count].author, ';');
    getline(line_stream, books[count].price, ';');
    count++;
}

I hope you get the basic idea.

thanks alot for the help, I will try your suggestion..

however, I am still a little confused, was there anything wrong with the code I used?

and is there anyway I could fix it (albeit, maybe not as good as yours) to work?

Would love to know where I went wrong..

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.