2

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;
}
6
  • 1
    Perhaps read cplusplus.com/reference/algorithm/sort Commented Mar 3, 2018 at 2:39
  • 1
    ... and en.cppreference.com/w/cpp/algorithm/for_each Commented Mar 3, 2018 at 2:39
  • Your array is an array of strings. The indices of that array are integers. stringArray[stringReturnedFromFunction] doesn't make sense, but stringArray[indexOfStringYouFoundInTheFunction] does. Commented Mar 3, 2018 at 2:41
  • those all deal with sorting numbers.. that I can do, strings is a bit different.. well to me at least Commented Mar 3, 2018 at 2:42
  • 2
    There are numerous other issues with your program, like how do you expect to output the result of a void function, and you're off by one in at least two places. Unless you must write your own sort algorithm you'd be much better off with a std::vector of strings and std::sort. Commented Mar 3, 2018 at 2:46

3 Answers 3

2

C++ is all about not re-inventing wheels. It was specifically designed for writing libraries and generic code like the STL (Standard Template Library).

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;  // Kids, do not try this at home

int main() {
    cout << "Welcome! How many Pokemon do you own?" << endl;
    int num = 0;
    cin >> num;
    cout << "Ok, enter the names!" << endl;
    std::vector<std::string> names(num);
    for (auto &name: names) {
        cin >> name;
    }

    std::sort(names.begin(), names.end());

    cout << "Thanks, here are the pokemon you entered: ";
    for (auto name: names) {
        cout << name << " ";
    }
    cout << endl;
    return 0;
}
Sign up to request clarification or add additional context in comments.

1 Comment

nice, I'm definitely going to practice algorithms with vectors!
0

Alright, I'm going to give you a plain and simple answer that'll be very easy to understand.

  1. Start using STL.It'll have most of the pre build functions that might come handy all the time.
  2. Instead of using #include<iostream> use #include<bits/stdc++.h>.

Now coming to your question, I'll tweak your code and make it simpler and will become easy to understand.

#include <bits/stdc++.h>//read the link that I provided above
using namespace std;

void sortArray(string names[],int num)
{
  sort(names,names+num);//This is the function of the  STL  for which I 
  //included <bits/stdc++.h>.Read from link provided above.
  for(int i=0;i<num;i++)
  {
    cout<<names[i]<<endl;
  }
}
   int main()
{
  cout<<"Welcome! How many Pokemon do you own?"<<endl;
  int num = 0;
  cin >> num;
  cout<< "Ok, enter the names!"<<endl;
  string names[num];
  for(int i=0;i<num;i++)
  {
    cin>>names[i];
  }
  cout<<"Thanks, here are the pokemon you entered:"<<endl;  
  sortArray(names,num);
}

Let me know if you don't understand anything.

5 Comments

I haven't gotten to the topic vectors just yet, thanks anyway rajat! (:
What and where is <bits/stdc++.h>
@Raul747 bro I have not used the vector here, I just used the sort() to sort the array alphabetically.You can also use sort() to sort anything, be it integers, float or any data type. Just use sort(arrayname,arrayname+sizeof(array)).
@JiveDadson <bits/stdc++.h> basically includes all the libraries so that you don't have to include any other extra #include<>. It just makes our life simpler.
@rajat sorry, that comment was meant for the other post, I appreciate your comments by the way, thank you!
-1

//Got IT!!!!

#include <iostream>
using namespace std;

int findFirst(string *x, int start, int end)
{
    int first = start;
    for(int i=start; i <= end; i++)
    {
        if(x[i] < x[first])
        {
            first = i;
        }
    }
    return first;
}
/*
void swap(string &s1, string &s2) {
   string temp = s1;
   s1 = s2;
   s2 = temp;
}
*/
void sortArray(string *items, int start, int end)
{
    for(int i=start; i<=end; i++)
    {
        int 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];
    }
    sortArray(names, 0, num-1);
    cout<<"Thanks, here are the pokemon you entered: ";
    for(int i=0; i<num; i++)
    {
        cout << names[i] << " ";
    }
    cout << endl;


    return 0;
}

4 Comments

There's a memory leak. Quit using new and new[]. Use std::vector<std::string>>
This is most probably a school work, so we can't expect OP not to use new and use a vector. If there is memory leak, then we should remind him to release the allocated memory instead of using vector.
user3437460 is correct, I'm 16 doing practice problems I've found online.
and you are correct, I'll be sure to look out for memory leaks next time, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.