Skip to content
C++ project #17Intermediate

Contact Book with Indexed Search

Store contacts in a sorted map, search by name prefix with lower_bound, by city through a multimap index and by phone digits — with validation and consistent edits.

Estimated time
~4 hours to build
Files
5 files · 384 lines
Technologies
C++17, <map>, <vector>, <algorithm>, <cctype>

Introduction

Searching is where data-structure choice matters. This contact book keeps contacts in a std::map, which makes prefix search a fast lower_bound plus a short walk, and maintains a secondary std::multimap index by city. The tricky part — keeping the index correct when a contact moves city or is deleted — is exactly what real databases do.

Problem statement

Write a contact manager. Contacts have a name, a 10-digit phone number (formats like +91-99000-11223 are normalised), an e-mail address and a city. Users can add (rejecting duplicates by case-insensitive name and invalid data), edit phone, e-mail or city, delete, search by name prefix, list by city, find by part of a phone number, list everything alphabetically and see counts per city.

Objectives

  • Use std::map::lower_bound for prefix search
  • Maintain a secondary index with std::multimap and equal_range
  • Normalise and validate user input
  • Keep derived data consistent during updates

Features

  • Case-insensitive unique names
  • Phone normalisation and e-mail validation
  • Prefix, city and phone-digit searches
  • Index kept consistent on edit and delete

Concepts demonstrated

  • Ordered maps
  • Secondary indexes
  • lower_bound / equal_range
  • Input normalisation
  • Data consistency

System architecture

ContactBook stores contacts_ keyed by the lower-cased name (so iteration is alphabetical) and by_city_, a multimap from lower-cased city to name key. starting_with jumps to lower_bound(prefix) and walks forward while keys still start with the prefix. in_city uses equal_range. Every operation that changes a city or deletes a contact first calls unindex to remove the old index entry. main splits command arguments on | so names and cities may contain spaces.

Folder structure

TextProject tree
contact-book/
├── README.md
├── Makefile
├── src/main.cpp
├── tests/expected-output.txt
└── tests/sample-input.txt

Complete source code

Makefile

20 lines
MakefileMakefile
# Generated for KENHOLT Skills. Usage: make | make test | make clean
CXX = g++
CXXFLAGS = -std=c++17 -Wall -Wextra -O2
SRC := $(wildcard src/*.cpp)
HDR := $(wildcard src/*.h src/*.hpp)
ARGS := $(shell cat tests/args.txt 2>/dev/null)
INPUT := $(wildcard tests/sample-input.txt)
app: $(SRC) $(HDR)
$(CXX) $(CXXFLAGS) -Isrc $(SRC) -o app
test: app
./app $(ARGS) $(if $(INPUT),< $(INPUT),) > actual.txt
diff actual.txt tests/expected-output.txt && echo "Test passed: output matches tests/expected-output.txt"
clean:
rm -f app app.exe actual.txt
.PHONY: test clean

What each file does

src/main.cpp
Validation helpers, the ContactBook class with its two indexes, result printing and the command loop.
Makefile
Build automation: make builds ./app, make test runs the sample session and compares it with the expected output, make clean removes build files.

How to run it

Build

Terminal
g++ -std=c++17 -Wall -Wextra -Isrc src/*.cpp -o app
# or simply: make

Run

Terminal
./app # Linux / macOS
.\app.exe # Windows

Test against the expected output

Terminal
./app < tests/sample-input.txt > actual.txt
diff actual.txt tests/expected-output.txt # no output means they match
# Windows PowerShell:
# Get-Content tests\sample-input.txt | .\app.exe > actual.txt
# Compare-Object (Get-Content actual.txt) (Get-Content tests\expected-output.txt)

Expected output

Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.

Texttests/expected-output.txt
=== Contact Book ===
add <name> | <phone> | <email> | <city> edit <name> | <field> | <value>
delete <name> find <prefix> city <city> phone <digits> list stats quit
> Saved Asha Rao
> Saved Ravi Kumar
> Saved Arjun Mehta
> Invalid phone number '12345' (need 10 digits)
> Invalid e-mail address 'meera@example'
> Saved Meera Iyer
> asha rao already exists (use edit)
> Invalid phone number '(080) 4123 4567' (need 10 digits)
> Arjun Mehta 91234 56789 arjun@example.net Bengaluru
Asha Rao 98450 12345 asha@example.com Bengaluru
Meera Iyer 99887 76655 meera@example.com Mumbai
Ravi Kumar 99000 11223 ravi.k@example.org Chennai
> Arjun Mehta 91234 56789 arjun@example.net Bengaluru
Asha Rao 98450 12345 asha@example.com Bengaluru
> Arjun Mehta 91234 56789 arjun@example.net Bengaluru
> Arjun Mehta 91234 56789 arjun@example.net Bengaluru
Asha Rao 98450 12345 asha@example.com Bengaluru
> Meera Iyer 99887 76655 meera@example.com Mumbai
> Updated Arjun Mehta's city
> Invalid e-mail address
> Updated Ravi Kumar's phone
> Asha Rao 98450 12345 asha@example.com Bengaluru
> Arjun Mehta 91234 56789 arjun@example.net Pune
> No contact called Anil Shah
> No contact called Nobody
> Bengaluru: 1
Chennai: 1
Mumbai: 1
Pune: 1
> Arjun Mehta 91234 56789 arjun@example.net Pune
Asha Rao 98450 12345 asha@example.com Bengaluru
Meera Iyer 99887 76655 meera@example.com Mumbai
Ravi Kumar 98765 43210 ravi.k@example.org Chennai
>
Input
add Asha Rao | 98450 12345 | asha@example.com | Bengaluru
add Ravi Kumar | +91-99000-11223 | ravi.k@example.org | Chennai
add Arjun Mehta | 9123456789 | arjun@example.net | Bengaluru
add Meera Iyer | 12345 | meera@example.com | Mumbai
add Meera Iyer | 9988776655 | meera@example | Mumbai
add Meera Iyer | 9988776655 | meera@example.com | Mumbai
add asha rao | 9000000000 | other@example.com | Pune
add Anil Shah | (080) 4123 4567 | anil@example.co.in | Bengaluru
list
find a
find ar
city bengaluru
phone 6655
edit Arjun Mehta | city | Pune
edit Ravi Kumar | email | ravi@
edit Ravi Kumar | phone | 98765-43210
city Bengaluru
city Pune
delete Anil Shah
delete Nobody
stats
list
quit

Testing

  • Compare ./app < tests/sample-input.txt with tests/expected-output.txt.
  • asha rao is rejected as a duplicate of Asha Rao.
  • After Arjun moves to Pune, city Bengaluru lists only Asha and city Pune lists Arjun.
  • phone 6655 finds Meera from the last four digits.

Common errors

A contact still appears under the old city after editing

Remove the old (city, key) pair from the multimap before inserting the new one.

Prefix search misses capitalised names

Normalise both the stored keys and the search prefix to lower case.

Possible improvements

  • Save to and load from a CSV file
  • Add birthdays and an upcoming-birthdays command
  • Support several phone numbers per contact

Additional challenges

  1. 1Add fuzzy search that tolerates one typo.
  2. 2Add an index by e-mail domain.
  3. 3Merge two contacts that turn out to be the same person.

Built and tested it yourself? Mark it to update your dashboard.