| eibarra (10) | |||
|
I'm new to arrays with dynamic sizes. That being said, I'm working with a simple code which seems to work just fine, my only concern is that once I display the 'char array', not only displays the user's inputs but some extra data, symbols and what not. Can someone explain why, if to my understanding the first user's input already sets the size of the array, and how can this be fixed?
| |||
|
|
|||
| Disch (8615) | |||
|
'cout' has no way to know how big the array is. You're just giving it a chunk of data and saying "print this". It determines where to stop printing by looking for a special "null terminator". Which basically is the literal value of zero. This marks the end of a C-style string (a char array). This also means your array has to be 1 larger than the desired size, because you need to have that extra 1 char to hold the null terminator.
Of course this would be much easier if you use actual strings instead of char arrays. But I'm going to assume you're doing it the hard way because this is a homework assignment or something. | |||
|
|
|||
| eibarra (10) | |
|
So in arrays there is no way to input a "null terminator"? I want to use a dynamic size array because of the fact that its size may very whenever I call this function (not a function now but planning on making it one) | |
|
|
|
| Zhuge (2974) | |
| There is a way to put the null terminator in the array. It is '\0', a character with the numeric value of 0, as Disch showed on line 11 of his example. | |
|
|
|
| Smac89 (199) | |||
|
@Disch I'm actually surprised this is not done automatically. E: Yes it is done automatically
| |||
|
Last edited on
|
|||
| eibarra (10) | |
| got it! that null terminator had me stuck. THANK YOU BOTH very much | |
|
|
|
| eibarra (10) | |
|
@Smac89 using your code I'm still getting does random characters when the array is displayed. How would you fix that? | |
|
Last edited on
|
|
| Smac89 (199) | ||||
|
That's weird, I guess do exactly what was suggested by Disch so on line 17 above the for-loop, specify array[size] = 0 E: Output
| ||||
|
Last edited on
|
||||
| Disch (8615) | |
|
array[size] is out of bounds. You should do array[size-1] = 0;But again... you can always just use strings and then you don't have to deal with this junk. | |
|
|
|
| eibarra (10) | |
|
@Disch can strings have dynamic sizes too? would it be the same process done for the char type array? something like string array= new string[size]??
| |
|
|
|
| Zhuge (2974) | |
| You don't even need to dynamically allocate for a string; it handles all that for you. Just assign to it and it will handle the rest. | |
|
|
|
| eibarra (10) | |
I'm a little confused , I thought every time a string is declared it has to be followed by its size in square brackets, like so string a[50] variable 'a' type string with 50 parameters. Can anyone provide an example? | |
|
|
|
| Disch (8615) | |||
An individual string can hold any number of characters.
Plus you don't have to worry about deleting / cleaning up memory... or null terminators... or exceeding your array size... etc. string a[50]; This gives you an array of 50 strings. This is perfectly legal to do, but if all you need is one string, it's entirely unnecessary.
| |||
|
|
|||