-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstudent_group_generator.cpp
More file actions
120 lines (99 loc) · 2.75 KB
/
student_group_generator.cpp
File metadata and controls
120 lines (99 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <fstream>
using namespace std;
class GroupMaker {
private:
int totalStudents;
string students[100];
string groups[20][5];
public:
GroupMaker(int total) {
totalStudents = total;
if (totalStudents % 5 != 0) {
cout << "Total students must be divisible by 5"<<endl;
exit(0);
}
}
void inputNamesAndSaveToFile(string filename) {
ofstream file(filename);
if (!file.is_open()) {
cout << "File could not be created!\n";
exit(0);
}
cout << "Enter names of " << totalStudents << " students:"<<endl;
string name;
for (int i = 0; i < totalStudents; i++) {
cout << "Student " << i + 1 << ": ";
getline(cin >> ws, name);
if (name.empty()) {
i--;
continue;
}
students[i] = name;
file << name << "\n";
}
file.close();
cout << filename << " file created with " << totalStudents << " names"<<endl;
}
void inputNamesFromFile(string filename) {
ifstream file(filename);
if (!file.is_open()) {
cout << "File open failed!\n";
exit(0);
}
int i = 0;
string name;
while (getline(file, name) && i < totalStudents) {
students[i] = name;
i++;
}
file.close();
}
void shuffleStudents() {
for (int i = totalStudents - 1; i > 0; i--) {
int r = rand() % (i + 1);
swap(students[i], students[r]);
}
}
void makeGroups() {
int g = 0, pos = 0;
for (int i = 0; i < totalStudents; i++) {
groups[g][pos] = students[i];
pos++;
if (pos == 5) {
pos = 0;
g++;
}
}
}
void printGroups() {
cout << "===== Generated Groups ====="<<endl;
int totalGroups = totalStudents / 5;
for (int g = 0; g < totalGroups; g++) {
cout << "Group " << g + 1 << ":\n";
for (int j = 0; j < 5; j++) {
cout << " - " << groups[g][j] << "\n";
}
cout << "\n";
}
}
};
int main() {
srand(time(0));
int total;
cout << "Enter total number of students (5 to 100, divisible by 5): ";
cin >> total;
if (total < 5 || total > 100 || total % 5 != 0) {
cout << "Invalid input! Choose 5 to 100 divisible by 5."<<endl;
return 0;
}
GroupMaker gm(total);
gm.inputNamesAndSaveToFile("students.txt");
gm.shuffleStudents();
gm.makeGroups();
gm.printGroups();
return 0;
}