Solved

How to ignore certain white spaces when reading an object of strings.

Posted on 2003-11-08
2
211 Views
Last Modified: 2010-04-01
Hello experts:

I am trying to read an object into a list and
I can't figure out how to ignore white space in the title of a Book object. (See below for overloaded >> operator function.) I heard that it might be better to read the object members into stringstream with a getline but can't figure out exactly how.  Any suggestions would be greatly appreciated (have spent days trying to figure this out!).

Here is one line from the data file I'm trying to read into an object:
Herbert Schildt,  C: The Complete Reference 0-07-212124-6  Mcgraw Hill 2000 805 24.99 paperback

The data members of my Book object are:
string author_first_name;
string author_last_name;
string title;
string isbn;
string publisher;
string pub_year;
string page_count;
string price;
string book_type;
....................................................................
      

istream& operator>>(istream& infile, Book& a_book)
{
      infile >> a_book.author_first_name >> a_book.author_last_name
               >> a_book.title >> a_book.publisher >> a_book.isbn
               >> a_book.pub_year >> a_book.page_count
               >> a_book.price >> a_book.book_type;            

      return infile;
}

0
Comment
Question by:deekakes
2 Comments
 
LVL 6

Accepted Solution

by:
GaryFx earned 80 total points
ID: 9706971
The specific answer to your question is that you can turn off the skipping of whitespace by unsetting the skipws flag:
   infile.unsetf(ios::skipws);

But that doesn't really sovle the problem.  The core problem is that input syntax is hopelessly inadequate.  Suppose you had to read in a listing for Fahrenheit 451:

Ray Bradbury, Fahrenheit 451 0345342968 Ballentine Books 1995 208 6.99 paperback

How do you know where the book title ends and the ISBN begins?  You can figure out because the title is familiar or because you know that ISBNs are typically very big, but it takes a lot more work for the computer to figure out that 451 is part of the title and not part of the ISBN.

Keep in mind that there is (in general) no standard formatting for ISBNs; they can appear with or without the hyphens, spaces instead of hyphens, and the spaces/hyphens can be in different positions.   You may be able to make some assumptions about the ISBNs for your purposes, but it's still tricky.

So you either need to be prepared to put significantly more effort into the parsing, or else you need to change the input format so that it's much, much easier to parse.

Gary
0
 

Author Comment

by:deekakes
ID: 9707080
I think I'll reformat the input file.  It will definitely make more sense to put each data member on a separate line or use commas as delimiters if  they're on a single line.

Thank you....
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now