Write a C++ program that declares a struct called course with the following attributes:
int ID;
string name;
Then create a struct called student that contains the following attributes:
int ID;
string name;
Vector of courses
Write a main program that:
1. Declare structure student
2. Pass it to a function by reference to fill it in with information
3. Pass it to a function to print it out
While filling the student structure, ask the user for the number of courses and fill in the vector accordingly.
#include <iostream>
#include<vector>
using namespace std;
struct Course
{
int ID;
string name;
};
struct Student
{
int numb;
int ID;
string name;
vector<Course>courses;
};
void get_info(Student& the_info);
void print_info(Student& the_info);
int main()
{
vector<Course>courses;
courses.push_back(Course());
Student std;
get_info(std);
print_info(std);
return 0;
}
void get_info(Student& the_info)
{
cout << "Number of courses: ";
cin >> the_info.numb;
cout << "Enter Student ID: ";
cin >> the_info.ID;
cout << "Enter Student Name: ";
cin >> the_info.name;
}
void print_info(Student& the_info)
{
}
To write a C++ program that fulfills the requirements mentioned, you need to follow these steps:
Step 1: Declare the necessary header files
Include the necessary header files in your program, which are <iostream> for input and output operations, <string> for string operations, and <vector> for using vectors.
```cpp
#include <iostream>
#include <string>
#include <vector>
```
Step 2: Define the structures
Define the structures "course" and "student" as per the given attributes. Additionally, define a function named "fillStudent" and a function named "printStudent" that will be used to fill and print the student structure, respectively.
```cpp
struct course {
int ID;
std::string name;
};
struct student {
int ID;
std::string name;
std::vector<course> courses;
};
void fillStudent(student& s);
void printStudent(const student& s);
```
Step 3: Implement the functions
Write the function definitions for "fillStudent" and "printStudent".
```cpp
void fillStudent(student& s) {
std::cout << "Enter student ID: ";
std::cin >> s.ID;
std::cout << "Enter student name: ";
std::cin.ignore();
std::getline(std::cin, s.name);
int numCourses;
std::cout << "Enter the number of courses: ";
std::cin >> numCourses;
for (int i = 0; i < numCourses; i++) {
course c;
std::cout << "Enter course ID for course " << i + 1 << ": ";
std::cin >> c.ID;
std::cout << "Enter course name for course " << i + 1 << ": ";
std::cin.ignore();
std::getline(std::cin, c.name);
s.courses.push_back(c);
}
}
void printStudent(const student& s) {
std::cout << "Student ID: " << s.ID << std::endl;
std::cout << "Student name: " << s.name << std::endl;
std::cout << "Courses: " << std::endl;
for (const auto& c : s.courses) {
std::cout << "Course ID: " << c.ID << ", Course name: " << c.name << std::endl;
}
}
```
Step 4: Implement the main function
Inside the main function, declare a student structure variable, call the fillStudent function to fill in the information, and then call the printStudent function to print the student details.
```cpp
int main() {
student s;
fillStudent(s);
printStudent(s);
return 0;
}
```
That's it! You have successfully written a C++ program that fulfills the given requirements.