Question: Can someone edit the program below and add these two new criteria to it and make sure it runs? 6)Upgrade your names program from lab
Can someone edit the program below and add these two new criteria to it and make sure it runs?
6)Upgrade your names program from lab 06 so that you can enter names like this John Doe at the keyboard but are converted to this Doe, John before being added to the storage list (array or vector). HINT: using str.find(substr, pos) function to find the between the first name and last name entered; and then using str.substr(pos, num) function to extract the first name and last name; finally, concatenate the last name, ,, and the first name to form the required string.
Add another function that uses a stringstream to create and return a new string made up of only the last names in the list (pass the list to the function). Enclose each last name with double quotes. You will need to find where each last name ends in the string then extract just the last name. HINT: You will need the same functions mentioned in question 6 to find the , at the end of each last name, and extract the last name
// Names
#include
#include
#include
using namespace std;
int Menu();
void getName(vector
int removeName(vector
void displayName(vector
int findName(vector
void sortName(vector
int main()
{ vector
int count = 0;
int choice = Menu();
while (choice != 0)
{ switch (choice)
{ case 1: { getName(names, count); } break;
case 2: { string name;
cout << "Enter name to remove: ";
getline(cin, name);
int x = removeName(names, count, name);
if (x == -1) { cout << "Name is not in the list" << endl; } else { count = x; } } break;
case 3: { displayName(names, count); } break;
case 4: {
string name;
cout << "Enter name to find: ";
getline(cin, name);
int num = findName(names, count, name);
if (num == -1) { cout << "Name not found in the list" << endl << endl; }
else { cout << "Name found at number " << num << " in the list." << endl << endl; } } break;
case 5: { sortName(names, count); } break;
case 0: { return 0; } break; }
choice = Menu(); }
system("pause");
return 0; }
void getName(vector
{ string name;
cout << "Enter name: ";
getline(cin, name);
names.push_back(name);
count++; }
int Menu() {
int choice;
cout << "1. Add a name" << endl;
cout << "2. Remove a name" << endl;
cout << "3. Display names " << endl;
cout << "4. Search a name" << endl;
cout << "5. Sort names" << endl;
cout << "0. Quit" << endl;
cout << "Enter a option: ";
cin >> choice;
while (choice < 0 || choice > 5)
{ cout << "Choice not on list: ";
cin >> choice; }
cin.ignore();
return choice; }
void sortName(vector
{ for (int i = 0; i { for (int j = i + 1; j { if (names[i] > names[j]) { string temp = names[i]; names[i] = names[j]; names[j] = temp; } } } for (int i = 0; i < count; i++) { cout << names[i] << endl; } } int findName(vector { for (int i = 0; i { if (names[i] == list) return i; } return -1; } int removeName(vector { int find = findName(names, count, list); if (find == -1) return find; for (int i = find; i { names[i] = names[i + 1]; } count = count - 1; return count; } void displayName(vector { if (count == 0) { cout << "No names to display" << endl; return; } for (int i = 0; i { cout << names[i] << endl; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
