Solved

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

Posted on 2003-11-08
2
214 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

830 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