>>, getline(), and ignore() all read some characters from the stream. They don't care what the user enters. If the code is to get 7 characters from the stream, then that function will continue until it has got 7 characters.
In other words: not "
if entered only one" but "It
will ignore next character"
agent max wrote: |
---|
this is not good programming practice, because 1: it dynamically allocates memory at run-time without requesting it, and 2: you can run into memory issues when you do that, particularly because you're using strings which also dynamically allocate memory. You're better off using a pointer to properly dynamically allocate the memory. |
VLA (variable length array) is worse than "not good". It is invalid in C++. Undefined behaviour.
Some compilers have support for VLA in C++, but then code is not portable. Yes, that is "not good practice".
C++ Standard Library offers
std::vector
. That is the C++ way to have a "VLA". The vector is "a pointer" with syntactic sugar. Sweet.
Your code sample has raw pointer from
new []
, but you don't show
delete []
. Nobody is "better off" by learning only small part of memory management. The 'new' statement is trivial. The 'delete' can be deviously hard. Plenty of crashes or memory leaks (that ultimately lead to crashes) are due to inappropriate or lacking delete.
Manual memory management is not good programming practice.
The
std::vector
does not forget the deallocation. It knows the tricky cases.