In: Computer Science
C++
Write a program that reads in a list of 10 names as input from a user and places them in an array.
The program will prompt for a name and return the number of times that name was
entered in the list. The program should output total number of instances of that name and then prompt for another name until the word done is typed in.
For this lab, use the string data type as opposed to char to store the names (i.e. don’t use c-strings).
Expected Input/ Output:
Enter name #1: Joe
Enter name #2: Sally
Enter name #3: Joe
Enter name #4: Sue
Enter name #5: Sally
Enter name #6: Adam
Enter name #7: Joe
Enter name #8: Adam
Enter name #9: Adam
Enter name #10: Joe
Who do you want to search for (enter done to exit)? Joe
There are 4 instances of the name Joe.
Who do you want to search for (enter done to exit)?Sally
There are 2 instances of the name Sally.
Who do you want to search for (enter done to exit)? Adam
There are 3 instances of the name Adam.
Who do you want to search for (enter done to exit)? Sue
There is one instance of the name Sue.
Who do you want to search for (enter done to exit)? John
John’s name does not exist in this list.
Who do you want to search for(enter done to exit)?done
Thank you for using my program.
#include <iostream> #include <string> using namespace std; int main() { string names[10], name; for (int i = 0; i < 10; ++i) { cout << "Enter name #" << i + 1 << ": "; getline(cin, names[i]); } while (true) { cout << "Who do you want to search for (enter done to exit)? "; getline(cin, name); if (name == "done") break; int count = 0; for (int i = 0; i < 10; ++i) { if (names[i] == name) { count++; } } if (count == 0) { cout << name << "'s name does not exist in this list." << endl; } else if (count == 1) { cout << "There is one instance of the name " << name << "." << endl; } else { cout << "There are " << count << " instances of the name " << name << "." << endl; } } return 0; }