I'm trying to ask the user to enter a random word, but when I go to try to store it, the normal cin
isn't working and just seems to be confusing ncurses. I've tried other functions like wgetstr()
but it takes a char and not a string. I've been attempting multiple conversion functions like c_str()
but nothing. Does anybody have any tips?
CodePudding user response:
getstr()
family of functions do return null-terminated strings, not just a single character. It's C library, there isn't any std::string
type.
You must supply a suitable large buffer for the functions. It is more safe to use getnstr
which limits the number of read characters.
char buffer[256];
int result = getnstr(buffer,sizeof(buffer)-1);//Make space for '\0'
assert(results!=-1);
buffer[sizeof(buffer)-1] = '\0'; // Force null-termination in the edge case.
size_t length = strlen(buffer);
I am not 100% sure whether the limit on n
read characters includes the null byte, if it works as strncpy
, it might not and in that case it's better to leave a space for it and add it explicitly.