0

I am trying to use a function to sort through a char array full of words. The current issue I am having is that in my sortNames function I am getting the error, "expression must be a modifiable lvalue" at the part below

hold = nameArr[ii];
nameArr[ii] = nameArr[jj];
nameArr[jj] = hold;

I am guessing that its because I am trying to pass values through an array for some reason. I am struggling with understanding references and pointers and the such, and I imagine that is hurting me here as well. Any help with this would be fantastic, thank you in advance.

Here is my current code...

#include <iostream>
#include <string>

using namespace std;

char nameArr[20][15];           // array to store the 20 values
int val = 0;                    // variable to pass values to the array
int x = 0;                      // loop counter outside functions

//Function prototypes
void getNames(char (&nameArr)[20][15], int &val);
void sortNames( char(&nameArr)[20][15]);

//getNames Function
void getNames(char (&nameArr)[20][15], int &val)
{
    int i = 0;                  // loop counter

    cout << "Awesome, now lets input those names...\n" << endl; 

    for (i = 0; i < val; i++)
    {
        cout << "\nNAME " << i+1 << ": " << ' ';
        cin >> nameArr[i];
    }

    cout << "\n\n\nThese are the names that you inserted:\n" << endl;

    for (i = 0; i < val; i++)
    {
         cout << nameArr[i] << "\n" << endl;
    }
}

// sortNames function
void sortNames( char(&nameArr)[20][15])
{
    int n = 15;             // max length of word
    int ii = 0;             // loop counter
    int jj = 0;             // other counter
    string hold;            // holding array

    for (int ii = 0 ; ii < n ; ii++) 
    {   
         for (int jj = ii + 1; jj < n; jj++) 
        {
             if (nameArr[ii] > nameArr[jj])
            {
                hold = nameArr[ii];
                nameArr[ii] = nameArr[jj];
                nameArr[jj] = hold;
            }
        }
    }
}


int main()
{
    cout << "NAME SORTER!\n\nPlease enter in the amount of names you wish to enter: " << ' ';
    cin >> val;

    getNames(nameArr, val);

    cout << "\n\n\nAlright, lets sort now..." << endl;

    sortNames(nameArr);

    cout << "\nHere are the results:\n" << endl;

    for (x = 0; x < val; x++)
    {
         cout << nameArr[x] << "\n" << endl;
    }

    system("pause");
 }
Dpry12
  • 105
  • 8

2 Answers2

3

Your main problem here is that you are trying to use an assignment operator on two fixed sized arrays, which isn't legal. Consider the following code:

int a[2] = {0, 0};
int b[2] = {1, 1};

a = b;

This gives the same error you are getting. On the lines you mentioned, you are doing the same thing with char[15] arrays.

To fix your problems, you either need to allocate your char array dynamically/work with the pointers, or a simpler solution would be to just change your char[][] array to a string[] array.

That being said, there are a lot of things you can clean up here:

  • You have a few variables declared globally that can just be defined in main or lower
  • You can declare loop counters inside the for loop instead of beforehand, as you do in the sortNames function
  • In sortNames you are declaring a few variables twice
dwcanillas
  • 3,562
  • 2
  • 24
  • 33
  • I'm trying to understand this, but having a little bit of trouble. the arrays are both fixed sized and the same size, I see that. But what I don't understand is, if you are assigning a = b, if they are the same fixed size, why doesn't it work? Excuse my ignorance, still learning. – Dpry12 Apr 22 '15 at 20:51
  • @Dpry12 http://stackoverflow.com/questions/3437110/why-does-c-support-memberwise-assignment-of-arrays-within-structs-but-not-gen – dwcanillas Apr 22 '15 at 20:57
0

I'll add a few things to dwcanilla's answer.

You will want to change your function prototypes and headers to something more like this:

void getNames(char ** & arr, int val);
void sortNames(char ** & arr);

What this means is that the function accepts a reference to an array of c-strings; that is, when you work with the array within the function you are modifying the actual array you passed and not just a copy. Also I think you'd be fine just passing the integer by value for getNames.

Second, global variables are generally a bad idea. Since you can pass the array reference directly to your functions you may want to declare nameArr and your other global variables inside main instead.

Third, in getNames you won't be able to use cin to assign your c-strings directly.

EDIT: This is a better way -- getting console input for Cstrings

Finally, the < operator doesn't work on c-strings the way you're using it in your sort function. Use strcmp() instead (and be sure to include the cstring header):

if(strcmp(arr[ii], arr[jj]) > 0)
Community
  • 1
  • 1
i-am-wells
  • 61
  • 4
  • getting an error at the arr[i] = temp.c_str(); part, saying "Const char*" cannot be assigned to an entity of type "char *"...another error I don't understand lol – Dpry12 Apr 22 '15 at 21:03
  • Hmm, sorry, my advice was wrong. Try this instead for getting strings from the user: http://stackoverflow.com/questions/9755100/getting-console-input-for-cstrings – i-am-wells Apr 22 '15 at 21:11