Deleting element from array

Mar 10, 2013 at 6:49pm
Hey all, I have code that is set up to delete an element from an array. The array works perfectly fine as long as the user does not input the 0th array element to be deleted; I am not entirely sure why. If you can see why, please point that out for me! Much appreciated!

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
//============================================================================
// Name        : dsafnaskfj.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
using namespace std;

int arrayA(int theArray[],int size)
{
	cout << "Array: " << '{';
	int i = 0;
	while (i < size)
	{
		cout << theArray[i];
		if (i < size-1){cout << ',';}
		i++;
	}
	cout << '}' << endl << "Want to delete something (y/n)? ";
	char ansr;
	cin >> ansr;
	if (ansr == 'y')
	{
		cout << "Offset to delete? ";
		int offset;
		cin >> offset;
		return offset;
	}
	else if (ansr == 'n')
	{
		cout << "Goodbye!";

	}
	return 0;
}

int	arrayB(int theArray2[], int size, int offset)
{
	if ((offset >= size)||(offset < 0)){ cout << "Sorry, cannot delete that." << endl; return size;}
		theArray2[offset]=theArray2[size-1];
		size--;


	return size;
}



int main() {
	int N = 10;
	int y;
	int fling[]={1,2,3,4,5,6,7,8,9,10};

	do{
	y = arrayA(fling, N);
	N = arrayB(fling, N, y);

	}while ((N > 0)&&(y != 0));



	return 0;
}
Mar 10, 2013 at 8:14pm
I altered the code such that it shifts every element in the array to the left once the ith element is deleted. However, I am still having the issue. All of the offsets delete with ease, however, when the index is chosen to be 0, the program immediately terminates.
Mar 11, 2013 at 3:52am
when you enter 0 the function on line 58 y = arrayA(fling, N);returns a zero to y.

on line 61 the ((N > 0)&&(y != 0)); causes the loop to exit
Topic archived. No new replies allowed.