我正在尝试使用3D结构阵列创建学校课程安排计划。
struct SPS
{
string Class;
string Teacher;
int noStudents;
};
struct SPS array[3][4][5];
概念视图:
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";
}
任何帮助都将不胜感激。
答案 0 :(得分:2)
你不能像这样输出整个结构。 <section class="main-section" >
<div class="p-grid-isotope" >
<div class="p-item grid-sizer" ></div>
</div>
</section>
<button type="submit" id="upload-img" class="following-button" style="color: white; margin-top: 30px; margin-left: 18%" onclick="uploadImage()"><span class="icons i1"><i class="fa fa-upload" aria-hidden="true"></i></span>Upload to account</button>
不是魔法。编译器应该如何知道要使用哪种格式?
只需使用+---------+-----------------------+-----------------+
| Existing| Source of new element | Expected result |
+---------+-----------------------+-----------------+
| [] | [1] | [1] |
| [1,2] | [3,4] | [3, 4] |
| [1,2,3] | [3,4] | [4] |
+---------+-----------------------+-----------------+
和printf()
输出结构的每个成员:
cout
答案 1 :(得分:0)
您应该只使用printf("%d\t", array[a][b][c])
,而不是使用cout
,您必须为数组中的每个位置获取Class,Teacher和noStudents。所以它应该像
cout << array[i][j][k].Class << ' '
<< array[i][j][k].Teacher << ' '
<< array[i][j][k].noStudents << '\n';
答案 2 :(得分:0)
我发现这个结构没有价值
struct SPS
{
std::string Class;
std::string Teacher;
int noStudents;
};
struct SPS array[3][4][5];
它是禁用的,被动的,并且没有提供有用的代码来帮助您完成其余的编程工作。
C ++可以轻松地使每个实例都能执行独特的功能。具体来说,您应该考虑在类中实现show(和fill)。这些函数使每个实例都能对应用程序有用...即“show()”,“dump()”,“saveToFile()”,“restoreFromFile()”等。
#include <iostream>
#include <sstream>
#include <string>
struct SPS
{
private: // for encapsulation
std::string Class;
std::string Teacher;
int noStudents;
public:
// class instance 'show's itself
std::string show()
{
std::stringstream ss;
ss << Class << ' ' << Teacher << " " << noStudents;
return ss.str();
}
// for this MCVE, a simple replacement for file i/o
void fill(int a, int b, int c)
{
std::string s =
std::to_string(a)
+ '.' + std::to_string(b)
+ '.' + std::to_string(c);
Class = "\n Class " + s;
Teacher = " Teacher " + s;
noStudents = c;
}
// add saveToFile(), restoreFromFile(), dump()
};
struct SPS array[3][4][5];
注意 - 不需要单字段获取者或设置者......这些都浪费了程序员的时间。
完成此MCVE,我提供:
class T601_t // a simple test
{
public:
T601_t() = default;
~T601_t() = default;
int exec(int , char** )
{
fill3dArray();
show3dArray();
return 0;
}
private: // methods
// specific to this MCVE, a quick and dirty fill
void fill3dArray()
{
for (int a = 0; a<3; a++)
{
for (int b = 0; b<4; b++)
{
for (int c = 0; c < 5; c++)
{
array[a][b][c].fill(a, b, c);
// record -----^^^^ self filling
}
}
}
}
// your code would enter the a, b, and c from user prompts?
// or perhaps a file read?
// specific to this MCVE, invoke the show method of
// each element of the array
void show3dArray()
{
for (int a = 0; a<3; a++)
{
for (int b = 0; b<4; b++)
{
for (int c= 0; c<5; c++)
{
std::cout << array[a][b][c].show() << '\n';
// record ------------------^^^^ self showing
// it is trivial for this cout to prefix the show
// with a triple for readability or diagnostics
}
std::cout << "\n";
}
std::cout << "\n";
}
}
}; // class T601_t
int main(int argc, char* argv[])
{
T601_t t601;
return t601.exec(argc, argv);
}