类中成员变量的数量不确定?C++
Indefinite Number Of Member Variables In A Class? C++
对于我的 GroupPicker 程序,我需要允许class group
为每个随机生成的数字提供无限数量的插槽,这些数字分配给每个输入的学生。代码:
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <time.h>
using namespace std;
class student
{
int m_studentNumber;
public:
string nameFirst;
string nameLast;
string nameFull;
int getStudentNumber() { return m_studentNumber; }
void setStudentNumber(int studentNumber) { m_studentNumber = studentNumber; }
};
class group
{
public:
};
ostream& operator<<(ostream& os, const student& s)
{
return os << s.nameFirst << ' ' << s.nameLast;
}
student typeName()
{
student bar;
cout << "Type in a student's first name: ";
cin >> bar.nameFirst;
cout << "Type in that student's last name: ";
cin >> bar.nameLast;
cout << "n";
bar.nameFull = bar.nameFirst + " " + bar.nameLast;
return bar;
}
void displayStudents(student listOfStudents[50], int studentHeadCount)
{
for (int i = 0; i < studentHeadCount; i++)
{
cout << listOfStudents[i].nameFull << endl;
cout << listOfStudents[i].getStudentNumber() << endl;
cout << "n";
}
}
void options()
{
cout << "Select what you want to do:n";
cout << "1) Exit applicationn";
cout << "2) Enter a Studentn";
cout << "3) Display Studentsn";
cout << "4) Display Groupsn";
cout << "5) Output groups as text filen";
cout << "n";
}
int main()
{
student allStudents[50]; // Having 50 students alone is ridiculous
bool endProg = 0;
int maxStudents;
int studentsPerGroup;
int optionSelect;
int studentHeadCount = 0;
int remainder;
int numberOfGroups;
cout << "GroupPicker 1.0n";
cout << "How many students are in the class?n" << "(Note: You cannot have more than 50 in this program)n";
cin >> maxStudents;
if (maxStudents > 50)
{
cerr << "Too many students! Exiting program...n";
system("PAUSE");
exit(1);
}
cout << "How many students per group?n";
cin >> studentsPerGroup;
if (studentsPerGroup >= maxStudents)
{
cerr << "You're kidding, right?n" << "Exiting program...n";
system("PAUSE");
exit(1);
}
while (endProg == 0) {
options();
cin >> optionSelect;
switch (optionSelect) {
case 1:
endProg = 1;
break;
case 2:
{
if (studentHeadCount == maxStudents)
{
cerr << "You can't enter more than " << maxStudents << " studentsn";
}
else
{
allStudents[studentHeadCount] = typeName();
allStudents[studentHeadCount].setStudentNumber(studentHeadCount);
studentHeadCount++;
}
break;
}
case 3:
cout << "Current list of students:nn";
displayStudents(allStudents, studentHeadCount);
break;
case 4:
{
if (studentHeadCount < studentsPerGroup || studentHeadCount < maxStudents)
{
cerr << "Invalid group parameters.n" << "Returning to main menu...n";
break;
}
else
{
cout << "Here are the groups:n";
numberOfGroups = maxStudents / studentsPerGroup;
}
}
case 5:
{
cout << "Saving groups to file...n";
ofstream studentGroups;
studentGroups.open("studentGroups.txt");
}
}
}
}
我如何在小组班级中拥有无限数量的变量,具体取决于每个小组有多少学生?
您不能有"无限"的数字,因为您将受到可用内存的限制。 但是,可以有一个在编译时未指定的数字。 将学生数组更改为学生向量:
std::vector<student> allStudents;
您似乎正在寻找一种数据结构,例如 std::vector<student>
。
你实际上不需要每个小组中有无限数量的学生。你真正需要的是每个小组中无限数量的学生。为此,您可以在group
类中使用任何标准容器,例如std::vector<student>
或std::list<student>
,也可以替换group
类。
相关文章:
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 不确定如何装饰我的C++库代码以在 C 中使用
- C++功能泄漏内存,我是C++新手,不确定如何解决
- "错误 C0000:语法错误,令牌"<EOF>"处出现意外$end,并且不确定
- 代码在 CodeSignal 中工作不正确。不确定这是否是我的代码缺陷
- 为什么以下代码是不确定的?
- 编译错误:"lvalue required as left operand of assignment" 不确定原因
- 误用指针会产生分段错误。我仍然不确定如何纠正它
- 我不确定为什么代码没有运行,它在编译时抛出错误,说它无法找到 PrintArray()
- 不确定出了什么问题(字符串输入)
- C++睡眠时间不确定
- 在C++中做一个场景问题,我不确定如何处理布尔和if循环
- 代码输出随机符号,我不确定出了什么问题
- 可变值变化,不确定原因
- 有没有办法让C++从 cin 中接收不确定数量的字符串?
- 尝试一次将单个字符读取到大小不确定的数组中
- 我不确定如何引用此神经网络训练方法中的权重
- 函数参数:不确定排序还是未排序?
- std::random_device如何生成不确定的随机数?
- 不确定有关入门C++参考的描述