Hello, I was supposed to make a student database. I have made the code however, there is one problem which I am facing and I have been unable to solve it.
The problem is with the deletion function...
First, here is the 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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
void initialize();
void print(int);
void printstu(int);
bool menu();
void update();
void add();
void semsearch();
void namesearch();
void deleteinfo();
void duplicate();
struct date {
int d;
int m;
int y;
void input()
{
cin >> d >> m >> y;
};
};
enum extracurricularSkill { SocietyMember, Sportsman, InternetGeek, None };
void printenum(extracurricularSkill);
struct student {
int id;
string name;
date dob;
char gen;
int sem;
string dep;
float gpa;
extracurricularSkill extra;
void input()
{
cout << "Enter ID: ";
cin >> id;
getchar();
cout << "Enter Nmae: ";
getline(cin, name);
cout << "Enter Date of Birth (D/M/Y): ";
dob.input();
cout << "Enter Gender: ";
cin >> gen;
cout << "Enter Semester: ";
cin >> sem;
getchar();
cout << "Enter Department: ";
getline(cin, dep);
cout << "Enter GPA: ";
cin >> gpa;
int a;
cout << "Pick the Extracurricular Skill, " << endl
<< "1) SocietyMember" << endl
<< "2) Sportsman" << endl
<< "3) InternetGeek" << endl
<< "4) None" << endl
<< "Enter skill: ";
cin >> a;
a--;
extra = (extracurricularSkill)a;
}
void output()
{
cout << left << setw(6) << id;
cout << left << setw(20) << name;
cout << left << setw(10) << gen;
cout << left << setw(3) << dob.d << setw(3) << dob.m << setw(14) << dob.y;
cout << left << setw(15) << sem;
cout << left << setw(15) << dep;
cout << left << setw(10) << gpa;
cout << left << setw(30);
printenum(extra);
cout << endl;
}
};
int num = 5;
student *s = new student[num];
int main()
{
initialize();
bool fh = true;
while (fh)
{
fh = menu();
}
cout << endl;
system("pause");
return 0;
}
void initialize()
{
for (int i = 0, j = 1; j <= 5; j++, i++)
{
s[i].id = j;
s[i].gen = 'M';
s[i].gpa = 3.14;
s[i].sem = j;
s[i].dep = "CS";
s[i].dob.d = j + 20;
s[i].dob.m = j + 1;
s[i].dob.y = 1999 - j;
s[i].extra = InternetGeek;
}
s[0].name = "Ahmed Nadeem";
s[1].name = "Zurriat Baloch";
s[2].name = "Ahmed Nadeem";
s[2].id = 1;
s[3].name = "Mamoon Amin";
s[4].name = "Tahir Baloch";
s[2].sem = 1;
s[1].gen = 'F';
s[1].dep = "BBA";
s[4].dep = "EE";
}
bool menu()
{
cout << right << setw(50) << "Student Database" << endl;
cout << endl << "1) Display Information of all students" << endl
<< "2) Update Information of an existing student" << endl
<< "3) Add information of a new student" << endl
<< "4) Search and display specific student's information by NAME" << endl
<< "5) Search and display specific student's information by SEMESTER and DEPARTMENT" << endl
<< "6) Delete a student's record" << endl
<< "7) Search for Duplicated Entries" << endl
<< "8) " << endl
<< "9) Quit the Porgram" << endl << endl;
cout << "Please Enter Your Selection: ";
int ch;
cin >> ch;
switch (ch)
{
case 1:
print(0);
break;
case 2:
update();
break;
case 3:
add();
break;
case 4:
namesearch();
break;
case 5:
semsearch();
break;
case 6:
deleteinfo();
break;
case 7:
duplicate();
break;
case 8:
break;
case 9:
return false;
break;
default:
cout << "You entered an invalid choice. Please enter again";
break;
}
return true;
}
void print(int a)
{
int sem = -1;
string dep = "None";
if (a == 1)
{
cout << "Enter Semester: ";
cin >> sem;
}
if (a == 2)
{
getchar();
cout << "Enter Department: ";
getline(cin, dep);
}
cout << left << setw(6) << "ID";
cout << left << setw(20) << "Name";
cout << left << setw(10) << "Gender";
cout << left << setw(20) << "Date of Birth";
cout << left << setw(15) << "Semester";
cout << left << setw(15) << "Department";
cout << left << setw(10) << "GPA";
cout << left << setw(30) << "Extracurricular Skill";
cout << endl << endl;
for (int i = 0; i < num; i++)
{
if (a == 1 && s[i].sem == sem)
{
s[i].output();
}
else if (a == 2 && dep == s[i].dep)
{
s[i].output();
}
else if (a == 0)
{
s[i].output();
}
}
}
void printstu(int i)
{
cout << "ID: " << s[i].id << endl;
cout << "Name: " << s[i].name << endl;
cout << "Gender: " << s[i].gen << endl;
cout << "Semester: " << s[i].sem << endl;
cout << "Department: " << s[i].dep << endl;
cout << "GPA: " << s[i].gpa << endl;
cout << "Extracurricular Skill: ";
printenum(s[i].extra);
cout << endl << endl;
}
void duplicate()
{
bool fh = true;
for (int i = 0; i < num; i++)
{
for (int j = i+1; j < num; j++)
{
if (s[i].name == s[j].name && s[i].id == s[j].id && s[i].gen == s[j].gen && s[i].sem == s[j].sem && s[i].dep == s[j].dep && s[i].gpa == s[j].gpa && s[i].extra == s[j].extra)
{
swap(s[i], s[num = 1]);
num--;
fh = false;
}
}
}
if (fh == false)
{
cout << endl << "Duplicate Entry found and deleted. ";
}
else
cout << endl << "No Duplicate Entry found.";
cout << endl << endl << "Press Enter to continue";
getchar();
}
void printenum(extracurricularSkill extra)
{
switch (extra)
{
case 0:
cout << "SocietyMember";
break;
case 1:
cout << "Sportsman";
break;
case 2:
cout << "InternetGeek";
break;
default:
cout << "None";
break;
}
}
|
The problem that is occurring is with duplicate() function and the print(int a = 0) function.
I have made a duplicate entry in my code... This is what you get when you press 1 at start of program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
Student Database
1) Display Information of all students
2) Update Information of an existing student
3) Add information of a new student
4) Search and display specific student's information by NAME
5) Search and display specific student's information by SEMESTER and DEPARTMENT
6) Delete a student's record
7) Search for Duplicated Entries
8)
9) Quit the Porgram
Please Enter Your Selection: 1
ID Name Gender Date of Birth Semester Department GPA Extracurricular Skill
1 Ahmed Nadeem M 21 2 1998 1 CS 3.14 InternetGeek
1 Ahmed Nadeem M 23 4 1996 1 CS 3.14 InternetGeek
4 Mamoon Amin M 24 5 1995 4 CS 3.14 InternetGeek
|
As you can see, the entry number 1 and 3 are same... So, when I press 7, it is supposed to delete the entry number 1.
And now, when I do press 7, it does show that the duplicate entry has been deleted but when I again press 1 now, it shows no users... I am unable to understand the problem here. From what I get, it should show all the other remaining entries but it shows no entry now...
This is what I get after pressing 7 and then 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
Please Enter Your Selection: 7
Duplicate Entry found and deleted.
Student Database
1) Display Information of all students
2) Update Information of an existing student
3) Add information of a new student
4) Search and display specific student's information by NAME
5) Search and display specific student's information by SEMESTER and DEPARTMENT
6) Delete a student's record
7) Search for Duplicated Entries
8)
9) Quit the Porgram
Please Enter Your Selection: 1
ID Name Gender Date of Birth Semester Department GPA Extracurricular Skill
|
Can someone help and identify the problem, please?
Thank you!
(I have deleted some of the code and some of the irrelevant output because of length limit)