I am having trouble with figuring out how to accept user input for this C++ assi
ID: 3909814 • Letter: I
Question
I am having trouble with figuring out how to accept user input for this C++ assignent. Any help would be extremely appreciated. Here is the code I have...
#include <iostream>
#include <vector>
using namespace std;
struct Course;
struct Student
{
string name;
vector <Course*> courses;
};
struct Course
{
string name;
vector <Student*> students;
};
void print_student(Student * s)
{
cout << s -> name << endl;
for (int i = 0; i < s -> courses.size(); i++)
{
cout << s -> courses[i] -> name << endl;
}
}
void enroll(Student *s, Course *c)
{
s -> courses.push_back(c);
c -> students.push_back(s);
}
int main()
{
Student s1;
s1.name = "John";
Course* c = new Course;
s1.courses.push_back(c);
s1.courses[0] -> name = "cis1";
c = new Course;
s1.courses.push_back(c);
s1.courses[1] -> name = "cis2";
print_student(&s1);
Course c1, c2;
c1.name = "cis3";
c2.name = "cis4";
Student s2, s3;
s2.name = "David";
s3.name = "charles";
enroll(&s2, &c1);
enroll(&s2, &c2);
print_student(&s2);
return 0;
}
Explanation / Answer
Given below is the code for the question. The code given by you does exactly whatever the questions asks for. But since you wanted extended functionality, I have a menu driven program to get user input and perform appropriate actions.
Please do rate the answer if it was helpful. Thank you
#include <iostream>
#include <vector>
using namespace std;
struct Course;
struct Student
{
string name;
vector <Course*> courses;
};
struct Course
{
string name;
vector <Student*> students;
};
void print_student(Student * s)
{
cout << "Student Name: " << s -> name << endl;
cout << "Courses enrolled: ";
for (int i = 0; i < s -> courses.size(); i++)
{
cout << s -> courses[i] -> name << " ";
}
cout << endl << endl;
}
void print_course(Course *c)
{
cout << "Course Name: " << c->name << endl;
cout << "Students enrolled: ";
for (int i = 0; i < c-> students.size(); i++)
{
cout << c -> students[i] -> name << " ";
}
cout << endl << endl;
}
void enroll(Student *s, Course *c)
{
s -> courses.push_back(c);
c -> students.push_back(s);
}
int findStudent(const vector<Student*> students, string name)
{
for (int i = 0; i < students.size(); i++)
{
if(students[i]->name == name)
return i;
}
return -1;
}
int findCourse(const vector<Course*> courses, string name)
{
for (int i = 0; i < courses.size(); i++)
{
if(courses[i]->name == name)
return i;
}
return -1;
}
int main()
{
vector<Course *> courses;
vector<Student*> students;
int choice = 0;
int cindex, sindex;
string sName, cName;
Student *s;
Course *c;
while(choice != 6)
{
cout << "1. Add Student" << endl;
cout << "2. Add Course" << endl;
cout << "3. Enroll student" << endl;
cout << "4. Print Students" << endl;
cout << "5. Print Courses" << endl;
cout << "6. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
cin.ignore(); //get rid of newline
switch(choice)
{
case 1:
cout << "Enter student name: ";
getline(cin, sName);
s = new Student;
s->name = sName;
students.push_back(s);
cout << "Student '" << sName << "' added" << endl;
break;
case 2:
cout << "Enter course name: ";
getline(cin, cName);
c = new Course;
c->name = cName;
courses.push_back(c);
cout << "Course '" << cName << "' added" << endl;
break;
case 3:
cout << "Enter student name: ";
getline(cin, sName);
cout << "Enter course name: ";
getline(cin, cName);
sindex = findStudent(students, sName);
if(sindex == -1)
cout << "Student '" << sName << "' does not exist" << endl;
else
{
cindex = findCourse(courses, cName);
if(cindex == -1)
cout << "Course '" << cName << "' does not exist" << endl;
else
{
enroll(students[sindex], courses[cindex]);
cout << "Enrolled '" << sName << "' into course '" << cName << "'" << endl;
}
}
break;
case 4:
cout << "List of students " << endl;
for(int i = 0; i < students.size(); i++)
print_student(students[i]);
break;
case 5:
cout << "List of courses " << endl;
for(int i = 0; i < courses.size(); i++)
print_course(courses[i]);
break;
case 6:
break;
default:
cout << "Invalid choice!" << endl;
}
}
}
output
=====
1. Add Student
2. Add Course
3. Enroll student
4. Print Students
5. Print Courses
6. Exit
Enter your choice: 1
Enter student name: John Smith
Student 'John Smith' added
1. Add Student
2. Add Course
3. Enroll student
4. Print Students
5. Print Courses
6. Exit
Enter your choice: 1
Enter student name: David P
Student 'David P' added
1. Add Student
2. Add Course
3. Enroll student
4. Print Students
5. Print Courses
6. Exit
Enter your choice: 2
Enter course name: CIS001
Course 'CIS001' added
1. Add Student
2. Add Course
3. Enroll student
4. Print Students
5. Print Courses
6. Exit
Enter your choice: 2
Enter course name: CIS002
Course 'CIS002' added
1. Add Student
2. Add Course
3. Enroll student
4. Print Students
5. Print Courses
6. Exit
Enter your choice: 3
Enter student name: CIS003
Enter course name: 4
Student 'CIS003' does not exist
1. Add Student
2. Add Course
3. Enroll student
4. Print Students
5. Print Courses
6. Exit
Enter your choice: 2
Enter course name: CIS003
Course 'CIS003' added
1. Add Student
2. Add Course
3. Enroll student
4. Print Students
5. Print Courses
6. Exit
Enter your choice: 4
List of students
Student Name: John Smith
Courses enrolled:
Student Name: David P
Courses enrolled:
1. Add Student
2. Add Course
3. Enroll student
4. Print Students
5. Print Courses
6. Exit
Enter your choice: 5
List of courses
Course Name: CIS001
Students enrolled:
Course Name: CIS002
Students enrolled:
Course Name: CIS003
Students enrolled:
1. Add Student
2. Add Course
3. Enroll student
4. Print Students
5. Print Courses
6. Exit
Enter your choice: 3
Enter student name: John Smith
Enter course name: CIS001
Enrolled 'John Smith' into course 'CIS001'
1. Add Student
2. Add Course
3. Enroll student
4. Print Students
5. Print Courses
6. Exit
Enter your choice: 3
Enter student name: John Smith
Enter course name: CIS003
Enrolled 'John Smith' into course 'CIS003'
1. Add Student
2. Add Course
3. Enroll student
4. Print Students
5. Print Courses
6. Exit
Enter your choice: 3
Enter student name: David P
Enter course name: CIS002
Enrolled 'David P' into course 'CIS002'
1. Add Student
2. Add Course
3. Enroll student
4. Print Students
5. Print Courses
6. Exit
Enter your choice: 4
List of students
Student Name: John Smith
Courses enrolled: CIS001 CIS003
Student Name: David P
Courses enrolled: CIS002
1. Add Student
2. Add Course
3. Enroll student
4. Print Students
5. Print Courses
6. Exit
Enter your choice: 5
List of courses
Course Name: CIS001
Students enrolled: John Smith
Course Name: CIS002
Students enrolled: David P
Course Name: CIS003
Students enrolled: John Smith
1. Add Student
2. Add Course
3. Enroll student
4. Print Students
5. Print Courses
6. Exit
Enter your choice: 6
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.