For the curious, the rest of this question is here:
http://www.cplusplus.com/forum/beginner/48643/
Firstly, the logic here doesn't make any sense.
Why do you ask the user maxnumberofelements
times to input a seat ID? Shouldn't you keep asking them until they want to quit? (I noticed now that the user is asked
up to maxnumberofelements times, though getting the user input and doing the next update operation every iteration doesn't make sense.)
Next, you save the current request in array3. You shouldn't have to do this unless you need to store the user's requests in order.
And an operation like this shouldn't be done in the main loop:
1 2 3 4 5 6 7 8 9 10 11
|
int dispreservedarray[numberofinputs];
for (int x = 0; x < maxnumberofelements; x++)
{
dispreservedarray[x] = x;
if (array3[numberofinputs] == dispreservedarray[x])
{
dispreservedarray[x] = 'x';
}
}
|
Speaking of this operation, this
int dispreservedarray[numberofinputs];
causes undefined behaviour as you can't create an array with a length not known at compile time (without
new
or
malloc
). Also, the loop goes to
maxnumberofelements
yet the array
dispreservedarray
is
numberofinputs
long. And lastly, in the first iteration this array will be zero length (!), then the next it will be of length 1, then 2, etc.
cout<<dispreservedarray[x];
Not only is dispreservedarray out of scope here, but so is x. If you want to display the whole array, you will have to do it in a loop. As I said, you will have to re-examine the logic here.
Pointer, heap memory, neither? |
If you're willing to overwrite
array3
then the answer is neither. As seat IDs come in from the user, you could do
array3[inputvalue] = 'x';
. No need to remember the requests.
If the plan needs to be placed in
dispreservedarray
then you'll have to create it on the heap and copy array3 over to it (although in the code you posted before, array3 was uninitialized).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
int* dispreservedarray = new int[maxnumberofelements];
//copy array3 into dispreservedarray (if applicable)
for(int i = 0; i < maxnumberofelements; i++)
{
dispreservedarray[i] = array3[i];
}
//ask for requests from the user, and as they come in, assign them
//to dispreservedarray
//display the array
//finished
delete [] dispreservedarray;
|