[英]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 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");
}
}
}
}
根據每個小組有多少學生,小組班上如何有不確定數量的變量?
您不能有“無限”數,因為您將受到可用內存的限制。 但是, 可以有一個在編譯時未指定的數字。 將學生數組更改為學生向量:
std::vector<student> allStudents;
看來您正在尋找諸如std::vector<student>
類的數據結構。
實際上,每個組中不需要無限數量的學生。 您真正需要的是每組中有無限數量的學生。 為此,您可以在group
類中使用任何標准容器,例如std::vector<student>
或std::list<student>
或替換group
類。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.