Solved

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

Posted on 1997-05-29
2
1,136 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
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

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

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

14 Experts available now in Live!

Get 1:1 Help Now