Files
CSCI-1200/labs/09_maps/phonebook.cpp
Jidong Xiao dcb5c8a539 adding lab 9
2023-10-23 00:33:26 -04:00

35 lines
869 B
C++

// A simple "caller ID" program
#include <iostream>
#include <vector>
#include <string>
using namespace std;
// add a number, name pair to the phonebook
void add(vector<string> &phonebook, int number, string const& name) {
phonebook[number] = name;
}
// given a phone number, determine who is calling
void identify(const vector<string> & phonebook, int number) {
if (phonebook[number] == "UNASSIGNED")
cout << "unknown caller!" << endl;
else
cout << phonebook[number] << " is calling!" << endl;
}
int main() {
// create the phonebook; initially all numbers are unassigned
vector<string> phonebook(10000, "UNASSIGNED");
// add several names to the phonebook
add(phonebook, 1111, "fred");
add(phonebook, 2222, "sally");
add(phonebook, 3333, "george");
// test the phonebook
identify(phonebook, 2222);
identify(phonebook, 4444);
}