So everythings almost working perfectly. I set my 2d array to type
bool. Read in everything and mapped it instead of trying to do it while
its reading data in still. My last question is, how can I make my
courses_out file output correctly?
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct Students
{
string first_name, last_name;
int id, num_courses;
int course_ids;
};
struct Courses
{
string course_name;
int course_id;
};
int main()
{
Students student[500];
Courses course[25];
bool student_info[500][25];
//string course_name[25];
for(int x = 0; x < 500; x++)
{
for(int y = 0; y <25; y++)
student_info[x][y] = false;
}
ifstream courses_file;
courses_file.open("courses.txt");
if (courses_file.fail())
{
cerr << "Could not open courses.txt" << endl;
exit(1);
}
//students file = student name, id, number of classes, class id
ifstream students_file;
students_file.open("students.txt");
if (students_file.fail())
{
cerr << "Could not open students.txt" << endl;
exit(1);
}
//students output file = students name and courses they are in
ofstream students_out;
students_out.open("students_out.txt");
if (students_out.fail())
{
cerr << "Could not open students_out.txt" << endl;
exit(1);
}
//courses output file = courses and students enrolled in them
ofstream courses_out;
courses_out.open("courses_out.txt");
if (courses_out.fail())
{
cerr << "Could not open students_out.txt" << endl;
exit(1);
}
int i = 0, y = 0, x = 0, r = 0;
//Filling courses_name array with the names of the courses and
filling
student_info[499]
with the course id number
while(courses_file >> course.course_name >>
course.course_id)
i++;
while(students_file >> student[r].first_name >>
student[r].last_name
student[r].id >> student[r].num_courses)
{
//students_out << student[r].first_name << " "
<<
student[r].last_name << endl;
for(i = 0; i < student[r].num_courses; i++)
{
students_file >> student.course_ids;
//cout << student.course_ids;
student_info[student[r].id][student.course_ids] = true;
}
students_out << student[r].first_name << " " <<
student[r].last_name
<< endl;
for(x = 0; x < 25; x++)
if(student_info[student[r].id][x] == true)
{
students_out << course[x].course_name
<< endl;
}
if(student_info[student[r].id][r] == true)
{
courses_out <<
student[r].first_name << " " <<
student[r].last_name << endl;
}
}
students_out.clear();
students_out.close();
courses_out.clear();
courses_out.close();
return 0;
}
That is my current code. The problem here is, first course is cs101 so
it goes through the whole 2d array and prints out everyone who is in
cs101 based on which cells are true. Thats great but I stop right
there. I cant figure out how to print out the next course and then go
through the 2d array again.
I need the final output to look like
course1
student name
however many of them there are
....
....
course 2
student name
.....
....
....
course 3
......
and so on