gpt4 book ai didi

c++ - 显示一个三维结构数组

转载 作者:行者123 更新时间:2023-11-30 03:24:00 26 4
gpt4 key购买 nike

我正在尝试使用 3D 结构数组创建学校类(class)表。

struct SPS
{
string Class;
string Teacher;
int noStudents;
};

struct SPS array[3][4][5];

概念 View :

0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

我希望能够在特定位置输入数组,例如 [1][2][2]

这是我当前的代码,但它按预期运行。

cout << "Class:" << endl;
cin >> array[1][2][2].Class;
cout << "Teacher:" << endl;
cin >> array[1][2][2].Teacher;
cout << "Number of students:" << endl;
cin >> array[1][2][2].noStudents;

我正在尝试打印数组,但它没有在输出中显示:

for (int a = 0; a<3; a++)
{
for (int b = 0; b<4; b++)
{
for (int c= 0; c<5; c++)
{
printf("%d\t", array[a][b][c]);
}
cout << "\n";
}
cout << "\n";
}

如有任何帮助,我们将不胜感激。

最佳答案

你不能像那样只输出整个结构。 printf()不是魔法。编译器应该如何知道要使用什么格式?

只需使用 cout<<输出结构的每个成员:

for (int a = 0; a<3; a++)
{
for (int b = 0; b<4; b++)
{
for (int c= 0; c<5; c++)
{
cout << array[a][b][c].Class << ' '
<< array[a][b][c].Teacher << ' '
<< array[a][b][c].noStudents << '\n';
}
cout << "\n";
}
cout << "\n";
}

关于c++ - 显示一个三维结构数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50081591/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com