switching names from first name last name to last name first name.

i have a program that reads in from a txt file. called names.txt. the file has a main and a .h file while is where the functions are written. how do i switch at the names from first name last name to last name first name. and vise versa. here is what i have and the main.

#include<iostream>
using namespace std;

enum NameFormat{FIRST, LAST};

#include "assign8Answer.h"//Your .h filename here.

int main(){
const int size = 25; //the intended size of our array
int choice; //menu option that the user selects
char sortOption; //ascending or descending sort?
NameFormat format = FIRST; //format set to FIRST initially, because that's how the names are read in
string names[size]; //our string array, consisting of 25 strings
read(names, size); //read from the file names.txt and store to names, second argument is the size of the array
print(names, size); //print the unsorted array that was read from names.txt
choice = menu();
//begin by asking the user which choice he/she wants, if 6 hit off the bat, kick out of loop immediately

while(choice!=6){

if (choice<6){ //5 means to search;
if (choice%2){ //1 and 3 to sort in ascending order
sortOption = 'a';
}
else{//2 and 4 mean to sort in descending order
sortOption = 'd';
}
}
cin.ignore();//necessary for getline and cin to co-exist.
switch(choice){

case 1:
case 2:
if (format != LAST){//if the format is already not set to last name first...
convertToLastFirst(names, size);//perform conversion
format = LAST;//set the format of the names to represent last name, first name
}
sort(names, size, sortOption); //then call sort
break;
case 3:
case 4:
if (format == FIRST){//if the format has already been set to first name first...
convertToLastFirst(names, size);//convert to last name first
sort(names, size, sortOption);//sort
convertToFirstLast(names, size);//convert back to first name first
}
if (format != FIRST){//otherwise
sort(names, size, sortOption);//sort
convertToFirstLast(names, size);//convert to first name first order
format = FIRST;//set the format flag to denote the names are sorted with first name first
}
break;
case 5://search
if (isPresent(names, size, format)){//call is present
cout<<"Yes."<<endl<<endl<<endl;//if the result is true, say yes...
}
else{
cout<<"No."<<endl<<endl<<endl;//otherwise, say no...
}
break;
default:
cout<<"Unknown option; please select again.";//Error-handling mechanism
break;
}

if ((choice>0)&&(choice<5)){
//for options 1-4, we now need to print out the string array
print(names, size);
}
choice = menu(); //prompt the user to make another choice from the menu

}//end of loop
return 0;
}

//this is what i have so far in the .h file
#include <iostream>
#include <string>
//progam assignment 8 kain,ciara

int menu(){
int a;
cout << "Select an option below" << endl;
cout << "1) Display the names, in ascending alphabetical order, with last name first." << endl;
cout << "2) Display the names, in descending alphabetical order, with last name first." <<endl;
cout << "3) Display the names, in ascending alphabetical order, with first name first." << endl;
cout << "4) Display the names, in descending alphabetical order, with first name first." << end;
cout << "5) Search for a name." << endl;
cin >> a;

}
void read(string n[],int size){
cout<<"Enter "<<size<<"first names, one at a time."<<endl;
for(int i=0; i<size;i++){
cin>>n[i];
}
}

void print(string n[], int size){
for(int i=0;i<10;i++){
cout<<n[i]<<endl;
}
}

void sort(string n[],int size){
string temp;
string swapholder;
for(int i=1;i<size;i++){
temp = n[i];
int j = i;
while(j>0 && n[j-1] > temp){
swapholder = n[j-1];
n[j-1] = temp;
n[j] = swapholder;
j--;
}
}
}

void convertToLastFirst(string n[], int){

}

void convertToFirstLast(string[], int){

}

void sort(string[], int, char){

}
bool isPresent(const string[], int, NameFormat){
// this function makesa way to search for a name in txt. if the there is the name in the txt file
//then you return true if not false.
}
What are the contents of the file "names.txt"?
the contents are the txt file is a list of names that are line by line and are in first name last name
Last edited on
i updated that read file this is what i got for that so far but I'm not sure if it is right.
void read(string n[],int size){
ifstream infile;
infile.open("names.txt");
for (int i; i < 10 ; i++){
while (!infile.eof()) {
infile >> string n[i];
cout << string n[i];
}
}
}
Topic archived. No new replies allowed.