Solved

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

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
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…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

830 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