Reading in to a character array of unknown size

1.2k views Asked by At

How do I read in a c-string in to a character array without knowing the size of the string that the user will enter ?

1

There are 1 answers

6
MikeD On

Without any code or further description of your issue it's hard to know what you're trying to achieve, but, one of the following might be appropriate for your needs:

  1. Use a preallocated array of some maximum size you know is greater than the number of characters that will be entered.

  2. Create an empty std::string, and then use the string "+=" operator for each character entered. Then you can convert back to an array using the c_str() method.