I need to write a function that adds entered values to a sequence AFTER the current_index

void sequence::attach(const value_type& entry)
	{
		
		assert(size( ) < CAPACITY);
		
		size_type j;
			
		if (!is_item())
			current_index=0;

		for (j=used; j>current_index; --j)
			data[j] = data[j-1];

		data[current_index] = entry;

		++used;
	}

current_index is the position in the dara array where it is currently pointed to

used is the number of used slots in the array

so i have the for loop to copy the current numbers to the next slot on the right in the array but then new value that is to be inserted has to be after the current_index and the function i have now places it before

i have no idea how to make that change

Recommended Answers

All 5 Replies

but then new value that is to be inserted has to be after the current_index and the function i have now places it before

i have no idea how to make that change

data[current_index+1] = entry;
data[current_index+1] = entry;

I tried that but when i test it by putting in 10, 20, 30 it gives me some weird number with e first, then 30, 20 and the correct result should be 10, 20, 30.

...

Line 12 is to copy the value to the next place in the array.

Lets say the sequence is 10 20 30 and i want to insert a 5 after the 10 so I have to move over the 20 and 30 to the right and the number used increases.

This code was in the textbook, but it places the item before and not after the current_index.

I suggest displaying all the values before and after you try the insert.
Display the array, used, current_index, and any other variables that will help you figure out the problem.

Run the code in the debugger is also a good idea.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.