对于我的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 application\n";
cout << "2) Enter a Student\n";
cout << "3) Display Students\n";
cout << "4) Display Groups\n";
cout << "5) Output groups as text file\n";
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.0\n";
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 << " students\n";
}
else
{
allStudents[studentHeadCount] = typeName();
allStudents[studentHeadCount].setStudentNumber(studentHeadCount);
studentHeadCount++;
}
break;
}
case 3:
cout << "Current list of students:\n\n";
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");
}
}
}
}
我怎样才能在小组课程中拥有无限数量的变量,具体取决于每组学生的数量?
答案 0 :(得分:7)
您不能拥有“无限”号码,因为您将受到可用内存的限制。但是,可以具有在编译时未指定的数字。将学生数组更改为学生矢量:
std::vector<student> allStudents;
答案 1 :(得分:1)
您似乎正在寻找std::vector<student>
等数据结构。
答案 2 :(得分:1)
您实际上并不需要每组中有无数的学生。你真正需要的是每组中无限期的学生数量。为此,您可以在std::vector<student>
类中使用任何标准容器,例如std::list<student>
或group
,或者替换group
类。