C++ Make a Char Array have a Value of the string -
for program, have highscore section. input string, how can make string equal char array? fyi: string playersname
filled out name. here code:
class highscore { public: char name[10]; ...[code]... } ...[code]... // declare variables *the playersname filled out already* string playersname = ""; ...[code]... // how can data[playerscore].name equal playersname string? cin.get (data[playerscore].name, 9); // know cin.get not in code since players name string
you can use std::string::copy
member function, like
// length of destination buffer won't overflow size_t length = sizeof data[playerscore].name; // copy string content char buffer playersname.copy(data[playerscore].name, length); // add `'\0'` @ end data[playerscore].name[length] = '\0';
Comments
Post a Comment