Solved

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

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

777 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