char arr[100];
cin.get(arr,100);
- Is this safe? Will the null-character be appended at the end even if I type more than 100 chars? or should I use
cin.get(arr,99)? - When I type ENTER, is the end-of-line character part of the array or not?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The answers to both of your questions can be found here, but to reiterate:
The
getmethod reads at mostn - 1characters. This means that the method expects the size of the buffer and not the number of characters to read. This method automatically appends a null character to the end.The newline character is not extracted or stored in the array.
You may also want to consider using
std::getlinewhich you can use in conjunction withstd::string.