-1

I'm sorting an array of names and my IDE is giving me "no matching function for call to 'strcpy'. Here are the values I set up:

char Names [MaxNames] [MaxCharsPerName + 1];
const int MaxNames          (20);
const int MaxCharsPerName   (15);

Here is my function

void SortNames (const char Names[][MaxCharsPerName + 1], int NumNames)
{
int     i;
int     NumElements;
bool    Sorted;
char    Temp; // size 15?

NumElements = NumNames;
do  {
    Sorted = true;
    NumElements--;
    for (i = 0; i < NumNames; i++)
        {
         
            if(Names[i-1] > Names[i]){
                strcpy(Temp, Names[i]);
                strcpy(Names[i], Names[i+1]);
                strcpy(Names[i+1], Temp);

            }
        }
    
} while (!Sorted);

Do I have to use a reference or something?

Oh and these are at the top:

include

 using namespace std;
 #include "Constants.h"
 #include "Functions.h"
 #include <string.h>
 #include <stdio.h>
Community
  • 1
  • 1
Caleb Lawrence
  • 131
  • 1
  • 4
  • 11
  • If you are using C++, rather move over to using `std::string` for strings instead of `char []`, `char []` is the C way of doing things. Then refer to the answer of @andreas-dm below. – CJCombrink Apr 06 '16 at 05:50

2 Answers2

4
char Temp; // size 15?

Temp is a char and strcpy expects a char *(and compiler will give a compilation error in your case).

Use a char array instead-

char Temp[MaxCharsPerName + 1]; // any desired size but reserve place for '\0' 

And then pass it to strcpy.

ameyCU
  • 16,489
  • 2
  • 26
  • 41
  • Oh yeah. Well, I set it up like that and I'm getting the same error. – Caleb Lawrence Apr 06 '16 at 04:08
  • @CalebLawrence You get same error after making changes ? – ameyCU Apr 06 '16 at 04:16
  • I do, yeah. It looks like the first line strcpy(Temp, Names[i]); works but the others don't. Maybe it's a problem with the arrays. – Caleb Lawrence Apr 06 '16 at 04:19
  • @CalebLawrence You mean in other part of code now shown here or in these calls . – ameyCU Apr 06 '16 at 04:19
  • In these calls. I call it three times in the above code and I'm only getting errors on the bottom two. – Caleb Lawrence Apr 06 '16 at 04:23
  • @CalebLawrence Problem must be because of `Name` is `const char [][]` and _as you copy to it compiler is giving problem_ . Try removing `const` in function parameter list . – ameyCU Apr 06 '16 at 04:27
  • Oh yeah that let it pass. Thank you!! – Caleb Lawrence Apr 06 '16 at 04:31
  • @CalebLawrencen Welcome :) – ameyCU Apr 06 '16 at 04:32
  • @CalebLawrence I think the bug you are now seeing is because you are copying from beyond the end of your "names" list. `Names[i+1]` is going to go beyond the end of your list when `i = NumNames - 1` (i.e. the last iteration of your loop). if you change your for loop to `for (i = 0; i < NumNames - 1; i++)`, it should then get rid of your error – Grant Peters Apr 06 '16 at 04:32
  • 1
    also the `Names[i-1]` is a worry. And you should be using `strcmp` to compare your strings (right now you are just comparing the pointers to the strings) – Grant Peters Apr 06 '16 at 04:34
  • @GrantPeters Ohh nice , I just missed that :) – ameyCU Apr 06 '16 at 04:35
3

Sorting an array of names could be done much easier:

vector<string> names;
// ... 
sort(begin(names), end(names), less<string>());
for (const auto& name : names) cout << name << '\n';
Andreas DM
  • 10,685
  • 6
  • 35
  • 62