delete function

Hey guys I'm having trouble creating a delete function. First I need to scan an array and delete any line I want. Please help. Ill post the whole code that I created. Some errors may appear. Havent checked it yet.

Notice the delete fucntion.

#include<iostream>
#include<iomanip>
using namespace std;
string name[10];
int namectr;
int age[10];
int agectr;
string address[10];
int addctr;
int linenum=1;

void deleterecord()
{
int linedel;
cout<<"What record do you want to delete?: ";
cin>>linedel;
int accu1=linedel-1;
name[accu1]=" ";
age[accu1]=0;
address[accu1]=" ";
}

void editrecord()
{
int line;
cout<<"What record do you want to edit?: ";
cin>>line;
int accu=line-1;
cout<<"New name: ";
cin>>name[accu];
cout<<"New age: ";
cin>>age[accu];
cout<<"New address: ";
cin>>address[accu];

}

void displayrecords()
{
int x;
cout<<"+----------+-----+--------------------+"<<endl;
cout<<"| NAME | AGE | ADDRESS |"<<endl;
cout<<"+----------+-----+--------------------+"<<endl;

for(x=0;x<namectr;x++)
{
if (age[x]==0)
{
continue;
}
if (age[x]>0)
{
cout<<"|"<<linenum++<<". "<<setw(10)<<left<<name[x]<<"|"<<setw(5)<<left<<age[x]<<"|"<<setw(20)<<left<<address[x]<<"|"<<endl;
cout<<"+----------+-----+--------------------+"<<endl;
}
}

}


void getadd()
{
string add;
cout<<"Enter address: ";
cin>>add;
address[addctr]=add;
addctr++;
}

void getage()
{
int age1;
cout<<"Enter age: ";
cin>>age1;
age[agectr]=age1;
agectr++;
}

void getname()
{
string name1;
cout<<"Enter name: ";
cin>>name1;
name[namectr]=name1;
namectr++;

}

void menu()
{
int choice;
cout<<"Menu:"<<endl;
cout<<"1-Add record"<<endl;
cout<<"2-View records"<<endl;
cout<<"3-Edit record"<<endl;
cout<<"4-Delete record"<<endl;
cout<<"5-Exit program"<<endl;
cout<<">> ";
cin>>choice;
if (choice==1)
{
getname();
getage();
getadd();
menu();
}
if (choice==2)
{
displayrecords();
menu();
}
if (choice==3)
{
linenum=0;
editrecord();
displayrecords();
menu();
}
if (choice==4)
{
deleterecord();
displayrecords();
menu();
}
}

main()
{
menu();
system("pause");
}
closed account (2UD8vCM9)
Works fine for me.
yeah but the array is consumed and will never be used again.
closed account (2UD8vCM9)
ah I see what you're saying

so the issue is with the way you're doing the whole getadd getname getage

personally, this is what I would suggest to make the least changes to your code

I combined your 3 functions getadd, getname, getage
Instead of using your namectr int, they use a for loop to determine the first available slot based on if the age is 0
Due to checking if the age is 0, I had to change where int age[10]; is declared to int age[10] = {0}; so they'd initially all be at 0.

I also had to change how DisplayRecords works since it uses some variable called linectr or something. Instead I just made a new variable called CurrentRecord inside of that function.

Let me know if this is what you needed or not please, I hate answering and not getting a reply.

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
#include<iostream>
#include<iomanip>
#include <string>
using namespace std;
string name[10];
int namectr;
int age[10] = {0};
int agectr;
string address[10];
int addctr;
int linenum=1;

void deleterecord()
{
int linedel;
cout<<"What record do you want to delete?: ";
cin>>linedel;
int accu1=linedel-1;
name[accu1]=" ";
age[accu1]=0;
address[accu1]=" ";
}

void editrecord()
{
int line;
cout<<"What record do you want to edit?: ";
cin>>line;
int accu=line-1;
cout<<"New name: ";
cin>>name[accu];
cout<<"New age: ";
cin>>age[accu];
cout<<"New address: ";
cin>>address[accu];

}

void displayrecords()
{
int x;
cout<<"+----------+-----+--------------------+"<<endl;
cout<<"| NAME | AGE | ADDRESS |"<<endl;
cout<<"+----------+-----+--------------------+"<<endl;
int CurrentRecord=1;
for(x=0;x<10;x++)
{
if (age[x]==0)
{
continue;
}
if (age[x]>0)
{
cout<<"|"<<CurrentRecord++<<". "<<setw(10)<<left<<name[x]<<"|"<<setw(5)<<left<<age[x]<<"|"<<setw(20)<<left<<address[x]<<"|"<<endl;
cout<<"+----------+-----+--------------------+"<<endl;
}
}

}

void getrecordinfo()
{
	int AvailableSlot=-1; //Initialize to a number not between 0-9 since our array elements subscripts use 0-9
	for (int i=0; i<10; i++) //Going to loop to find the first available spot in our array when adding a record
	{
		if (age[i]==0) //If we found an available spot
		{
			AvailableSlot=i; //Set AvailableSlot to be our available slot
			break; //Exit the for loop once we find it if we do
		}
	}
	if (AvailableSlot==-1) //If there are no available slots
	{
		cout << "No available slots to add a record." << endl;
		return; //By returning, we exit this function ignoring the rest of the code in the case that there's no room to add a record
	}

	//Once we get to this part in the function, we must have found an AvailableSlot, or else the function will have returned by this point and ended
	//We know the first availableslot is = to our int AvailableSlot
	cout<<"Enter name: ";
	cin>>name[AvailableSlot];
	cout<<"Enter age: ";
	cin>>age[AvailableSlot];
	cout<<"Enter address: ";
	cin>>address[AvailableSlot];
}


void menu()
{
int choice;
cout<<"Menu:"<<endl;
cout<<"1-Add record"<<endl;
cout<<"2-View records"<<endl;
cout<<"3-Edit record"<<endl;
cout<<"4-Delete record"<<endl;
cout<<"5-Exit program"<<endl;
cout<<">> ";
cin>>choice;
if (choice==1)
{
getrecordinfo();
menu();
}
if (choice==2)
{
displayrecords();
menu();
}
if (choice==3)
{
linenum=0;
editrecord();
displayrecords();
menu();
}
if (choice==4)
{
deleterecord();
displayrecords();
menu();
}
}
int main()
{
menu();
system("pause");
}
I have a new project in working on and its really bugging me. Its pretty difficult compared to this problem. Please reply if you wanna see it. I Really need help. Thanks man.
Thanks for the reply man. Theres still one problem though. See the linenum? Thats the guide for the user. Meaning if he wants to delete a line inside the array, he'll follow the linenum. After deleting one line

NAME AGE ADDRESS
name[0] 1. John 15 XXXX //Delete this line.
name[1] 2. Josh 16 XXX

the array is now consumed. So what will happen next is.

NAME AGE ADDRESS
name[0] " " 0 " "
name[1] 1.Josh 16 XXX

so if you put the line num 1 if you want to delete josh from the record, the program will just delete the previously deleted array index.
Hope Im not wasting your time man.
closed account (2UD8vCM9)
sorry, I forgot to check back on the post.

I'm a bit confused at the issue here.

So are you saying that the issue is that if I enter in an entry
ex.
Josh 16 XXX

now that entry is in the array at pos 0

then I enter in another entry
Jimmy 12 xxx
Now this entry is in the array at pos 1

If we delete the Josh entry, and add a new entry, Jimmy will show up as the second entry when we view the list.


Do you just want it to be ordered in the order that they were added or what do you want to happen differently?
Topic archived. No new replies allowed.