We want to implement a flex like system to carry information about students and the courses they are currently enrolled in. The input data will be present in two text files called student.txt and courses.txt. The structure of the student.txt file will be (rollnumber, name, date of birth , batch, department) The structure of the courses.txt file will be (rollnumber, course code, coursename, credit hours) As shown above both the files will be sorted by the rollnumber and a student could be enrolled in any number of courses. Your system should be able to cater for any number of students. You have to i. Decide the classes needed to carry the above info (you might need a dynamic array of courses within the student) ii. Read the information present in the files iii. Display the students data properly on console.
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
struct courses
{
string course_code; //to store course code
string course_name; //to store course name
int credit_hours; //to store credit hours
};
struct student
{
string roll_no; //to store roll number
string name; //to store name
string DOB; //to store date of birth
int batch; //to store batch
string department; //to store department
vector<courses> v; //vector to store courses opted by the student.
};
int main() {
vector<student> data;
ifstream fin;
fin.open("student.txt");
while(!fin.eof())
{
string r,n,d,b,m;
student s;
fin>>r; //reading roll number from the file
r.erase(r.size()-1); //removing the comma
s.roll_no = r; //assigning the roll number
getline(fin,n,','); //reading the name from the file
s.name = n; //assigning the name
fin>>d; //reading date of birth from the file
d.erase(d.size()-1); //removing the comma
s.DOB = d; //assigning date of birth
fin>>b; //reading the batch from the file
b.erase(b.size()-1); //removing the comma
int x = 0; //converting string to integer
for(int i=0;i<b.size();i++)
{
int digit = b[i] - '0';
x = x*10 + digit;
}
s.batch = x; //assigning batch
fin>>m; //reading major from the file
s.department = m; //assigning major
data.push_back(s); //adding the object to the vector
}
fin.close(); //closing the file
fin.open("courses.txt");
int i = 0;
while(!fin.eof())
{
string roll;
fin>>roll;
string x = roll;
while(x == roll)
{
courses c;
string code, name;
int credit;
fin>>code; //taking input the course code
cout<<code<<endl;
code.erase(code.size()-1); //removing the comma
c.course_code = code; //assigning the course code
getline(fin,name,',');
c.course_name = name; //assigning the course name
fin>>credit; //taking input credit hours
c.credit_hours = credit; //assigning the credit hours
(data[i].v).push_back(c); //adding to the student courses
if(!fin.eof())
fin>>x; //reading student roll number
}
i++; //after reading all the values for 1 student we move to next student
}
for(int i=0;i<data.size();i++)
{
cout<<"Student Rollnumber: "<<data[i].roll_no<<endl;
cout<<"Name: "<<data[i].name<<endl;
cout<<"DOB: "<<data[i].DOB<<endl;
cout<<"Batch: "<<data[i].batch<<endl;
cout<<"department: "<<data[i].department<<endl;
cout<<"Courses Enrolled in:\n";
vector<courses> aux = data[i].v;
for(int j=0;j<aux.size();j++)
{
cout<<aux[i].course_name<<" "<<aux[i].course_code<<" "<<aux[i].credit_hours<<endl;
}
cout<<endl;
}
return 0;
}
Comments
Leave a comment