Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Modify static variables within a class in VC++ 3 95
TTimer no longer functioning 7 84
FMX enumerated colours 2 84
VS2015 Redefinition errors 4 23
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

929 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

12 Experts available now in Live!

Get 1:1 Help Now