std::cin.getline(f_name, 10)
If I have fo开发者_开发百科r example the following statements:
char f_name[11];
std::cin.getline(f_name,10);
Does thia mean: * Declare a string with 11-characters wide? * Read the entered line and pass it as the value for "f_name"?
Thanks.
Yes, you are correct!
char f_name[11];
declares the array f_name
with 11
elements.
std::cin.getline(f_name,10);
prompts for the value to be entered, which then stores it in f_name[11]
.
Yes, and no.
char f_name[11];
declares an array of char
with 11 elements. It's not really a string - you could consider it a "C string" if it had a NUL ('\0'
) at the end (which it does not).
std::cin.getline(f_name, 10);
May or may not read the entire entered line, because it only reads up to 9 chars. You need not make the buffer larger than the value given to cin.getline
.
Unless you have a specific reason not to, use std::getline
to read a line in C++. An example below.
#include <string>
std::string line;
std::getline(std::cin, line);
精彩评论