In C++. 1. Dene (write) a structure that will store a name and ID number. The st
ID: 3824718 • Letter: I
Question
In C++.
1. Dene (write) a structure that will store a name and ID number. The structure should have two members: a character array or string to store the name and an integer to store the ID.
2. Write a function that allows the user to enter a name and ID number for a structure variable.
3. Write a function that prints the structure. You can chose how the name and ID are printed.
4. Write a program that creates an array of ten (10) of these name and ID structures, allows the user to read up to ten (10) names and IDs, and then prints the list of names and IDs. The names and IDs should be entered and printed using the two functions written in steps 2 and 3 previously.
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
// structure to hold a students detail
typedef struct student {
string name;
int ID;
} Student;
// function to read student details in struct from user
void readStudent(Student* student)
{
cout << "Enter name: ";
cin >> student->name;
cout << "Enter ID: ";
cin >> student->ID;
cout << endl;
}
// function to print student details from struct
void printStudent(Student student)
{
cout << "Name: " << student.name << " ID: " << student.ID << endl;
}
int main()
{
int n = 10;
// declaring an array of 10 student
Student students[n];
// reading student data
cout << "Enter students details" << endl;
for(int i = 0; i < n; i++)
{
readStudent(&students[i]);
}
// printing student data.
cout << "Studnet detaisl are: "<< endl;
for(int i = 0; i < n; i++)
{
printStudent(students[i]);
}
return 0;
}
Please give a thumbs up if this solved your question.