Reputation:
I am trying to do a program where a user inputs three names and they get sorted. The conditions are that each name gets inputted like "firstname lastname" and then I need to sort the names on lastname, but if the lastname is the same for two entries I need to sort on the firstname.
I have solved how to sort on just the firstname OR lastname, but am getting stuck on how to sort on both. Any ideas how I can implement more conditional sorting without using an array or <algorithm>
in c++?
For each input I do this to split the input to firstname and lastname to lowercase:
cout << "Input name1: " << endl;
getline(cin, input1);
input1_old = input1;
size_t found = input1.find(space);
for (int i = 0; i < input1.size(); i++)
{
input1[i] = tolower(input1[i]);
}
input1Last = input1.substr(found + 1, string::npos);
input1First = input1.substr(0, found);
Then I "sort" like this:
if (input1Last <= input2Last && input2Last <= input3Last)
{
cout << input1_old << '\n' << input2_old << '\n' << input3_old << endl;
}
else if (input1Last <= input3Last && input3Last <= input2Last)
{
cout << input1_old << '\n' << input3_old << '\n' << input2_old << endl;
}
else if (input2Last <= input1Last && input1Last <= input3Last)
{
cout << input2_old << '\n' << input1_old << '\n' << input3_old << endl;
}
else if (input2Last <= input3Last && input3Last <= input1Last)
{
cout << input2_old << '\n' << input3_old << '\n' << input1Last << endl;
}
else if (input3Last <= input1Last && input1Last <= input2Last)
{
cout << input3_old << '\n' << input1_old << '\n' << input2_old << endl;
}
if (input3Last <= input2Last && input2Last <= input1Last)
{
cout << input3_old << '\n' << input2_old << '\n' << input1Last << endl;
}
Upvotes: 1
Views: 716
Reputation: 144
Assuming it's a homework question and you shouldn't use macros or try to cleverly avoid restrictions:
Use string concatenation (strcat in standard C library) to join two string together. Use " " to separate them. This will preserve the lexicographical order and reduce the problem to the one already solved :)
Use the 6 if clauses you've already made for selecting proper order of the resulting (single) string.
In c++ you can just use operator+ to join strings.
If you cannot print the resulting single string (the order of printing and comparison are different) - you have to split them (strtok) and reverse the first/last name before printing.
@EDIT
Also, while we're at it, you can just use std::cin >> firstName >> lastName
to read strings (it will read until first whitespace) and lowercase them char by char in the same loop using std::string::operator[] to access chars
Upvotes: 1
Reputation: 10048
To swap first and last names, use the following trick:
To do this, you should write a function that reverses a string between two indices, and you will need to use string::find()
(or a loop) to find the space between the names.
To sort three items, use the following trick:
Again, sorting two items is perfect for a function.
Hints:
void reverse( string& s, int first, int last );
void sort( string& a, string& b ); // swap a and b if !(a < b)
Upvotes: 0