No structs we havent got far yet. Write a program which will keep track of names
ID: 3912024 • Letter: N
Question
No structs we havent got far yet. Write a program which will keep track of names and phone numbers. You may use parallel vectors of strings or a vector of instances of a class you have written. The program will use a loop. The loop will print the following menu for each cycle. Enter one of the following: A to add a new name and phone number L to list the names and phone numbers, with line numbers Q to quit The user will enter a letter from the menu. If any invalid letter is entered, the program will display an error message and repeat the menu. If the user enters “A”, the program will ask for the name (which may contain blanks). After the name is entered, the program will ask for the phone number (which may contain non-numeric characters). For example, A Enter the name Neumann, Alfred E. Enter the phone number (405)555-0001 If the user enters “L”, the program will list all of the names and phone numbers along with line numbers. For example, L 0 Dough, Jane (405)555-0003 1 Neumann, Alfred E. (405)555-0001 2 Xavier, Charles (800)987-6543 If the user enters “Q”, the program comes to a normal end.
Explanation / Answer
Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you
#include <iostream>
#include <string>
using namespace std;
int main(){
string names[50], phones[50];
int count = 0;
string choice ="";
while(choice != "q" && choice != "Q")
{
cout << "(A)dd" << endl << "(L)ist" << endl << "(Q)uit" << endl;
cout << "Enter your choice: ";
cin >> choice;
cin.ignore(256, ' ');
if(choice == "a" || choice == "A"){
cout << "Enter the details to add" << endl;
cout << "Name: ";
getline(cin, names[count]);
cout << "Phone: ";
cin >> phones[count];
count++;
}
else if(choice == "l" || choice == "L")
{
for(int i = 0; i < count; i++)
cout << i << " " << names[i] << " " << phones[i] << endl;
cout << endl;
}
else if(choice != "q" && choice != "Q")
{
cout << "Invalid choice!" << endl;
}
cout << endl;
}
cout << "Good Bye!!" << endl;
}
output
-----
(A)dd
(L)ist
(Q)uit
Enter your choice: A
Enter the details to add
Name: Dough, Jane
Phone: (405)555-0003
(A)dd
(L)ist
(Q)uit
Enter your choice: A
Enter the details to add
Name: Neumann, Alfred E.
Phone: (405)555-0001
(A)dd
(L)ist
(Q)uit
Enter your choice: L
0 Dough, Jane (405)555-0003
1 Neumann, Alfred E. (405)555-0001
(A)dd
(L)ist
(Q)uit
Enter your choice: A
Enter the details to add
Name: Xavier, Charles
Phone: (800)987-6543
(A)dd
(L)ist
(Q)uit
Enter your choice: L
0 Dough, Jane (405)555-0003
1 Neumann, Alfred E. (405)555-0001
2 Xavier, Charles (800)987-6543
(A)dd
(L)ist
(Q)uit
Enter your choice: Q
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.