C++ 获取字符数组C++;

C++ 获取字符数组C++;,c++,C++,如果我运行此代码,程序会要求用户输入消息,因此如果用户输入“播放” 我想做一个int变量,其中包含用户输入的字符数 或 数组的最大索引 谢谢阵列在编译时具有固定容量: array[0] = P array[1] = l array[2] = a array[0] = y 或在运行时动态分配: char text[1024]; 如果必须使用字符数组,则需要使用str*()函数系列: std::string word; cout << "Enter word: "; cin >

如果我运行此代码,程序会要求用户输入消息,因此如果用户输入“播放”

我想做一个int变量,其中包含用户输入的字符数 或 数组的最大索引


谢谢

阵列在编译时具有固定容量:

array[0] = P
array[1] = l
array[2] = a
array[0] = y
或在运行时动态分配:

char text[1024];
如果必须使用字符数组,则需要使用
str*()
函数系列:

std::string word;
cout << "Enter word: ";
cin >> word;
cout << "Length of word: " << word.length() << "\n";

现在,输入“world”,数组将溢出

char数组[]{}
不是标准的c++。它使用一个gcc扩展。而是使用
std::string
cout << "Enter text length: ";
size_t length;
cin >> length;
char *text2 = new char[length];
std::string word;
cout << "Enter word: ";
cin >> word;
cout << "Length of word: " << word.length() << "\n";
char hello[]="hello";
cout << "Length is: " << strlen(hello) << "\n";
char word[2];
cin >> word;