🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.comp.lang.learn.c-c++
Article #328

Re: newbie: strings versus char*

#328
From: Rich Churcher
Date: Tue, 28 Dec 1999 00:00
87 lines
3608 bytes
tagus18@my-deja.com writes:

> [I have read two online c++ tutorials and found at least three usenet
> threads concerning this issue, but I am still unable to solve this
> problem. Therefore, please forgive me for bringing up this issue
> again... I have read the c++ faq-lite, too :-)...)

Not to belabour the issue, but it wouldn't hurt to read the FAQ for
this newsgroup too :o)  see URL, below.

 
> I am using turbo c++ 3.0 for DOS. (I know it is old but I suppose that
> it should be possible to do this sort of things.)

Turbo C++ 3.0 is moving out of the realm of "old" and into the arcane
land of "ancient".  You really can't expect it to compile code that
complies with many sections of the new C++ standard.  I suggest trying 
one of the free compilers for comparison - GCC gets my vote.  If you
need a DOS compiler, go with DJGPP.

 
> I want to prompt the user for input and allow her to type as much as he
> wants to (and afterwards store the inputed text in a string / char*).
> 
> Following the advice given to previous posters, I tried to include
> string.h and create a string object of undefined length. But none of
> the three constructors which are available do this: the first creates a
> string of predefined length (it is passed as an argument to the
> constructor), the second converts a null-terminated C-string to a
> "string" object of this class and the third is a copy constructor.

Sounds like a non-standard string class, presumably provided by
Borland.


> Previously I had tried to create an array of characters in the free
> store and use cin:
> 
> char* text = "";
> cin >> text;
> 
> This allowed the user to enter 127 characters(if I am not mistaken),

You are!  This creates a char pointer which points to an empty string
literal.  Attempting to modify something like this is undefined, so
don't try it.


> but then the computer just beeped (I suppose that the keyboard was
> full!).

I think they sell kits for fixing full keyboards.  An adhesive earth
wire attaches to the home keys and drains all the excess data out into 
the ground.  $9.95 at any good electronics retailer.[1]


> The inserted characters were stored correctly, it seems, in the
> array "text". What can I do? I suppose that increasing the size of the
> keyboard buffer isn't the solution, because that would imply setting a
> maximum value again.

Try this for starters:

  char text[ 128 ];
  cin >> text;

This is your 127 character buffer, with one extra for the terminating
'\0'.

With a compiler that supports the newer standard string type, this
will work:

  #include <iostream>
  #include <string>

  int main()
  {
    std::string s;
    std::getline( std::cin, s );
  }


[1] Not available in US stores.

-- 
Cheers,  // The alt.comp.lang.learn.c-c++ FAQ:
Rich.    // http://www.raos.demon.co.uk/acllc-c++/faq.html
Technical Details
Message-ID: <m2hfh35xij.fsf@mpx.com.au>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <8485tm$cfg$1@nnrp1.deja.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 1061d2,fcdf60e52711dfec
X-Google-Attributes: gid1061d2,public
X-Google-Thread: 114592,fcdf60e52711dfec
X-Google-Attributes: gid114592,public
From: Rich Churcher <churchmouse@mpx.com.au>
Subject: Re: newbie: strings versus char*
Date: 1999/12/28
Message-ID: <m2hfh35xij.fsf@mpx.com.au>
X-Deja-AN: 565582323
References: <8485tm$cfg$1@nnrp1.deja.com>
X-Complaints-To: abuse@mpx.com.au
X-Trace: news1.mpx.com.au 946365914 6344 198.142.210.156 (28 Dec 1999 07:25:14 GMT)
Organization: Optus Internet
NNTP-Posting-Date: 28 Dec 1999 07:25:14 GMT
Newsgroups: alt.comp.lang.learn.c-c++,borland.public.cpp.non-technical