Below is a simple C++ program that demonstrates how to:
- Create a dynamic array of structs, specifically a
Student struct.
- Get attributes from the user to fill the array.
- Print the array using a function.
- Search for a student by a key (in this case, the student ID) in the array's elements using a function.
#include <iostream>
#include <string>
// Define the structure for Student
struct Student {
int id;
std::string name;
float gpa;
};
// Function to create the dynamic array of Student structs
Student* createStudentArray(int size) {
return new Student[size];
}
// Function to get student information from the user
void inputStudentData(Student* students, int size) {
for (int i = 0; i < size; i++) {
std::cout << "Enter information for student " << i + 1 << ":" << std::endl;
std::cout << "ID: ";
std::cin >> students[i].id;
std::cin.ignore(); // To consume the newline left by std::cin so getline works properly
std::cout << "Name: ";
getline(std::cin, students[i].name);
std::cout << "GPA: ";
std::cin >> students[i].gpa;
}
}
// Function to print the array of Students
void printStudents(const Student* students, int size) {
std::cout << "\nList of students:" << std::endl;
for (int i = 0; i < size; i++) {
std::cout << "ID: " << students[i].id
<< ", Name: " << students[i].name
<< ", GPA: " << students[i].gpa << std::endl;
}
}
// Function to search for a Student by ID
Student* searchStudentById(const Student* students, int size, int id) {
for (int i = 0; i < size; i++) {
if (students[i].id == id) {
return const_cast<Student*>(&students[i]); // const_cast is used to remove const-ness
}
}
return nullptr; // Return nullptr if student not found
}
int main() {
int size;
std::cout << "How many students do you want to enter? ";
std::cin >> size;
// Create a dynamic array of Student structs
Student* students = createStudentArray(size);
// Get student data from the user
inputStudentData(students, size);
// Print the student data
printStudents(students, size);
// Searching for a student by ID
int searchId;
std::cout << "\nEnter an ID to search for a student: ";
std::cin >> searchId;
Student* foundStudent = searchStudentById(students, size, searchId);
if (foundStudent != nullptr) {
std::cout << "Student found: "
<< "ID: " << foundStudent->id
<< ", Name: " << foundStudent->name
<< ", GPA: " << foundStudent->gpa << std::endl;
} else {
std::cout << "No student with ID " << searchId << " found." << std::endl;
}
// Cleanup the dynamic array
delete[] students;
return 0;
}
This program defines a Student structure with attributes id, name, and gpa. We've created various functions for different operations:
createStudentArray is used to allocate the dynamic array of Student structs.
inputStudentData takes user input for filling out the array.
printStudents prints the list of entered students.
searchStudentById searches for a student by their ID.
In main, the user is asked how many students they would like to enter, and based on this, a dynamic array of Student structures is created. It then populates the array with data provided by the user, prints the data, and finally allows the user to search the array for a student based on an ID. Before the program ends, it cleans up the allocated memory to prevent memory leaks.
Compile and run this program to test its functionality. Note, for a production-level application, additional error checking and input validation would be necessary.