"expression must be a modifiable lvalue" in function

So the purpose of this is code is to sort 10 integers numbers entered by user which is stored in the array and then passed to the function to sort it and then return it back. The issue I am having is at the function in the while loop and after where it is giving me a error that "expression must be a modifiable lvalue" at datasort[moveItem]=datasort[moveItem-1]; and datasort[moveItem]=insert;.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <iomanip>
using namespace std;

void insertionSort(const int [], int);

int main()
{
   const int arraySize=10; // size of array a
   int data[arraySize]={};
   
   //loop for user entries of 10 numbers in array
   for (int index=0; index<arraySize; index++)
   {
		cout<<"Unsorted array:\n";
		cin>>data[index];
   }

   // output original array
   for (int i = 0; i < arraySize; ++i )
   {
		cout << setw( 4 ) << data[ i ];
   }
   cout<<"\nSorted array:\n";
   insertionSort(data, arraySize);
   // output sorted array
   for (int i=0; i<arraySize; ++i)
   {   
		cout<<setw(4)<<data[i];
   }
   cout<<endl;
} // end main


void insertionSort(const int datasort[], int arraySize)
{
   int insert; // temporary variable to hold element to insert
   // insertion sort
   // loop over the elements of the array
   for (int next=1; next<arraySize; ++next)
   {
      insert=datasort[next]; // store the value in the current element
      int moveItem=next; // initialize location to place element
	  // search for the location in which to put the current element     
      while ((moveItem>0) && (datasort[moveItem-1]>insert))
      {
         // shift element one slot to the right
         datasort[moveItem]=datasort[moveItem-1];
         --moveItem;
      } // end while
   
      datasort[moveItem]=insert; // place inserted element into the array
   } // end for
	return;
}//end of function 
Last edited on
void insertionSort(const int datasort[], int arraySize)

datasort is defined to be an array of constant integer values, so the function can read the elements of the array, but it can't change them.

Ok I fixed it by changing datasort to just a int and not a constant so it works now.
Topic archived. No new replies allowed.