Solved

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

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

Title # Comments Views Activity
C language IDE – Compilers installation 14 91
Getting IP address 8 124
Meaning of "exceptions()" in "ist.exceptions( ist.exceptions() | ios_base:: badbit);" 3 113
Outlook 13 100
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 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.

739 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