Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2003-11-08
2
Medium Priority
?
218 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 6

Accepted Solution

by:
GaryFx earned 320 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

670 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