Deleting an element in an array and working with it after

Okay, I got this program where the user can add fractions into an array. The user can do various things with that array. Now I got all the different functions working except for the delete function. Here's my code:

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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <iostream>
using namespace std;

struct Fraction
{
	int num;
	int den;
};

void add(Fraction* [], char, int&);
void edit(Fraction* [], char, int&);
void deletefrac (Fraction* [], int&);
int find(Fraction* [], char, int&);
void displayall (Fraction* [], char, int);
void deleteall (Fraction* [], int&);
int main()
{
	Fraction* fracptrs[20] = {0};
	int size = 0;
	int choice;
	char slash = '/';

	do{
		cout<<"Choose an option: \n"
			<<"\t1. Add new fraction\n"
			<<"\t2. Edit fraction\n"
			<<"\t3. Delete fraction\n"
			<<"\t4. Find fraction\n"
			<<"\t5. Display all fractions\n"
			<<"\t6. Show sum of all fractions\n"
			<<"\t7. Delete all fractions\n"
			<<"\t8. Quit\n"
			<<"***You have to add a fraction before selecting options 2 - 7***"
			<<"\nYour option is... ";
		cin>> choice;

		if(choice == 1)
		{
			add(fracptrs, slash, size);		
		}

		else if(choice == 2)
		{
			displayall(fracptrs, slash, size);
			edit(fracptrs, slash, size);
		}
		else if(choice == 3)
		{
			displayall(fracptrs, slash, size);
			deletefrac(fracptrs, size);
		}
		else if(choice == 4)
		{
			int coord = find(fracptrs, slash, size);
			if(coord != -1)
			{
				cout<<"\nThe fraction "<< fracptrs[coord] -> num 
					<< slash << fracptrs[coord] -> den << " is found at spot "
					<< coord+1 << ".\n";
			}
			else
				cout<<"\nThe fraction can not be found in the array.\n";
		}
		
		else if(choice == 5)
		{
			displayall(fracptrs, slash, size);
		}

		//else if(choice == 6)
		
		else
		{
			deleteall(fracptrs, size);
		}

		cin.ignore();
	}while(choice != 8);


	delete []* fracptrs;

	cout << '\n';
	system ("pause");
	return 0;
}
void add(Fraction* fracptrs[], char slash, int& size)//I wasn't asked to use a loop to populate the array
{
	fracptrs[size] = new Fraction[1];
	cout<<"Enter a fraction: ";
	cin>> fracptrs[size] -> num >> slash >> fracptrs[size] -> den;
	cout<<"\nYou added the fraction: " << fracptrs[size] -> num << slash << fracptrs[size] -> den  << '\n';
	size++;
}
void edit(Fraction* fracptrs[], char slash, int& size)
{
	int spot; 
	
	cout<<"Enter the position of the fraction you'd like to edit: ";
	cin>>spot;

	cout<<"Enter your new fraction: ";
	cin>> fracptrs[spot-1] -> num >> slash >> fracptrs[spot-1] -> den;
}
void deletefrac (Fraction* fracptrs[], int& size)
{
	int spot;

	cout<<"Enter the position of the fraction you'd like to delete: ";
	cin>> spot;

	fracptrs[spot-1] = 0;
}
int find(Fraction* fracptrs[], char slash, int& size)
{
	Fraction test;

	cout<<"Enter the fraction you'd like to search: ";
	cin>> test.num >> slash >> test.den;

	for(int i = 0; i < size; i++)
	{
		if(test.num == fracptrs[i]->num && test.den == fracptrs[i]->den)
		{
			return i;
		}
	}

	return -1;
}
void displayall (Fraction* fracptrs[], char slash, int size)//not yet tested
{
	for(int i = 0; i < size; i++)
	{
		if(fracptrs[i] != 0)
			cout<<fracptrs[i] -> num << slash << fracptrs[i] -> den
				<<" ";
	}
}
void deleteall (Fraction* fracptrs[], int& size)
{
	for(; size-1 >= 0; size--)
	{
		delete fracptrs[size];
	}

}


Now, I know I haven't done the function for choice 6, getting the sum of the fraction and I'm not concerned with that, at least not for now. My problem is the deletefrac() function. It deletes the given spot. I kinda forced the displayall to show the array nicely but when I work with the array afterwards, the computer just goes crazy or it just doesnt go through.

For example, I entered 1/2 1/3 1/4. I wanted to delete the 2nd spot (which is 1/3). After deleting, I chose display all fraction. 1/2 and 1/4 shows. When I choose edit and chose spot 2 (which should now be 1/4), the program just stops or something with a breakpoint happens. What should I do???
For example, I entered 1/2 1/3 1/4. I wanted to delete the 2nd spot (which is 1/3). After deleting, I chose display all fraction. 1/2 and 1/4 shows. When I choose edit and chose spot 2 (which should now be 1/4)


And why would spot 2 be 1/4? Your delete function doesn't magically remove an element of the array - it just sets one of the positions to be a null pointer.
From what I can imagine, the array still has 1/2 X 1/4. With X being a null pointer. Sorry for the confusion from the spot 2 "should now be 1/4". What I meant there was spot 2 is supposed to be 1/4 but it isn't because of the X.

With X still being there, it doesn't seem to be possible for the user to work the array (hence editing a fraction that's supposed to be as deep/deeper into the array than the deleted fraction).

Is it still possible to work with this array???
I'm thinking of manually rearranging the array in a for loop but I got a feeling that there's a much simpler and more correct way of dealing with my problem.
The simple way is to use a proper C++ container such as vector.
I haven't been officially taught vectors at school yet so, I'm not allowed to use them.
I hate sounding like I'm brushing off other people's advice but I really am not
Last edited on
I haven't been officially taught vectors at school yet so, I'm not allowed to use them.


I remember at school that going beyond the taught material and using advanced techniques merited more credit, rather than punishment. Still, if that's your teacher's attitude, can't be helped.

In this case, here is how to actually remove an element in the way you want:

1
2
3
4
5
6
7
for ( i= elementToRemove; i< numberOfElementsBeingUsed; ++i)
{
  array[i] = array [i+1]; // Copy over each element with the next element along,
                              // starting with the one to remove.
}
array[numberOfElementsBeingUsed-1] = 0; // and set the final element to zero
numberOfElementsBeingUsed--; // and reduce the number being used  by one 


Last edited on
Yeah, I was thinking the same thing. Lol I wrote the same exact loop just now except for decrementing part at the end.

Now, this is what I call the brute force + safest way :)

Thanks for all your help.
Topic archived. No new replies allowed.