overloading >> operator

istream &operator>>(istream &In,StrC &Copy)
{
      char* Buffer;
      In.getline (Buffer,'\n');
      delete [] Copy.TextBuffer;
      Copy.TextBuffer = new char[strlen(Buffer)+1];
      strcpy(Copy.TextBuffer,Buffer);
      delete [] Buffer;
      return In;
}

You can see from that example what I am trying to do, however, it does not work.  It seems you can't use
a char* inside of a getline like this.  What can I do to insure my Copy.TextBuffer not only does not go outside
of its bounds, but enlarges to accept more than its current bounds when needed?
LVL 8
List244Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
>>It seems you can't use a char* inside of a getline like this.

Not an *unitialized* one - use either

istream &operator>>(istream &In,StrC &Copy)
{
     char* Buffer = new char[MAX_SIZE];
     In.getline (Buffer,'\n');
     delete [] Copy.TextBuffer;
     Copy.TextBuffer = new char[strlen(Buffer)+1];
     strcpy(Copy.TextBuffer,Buffer);
     delete [] Buffer;
     return In;
}

or *preferrably*

istream &operator>>(istream &In,StrC &Copy)
{
     string Buffer;
     getline (In,Buffer);
     delete [] Copy.TextBuffer;
     Copy.TextBuffer = new char[Buffer.length()];
     strcpy(Copy.TextBuffer,Buffer.c_str());
     return In;
}

Why not use a 'std::string', since it makes like *so* much easier?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
List244Author Commented:
Haha, JKR, you should know by now that I know I can use string...  So how does the string class work?
If it can accept strings like this, shouldn't I be able to?  Does the string class have a max size for use with
cin?
jkrCommented:
>>Haha, JKR, you should know by now that I know I can use string...  So how does the string class work?

That's why I was slightly surprised why you didn't use that in the 1st place ;o)

You can just use it as in my example above. You might also want to consider changing 'TextBuffer' in 'StrC' to a 'string' also, this will save you from all that error prone 'new', 'delete' and Ästrcpy()' stuff.

>>Does the string class have a max size for use with cin?

Theoretically somewhere around 2GB *g*
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

List244Author Commented:
Well, the main part of this class, StrC, is recreating the string class.  However, minus most of the functions,
and with a few extra.  If I used the string class inside of it, I would basically just be parsing a string, and that
is too easy, I want to do it without strings, more fun. :P
jkrCommented:
OK, then replacing that by a string would probably defeat the purpose. BTW, I just noticed that you were omitting the 'streamsize' parameter, so that should be

     char* Buffer = new char[MAX_SIZE];
     In.getline (Buffer, MAX_SIZE, '\n');
List244Author Commented:
Yeah, I knew what you meant there.  Thanks again for the help.
itsmeandnobodyelseCommented:
Maybe you should change to that:

istream &operator>>(istream &In,StrC &Copy)
{
     char Buffer[1024];
     In.getline (Buffer, sizeof(Buffer), '\n');
     Copy = Buffer;
     return In;
}

>>>>     Copy = Buffer;

To make that work you need an appropriate assignment operator

StrC& StrC::operator=(const char* psz)
{
     delete [] TextBuffer;
     TextBuffer = new char[strlen(psz)+1];
     strcpy(TextBuffer, psz);
     return *this;
}

Regards, Alex
List244Author Commented:
Itsmeandnobodyelse:

I have:

istream &operator>>(istream &In,StrC &Copy)
{
      char Buffer[1001];
      In.getline(Buffer,1001,'\n');
      delete [] Copy.TextBuffer;
      Copy.TextBuffer = new char[strlen(Buffer)];
      strcpy(Copy.TextBuffer,Buffer);
      return In;
}

What benefits come from changing it to:

istream &operator>>(istream &In,StrC &Copy)
{
      char Buffer[1001];
      In.getline(Buffer,1001,'\n');
      Copy = Buffer;
      return In;
}

PS: I already have an overloaded = which works similar to yours.
itsmeandnobodyelseCommented:
>>>> What benefits come from changing it to

A simple benefit is, that it is shorter and much less error-prone. Furthermore, it is object-oriented programming (OOP). OOP means that the objects provide suitable member functions, e. g. assignment operators, that you can use them without having to know their internal implementation. That's why members like StrC::TextBuffer normally were defined private. And it isn't recommendable to modify data members directly from a non-member operator function. If you decide to change your string class. e. g. to support wide characters or reference counting, the operator>> functions don't need to be changed if they only use standard interfaces.

>>>> In.getline(Buffer,1001,'\n');

Use
 
    In.getline (Buffer, sizeof(Buffer), '\n');

instead. Then, you have to change only one parameter.


Regards, Alex
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.