我的代码是:突然额外的索引值
char randomChar(int randMax) {
return (65 + rand() % randMax);
}
int main() {
srand(time(NULL));
const int SIZE = 4;
const int LETTERS = 6;
char code[SIZE];
for (int i = 0; i < SIZE; i++) {
code[i] = randomChar(LETTERS);
}
cout << code;
return 0;
}
的问题是,即使我已经设定的代码阵列是长度为4时,randomChar
函数返回4个字母+一些额外的随机标志。
摆脱那些丑陋的字符数组,并使用'std :: string'。你不会再有这种问题了。使用C++的美妙之处,将指针和普通数组包装到安全的操场中。 – stefan 2013-02-14 13:00:32
我们还没有在我的课上学到指针:/ – user1784297 2013-02-14 13:19:11
更好。在学习C++的开始阶段,你不需要它。你不需要知道任何关于使用std :: string的指针。 – stefan 2013-02-14 13:21:17