Solved

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

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

743 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

10 Experts available now in Live!

Get 1:1 Help Now