Solved

Overloading  istream &operator >> (istream &InStr, BookLoan &aLoan)

Posted on 1997-05-29
2
1,154 Views
Last Modified: 2006-11-17

I wrote the following code to overload the input operator ‘>>‘ for interactive input which at first trial works fine.
istream &operator >> (istream &InStr, BookLoan &aLoan)
{
      Strings
            aString;

      cout << endl << "Enter the first name :";
      aString.GetLine(InStr,100, '\n');
      aLoan.FirstName_ = aString;
      cout << endl;


      cout << endl << "Enter the last name :";
      aString.GetLine(InStr);
      aLoan.LastName_ = aString;
      cout << endl;

return InStr;
}

However, I used the overload operators as follows:

do{

cin >> aLoan;
cout << aLoan;

cout << “ Another ? “;
cin >> Response
while (Response = y) ;

This still works however the second and subsequent iterations called for only print the last name. I have done a bit of stepping through the code and I have traced the fault to calling the  function GetLine(InStr). In turn this calls a function In.getline as follows:

istream& Strings::GetLine(istream& In, unsigned MaxChars, char UntilChar)
{
  char
    Buffer[MaxLength];                        // temporary string storage

  In.getline(Buffer,                          // read in the line
             (MaxLength < MaxChars) ? MaxLength : MaxChars,
             UntilChar);

  Length_ = strlen(Buffer);                   // set Length_

  delete [] String_;                          // remove any prior array

  String_ = new char [Length_ + 1];           // allocate new array
  assert(String_ != 0);                       // verify, and
  strcpy(String_, Buffer);                    // copy the buffer into it

  return In;                                  // return the istream
}
In the first iteration In.getline() is called and then goes to DOS  for the user to type the FirstName, Length_ is then set. In the second & subsequent iterations the program  still calls In.getline() however tracing through shows that the program does not go to DOS to receive the string. Instead it steps to Lenght_ which has the assigned value ). I suspect there is something in the buffer still though I cannot see it with ‘watch variables’ nor does it print to the ostream as I say,

any help greatly appreciated,

regards

David Klemitz (es00749@ozemail.com.au)



0
Comment
Question by:dklemitz
[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 3

Accepted Solution

by:
LucHoltkamp earned 100 total points
ID: 1163747
Try to use string (cstring.h) instead of Strings. Works great! (They already have operator>> defined, besides its Borland's recommended class).
.luc.
0
 

Author Comment

by:dklemitz
ID: 1163748
Thanks for your reply,

I will check this out. I solved the problem by writing

cin.get() as the carrage return from  entering the previous
object was still in the stream. cin.get()  'consumed' this
and allowed the inputing to proceed as normal

thanks agian

David  es00749@ozemail.com.au
0

Featured Post

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

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…
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…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

631 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