in C You will need to create two vectors of strings one for

in C++

You will need to create two vectors of strings, one for first names, and one for last names. You will then read in first and last names and place these into vectors.

See example below:

You will not know how many names will be entered, and will need to use vector functions to add strings into your vector.

After reading both the first name and last name, print the first name and last name separated by a space with a newline.

You will then sort the list of names using the selection sort we described in class. You will need to make some changes in order to sort on last name first, then first name.

Finally, you will print out the names sorted. Print a title above the sorted names:

I have most of the code written out but i keep getting:

so what am i doing wrong?...

#include <iostream>
#include <cstdlib>
#include <vector>
#include <string>
using namespace std;

int main(){
   string firstName;
   string lastName;
   vector<string>firstNameList;
   vector<string>lastNameList;
   string minName;
int minPosition= 0;
   unsigned int j= 0;
   unsigned int i= 0;
   string tempName;

while(true){
    cout << \"Enter First name: \";
   getline(cin,firstName);
if(firstName.empty()){
   break;
   }
  
   firstNameList.push_back(firstName);
   cout << \"Enter Last name: \";
   getline(cin,lastName);
   lastNameList.push_back(lastName);
   cout << firstName << \" \" << lastName << endl;
   }
   minPosition = 0;
   minName = lastNameList.at(0);
   for(j=0; j < lastNameList.size(); ++j){
   if(minName < lastNameList.at(j)){
       minName = lastNameList.at(j);
       minPosition = j;
   }
   }

   //cout << \" Smallest Name \" << endl;
   cout << firstNameList.at(minPosition) << \" \";
   cout << lastNameList.at(minPosition) << endl;
  
tempName= lastNameList.at(0);
lastNameList.at(0) = lastNameList.at(minPosition);
lastNameList.at(minPosition) = tempName;

   cout << endl << \" --Sorted Names-- \" << endl;
  
   for(i=0; i<=firstNameList.size();++i){
       cout << firstNameList.at(i) << \" \" <<lastNameList.at(i) << endl;
   }
   return 0;
}

Solution

Sorting logic was also not correct, I have modified it to use selection sort.

#include <iostream>
#include <cstdlib>
#include <vector>
#include <string>
using namespace std;
int main(){
string firstName;
string lastName;
vector<string>firstNameList;
vector<string>lastNameList;

while(true){
cout << \"Enter First name: \";
getline(cin,firstName);
if(firstName.empty()){
break;
}
  
firstNameList.push_back(firstName);
cout << \"Enter Last name: \";
getline(cin,lastName);
lastNameList.push_back(lastName);
cout << firstName << \" \" << lastName << endl;
}
  
for(int i = 0; i < lastNameList.size(); i++)
{
unsigned int minPosition = i;
  
for(int j = i + 1; j < lastNameList.size(); j++)
{
if(lastNameList.at(j) < lastNameList.at(minPosition))
{
cout<<\"Min Position: \"<<j<<endl;
minPosition = j;
}
}
if(minPosition != i)
{
cout<<\"Swap values from \"<<i<<\"th position to \"<<minPosition<<\"th position\"<<endl;
string temp = lastNameList[i];
lastNameList[i] = lastNameList[minPosition];
lastNameList[minPosition] = temp;
  
temp = firstNameList[i];
firstNameList[i] = firstNameList[minPosition];
firstNameList[minPosition] = temp;
}
  
}
  
cout << endl << \" --Sorted Names-- \" << endl;
  
for(int i=0; i< lastNameList.size();++i){
cout << firstNameList.at(i) << \" \" <<lastNameList.at(i) << endl;
}
return 0;
}

in C++ You will need to create two vectors of strings, one for first names, and one for last names. You will then read in first and last names and place these i
in C++ You will need to create two vectors of strings, one for first names, and one for last names. You will then read in first and last names and place these i
in C++ You will need to create two vectors of strings, one for first names, and one for last names. You will then read in first and last names and place these i

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site