Inserting data inside array through function

I want to add a string into a specific position in an array through a function. The way I tried doing it is by first finding the position in the array, storing that value into a temp string, then shifting each one after that by one. When I print out the list, the string two positions away is always overwritten. I do not know why it is not shifting over. I have tried to shorten my code so that only the relevant things are shown.

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <string>

int menu();
std::string* insertName(std::string list[]);
void printNames(std::string list[]);

int main()
{
	int choice;
	std::string listNames[10] = { "Bill","Bob","Jane","Tyler","Joe" };

	do {
		choice = menu();

		switch (choice)
		{
		case 2: insertName(listNames);
			break;
		case 4: printNames(listNames);
			break;
		default: break;
		}

		std::cout << "~~~~~~~~~~~~~~~~~~\n\n";

	} while (choice != 5);

	system("pause");

	return 0;
}

int menu()
{
	int choice;

	std::cout << "       Menu\n~~~~~~~~~~~~~~~~~~\n";
	std::cout << "2) Insert a name\n";
	std::cout << "4) Print all names\n";
	std::cout << "5) Exit\n";
	std::cin >> choice;

	return choice;
}

std::string* insertName(std::string list[])
{
	int position;
	std::string temp;
	std::string newName;
	std::cout << "What position in the list would you like to insert a name? (1-10)\n";
	std::cin >> position;
	for (int i = position - 1; i < 10 - position; i++)
	{
		if (i == (position - 1))
		{
			std::cout << "Enter name: ";
			std::cin >> newName;
			temp = list[i];
			list[i] = newName;
		}
		else if (i > (position - 1))
		{
			list[i] = temp;
			temp = list[i + 1];
		}
	}
	return list;
}

void printNames(std::string list[])
{
	std::cout << "       List\n~~~~~~~~~~~~~~~~~~\n";
	for (int i = 0; i < 10; i++)
	{
		if (list[i] == "")
			break;
		std::cout << (i + 1) << ". " << list[i] << "\n";
	}

}
Last edited on
Why would you insert something into a non dynamic array? Eventually your going to start losing data and causing segmentation faults or whatever it is when you index past the array.
This is an assignment for my data structures class, my instructor wants us to use a fixed size array for some reason.
Make i < 10 - position into i < 10 because you’re still trying to get all the way to index 9. If you say 10 - position, it will immediately exit and not actually move everything over.
I tried that, it gives me an exception error. I think it's losing data when I do that just like you mentioned
Hm.. does it have to be only 10 long? Just make it massive, but only allow a certain number of names that’s less than the actual size.

Edit: Like make listNames [11] instead of [10] and have listNames[10] just be a dump space.
Last edited on
do it on paper first so you realise the error in your logic
may also try an step-by-step run through your debugger

get 8 poker cards and put them in a row so no card is touching another, that's your array
cards may move left or right, but they can't go left of position 0 or right of position 9, at no point two cards may overlap
now try to insert another card at position 3
if you need space, you may put a card between the fingers of your left hand, but remember, at no point two cards may overlap

look carefully the operations that you need to do and translate them to pseudocode
also could you send the error?
I've tried to retrace it using the example you gave but I still can't find the problem. I see a problem when I have a full list of 10 cards, but not when I have less. Unless you mean to say that, since I have the memory allocated for 10 in my array, I will always be going right of position 9 no matter what. Unless I change the for loop to be one less, I see no other way to fix this. When I do change it to 9 though, I'm just back to my original problem. I can not think of any way to bypass this.

Error is:

Exception thrown at 0x0F402CEE (vcruntime140d.dll) in Project1.exe: 0xC0000005: Access violation reading location 0xCCCCCCCC.

Also, I did try to make the array bigger, but then I'm just back to the same problem I had to begin with. Either that, or this same error
Last edited on
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
//list = {"Bill","Bob","Jane","Tyler","Joe"}
//say that position=3 (so before "Jane") and newName = "Anne"
//so the wanted result is {"Bill","Bob","Anne","Jane","Tyler","Joe"}
//for (int i = position - 1; i < 10 - position; i++)
for (int i = 3 - 1; i < 10 - 3; i++) //traverse the array from index 2 to 7 (¿?)
	{
		if (i == (position - 1))
		{
			//this will only execute on the first iteration (perhaps should be outside the loop)
			//i=2
			temp = list[i];
			list[i] = newName;
			//so now we have temp = "Jane"
			//list = {"Bill","Bob","Anne","Tyler","Joe"}
		}
		else if (i > (position - 1))
		{
			//let's check the second iteration, i=3, temp="Jane"
			list[i] = temp;
			//list = {"Bill","Bob","Anne","Jane","Joe"}
			//there you lost "Tyler", I told you to not overlap the cards

			temp = list[i + 1];
			//here temp = "Joe"
			//in the next iteration you'll do list[4] = temp, so "Joe"="Joe"
			//so you won't change the rest of the array
		}
	}


try again
you've got 8 cards in a row, explain in plain english how do you insert another card at position 3
cards may move left, right or to your hand, no two cards may ever overlap.
Last edited on
@ne555 Ah, okay I think I get it now. So when you say that I can have a card in my hand, that is referring to the temp? So I find the position I want to insert newName in, hold the string from list in temp, then I proceed to move each string, then at the end insert newName into the given position?
yes, whatever number of temporaries that you need

> then I proceed to move each string
¿how do you move them?
Topic archived. No new replies allowed.