-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicArray.cpp
More file actions
131 lines (114 loc) · 3.06 KB
/
DynamicArray.cpp
File metadata and controls
131 lines (114 loc) · 3.06 KB
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
#include <iostream>
#include <string>
using namespace std;
void addName(string *&DArr, int &size, string newName)
{
string *temp = new string[size + 1];
for (int i = 0; i < size; i++)
{
temp[i] = DArr[i];
}
temp[size] = newName;
delete[] DArr;
DArr = temp;
++size;
}
void deleteName(string *&DArr, int &size, int index)
{
string *temp = new string[size + 1];
for (int i = 0; i < size; i++)
{
temp[i] = DArr[i];
}
for (int i = index; i < size - 1; i++)
{
temp[i] = DArr[i + 1];
}
delete[] DArr;
DArr = temp;
--size;
}
void searchName(string *&DArr, int &size, string nametosearch)
{
for (int i = 0; i < size; i++)
{
if (DArr[i] == nametosearch)
{
cout << "Name found at index : " << i << endl;
}
}
}
int main()
{
int n = 5;
string *DArr = new string[n]; // Initial allocation for 5 names
cout << "Enter " << n << " names:" << endl;
for (int i = 0; i < n; i++)
{
cin >> DArr[i];
}
string resp; // Declare input variable here
cout << "\n--- All Names ---" << endl;
for (int i = 0; i < n; i++)
{
cout << DArr[i] << endl;
}
while (true)
{
cout << "select operation" << endl;
cout << "Search" << endl;
cout << "Add" << endl;
cout << "delete" << endl;
cout << "Exit" << endl;
cin >> resp;
if (resp == "add" || resp == "Add")
{
string newName;
cout << "Enter new name to add: ";
cin >> newName;
addName(DArr, n, newName);
}
else if (resp == "search" || resp == "Search")
{
string Sname;
cout << "enter name to search : ";
cin >> Sname;
searchName(DArr, n, Sname); // Exit the loop if the user enters "no"
}
else if (resp == "exit" || resp == "Exit")
{
break;
}
else if (resp == "delete" || resp == "Delete")
{
int index;
cout << "Enter index of name to delete: ";
cin >> index;
if (index >= 0 && index < n)
{
deleteName(DArr, n, index);
cout << "Name at index " << index << " deleted." << endl;
}
else
{
cout << "Invalid index. Please enter a valid index." << endl;
}
}
else
{
cout << "Invalid input. Please select Above operations ." << endl;
// Optionally, you might want to clear the input buffer here if you
// anticipate issues with malformed input, but for simple 'yes/no'
// responses, it's often not strictly necessary.
}
}
cout << "\n--- All Names ---" << endl;
for (int i = 0; i < n; i++)
{
cout << DArr[i] << endl;
}
// Don't forget to free the dynamically allocated memory
delete[] DArr;
// This line is not necessary as it does not delete the last element properly
return 0; //
}