The swap function in my sortArray function doesn't seem to work properly. Error is:
No viable overloaded operator[] for type 'string *' (aka 'basic_string<char, char_traits<char>, allocator<char> > *').
I managed to find the item that goes first in the output, but now I just need a bit of help getting the rest of the user input to display as intended. To recap, the program needs to read in that many single-word names (one for each Pokemon) from the user and store the names in an array. After the user has finished entering their names, the program should display the list of all the pokemons the user entered, but in alphabetical order. The farthest I've got for the output was:
Welcome! How many Pokemon do you own?
4
Ok, enter the names!
Pikachu
Snorlax
Ekans
Squirtle
Output:
Thanks, here are the pokemon you entered: Ekans Ekans Ekans Ekans Program ended with exit code: 0
Here is my code:
#include <iostream>
#include "playground.h"
using namespace std;
string findFirst(string *x, int start, int end)
{
string first = x[0];
for(int i=start; i <= end; i++)
{
if(x[i] < first)
{
first = x[i];
}
}
return first;
}
void sortArray(string *items, int start, int end)
{
for(int i=start; i<=end; i++)
{
string min = findFirst(items,i, end);
swap(items[i], items[min]);
}
}
int main()
{
cout<<"Welcome! How many Pokemon do you own?"<<endl;
int num = 0;
cin >> num;
cout<< "Ok, enter the names!"<<endl;
string *names = new string[num];
for(int i=0; i<num; i++)
{
cin>>names[i];
}
cout<<"Thanks, here are the pokemon you entered: ";
for(int i=0; i<num; i++)
{
cout << sortArray(names, 0, num) << " ";
}
return 0;
}
stringArray[stringReturnedFromFunction]doesn't make sense, butstringArray[indexOfStringYouFoundInTheFunction]does.std::vectorof strings andstd::sort.