Implement a Simple Student Roster

C++Beginner
Practice Now

Introduction

In this challenge, you will create a C++ program that manages a student roster using the std::map container from the C++ Standard Template Library (STL). The goal is to practice using key-value data structures to efficiently track and manage student information as a school administrator.

The program should allow you to add new students with their names and ages, retrieve a student's age by their name, and display all the student records in the roster.

Implement a Simple Student Roster

As a school administrator, you need to create a digital system to efficiently track and manage student information using C++ STL map container. This challenge will help you practice using key-value data structures.

Tasks

  • Create a C++ program that manages a student roster using std::map
  • Implement functions to add students with their names and ages
  • Allow retrieving a student's age by their name
  • Display all student records in the roster

Requirements

  • Create the program in ~/project/student_roster.cpp
  • Use std::map<std::string, int> to store student names and ages
  • Implement the following operations:
    • Add a new student to the roster
    • Retrieve a student's age by name
    • Display all students in the roster
  • Use only standard C++ STL map operations learned in the previous lab
  • Compile the program using g++ compiler

Examples

Compile and run the program to manage the student roster:

g++ student_roster.cpp -o student_roster
./student_roster

Example input and output:

Enter command (add/get/list/quit): add
Enter student name: Alice
Enter student age: 20

Enter command (add/get/list/quit): add
Enter student name: Bob
Enter student age: 22

Enter command (get/list): get
Enter student name: Alice
Alice is 20 years old

Enter command (list): list
Alice: 20
Bob: 22

Hints

  • Use map.insert() or map[] to add students
  • Use map.at() or map[] to retrieve a student's age
  • Iterate through the map using a range-based for loop
  • Handle potential exceptions when retrieving ages
✨ Check Solution and Practice

Summary

In summary, this challenge requires you to implement a C++ program that manages a student roster using the std::map container. The program should allow you to add new students, retrieve a student's age by their name, and display all the student records in the roster. This challenge aims to help you practice using key-value data structures in C++ to efficiently track and manage student information.