Is it possible to use cin.getline()
without denoting an array size?
That is, with the following pseudo-code:
char array[15];
cin.getline(array,'.'); // '.' Is the delimiter.
Will this work?
Is it possible to use cin.getline()
without denoting an array size?
That is, with the following pseudo-code:
char array[15];
cin.getline(array,'.'); // '.' Is the delimiter.
Will this work?
Yes: join the 20th (!) century:
std::string str;
std::getline(std::cin, str, '.');
What will happen:
This will compile and run, but your delimiter won't work and you will get undefined behaviour. Furthermore you will loose 1) exception handling, when the array size is not given. (When the input is higher than the size, you will get a 2) "stack corrupted" run-time error). With the size given, the rest length will be cut.
1) Exception Handling:
If you define a size for your array, it will substring the user-input, so that you are not writing more characters into your string than it has room allocated. (i.e. more than 15) (This type of substring will only be available, when you define your size.
2) Stack Corrupted Error:
Without defining the size, you will get a "stack corrupted error" which means, that you try to write more than you have allocated.
3) Running your code: Your code will run, but you will have undefined behaviour at runtime.
http://coliru.stacked-crooked.com/a/64b881aa9f70c21d
Doing it the right way: As "lightnes races in orbit" already mentioned, this is the way to go:
std::string str;
std::getline(std::cin, str, '.');
more to read: http://www.cplusplus.com/reference/string/string/getline/