首先,这是一个“家庭作业”问题,因此 vector 库和字符串库是禁止使用的。我正在尝试了解 C++ 的基础知识。
我对这段代码的意图是制作和使用一个字符串数组。换句话说,单词列表。
当我运行这段代码时,我得到了一堆废话。
如果有更好的方法在 C++ 中制作单词列表,我很想听听。
const int cart_length = 50;
const int word_length = 50;
int main()
{
char cart_of_names[cart_length][word_length];
float cart_of_costs[cart_length];
char name[word_length];
cout << "enter the name of the first item: ";
cin >> name;
for(int i=0; i<word_length; i++)
{
cart_of_names[0][i] = name[i];
}
cout << endl;
cout << "that is: ";
for(int x=0; x<word_length; x++)
{
cout << cart_of_names[0][x];
}
cout << endl;
return 0;
}
最佳答案
如果输入的字符串长度不是 50 个字符 (cart_length
),则名称中的有效字符少于 50 个。你应该有一个 if(cart_of_names[0][x]==0) break;
在你的第二个循环中。
关于c++ - 我可以在 C++ 中做一个多维字符数组吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1521062/