Hello guys, just wondering why my program crashes when I try to delete a struct from my array. I'm trying to code so that the user searches for a student via a student ID, then opt to delete the searched student's record. I thought I got it right and the search works fine, but it crashes when I opt to delete the student's record! I know it's due to the actual deleting part in my code. Here's my whole code:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
void clrscr()
{
system("cls");
}
void pause()
{
system("echo.");system("echo.");system("pause");
}
void displayMenu(string msg)
{
clrscr();
cout << msg << "\n\nMAIN MENU\n"
"\n0. Exit"
"\n1. Search for a student"
"\n2. List students enrolled in a course"
"\n3. List students eligible to graduate"
"\n4. List all students"
"\n5. Update a student record"
"\n6. Add a student record"
"\n7. Delete a student record"
"\n\nYour choice is ->";
};
const int MAXRECORD = 500;
struct stdRecord
{
string studentID;
string studentName;
int courseCode;
int creditPoint;
};
const int NUMRECS = 3;
int main()
{
int option, i;
string word, stdIDInput, msg="Please type in the number of the corresponding \ntask you wish to perform then press enter.";
char choice;
stdRecord stdRec[NUMRECS]={{"15000000","Joshua Andrew Smith", 3506, 240},
{"16666666", "Jack Williams", 3506, 180},
{"17000010", "Lily Jones", 3639, 110}};
do
{
displayMenu(msg);
cin.clear();
if(!(cin >> option))
{
if(!cin.eof())
{
cin.clear();
cin >> word;
}
continue;
}
switch(option)
{
case 1: // this searches for a student via their studentID
do
{
cout << "Please type in the student ID number\nof the student you want to search,\nthen press enter.\n";
cin >> stdIDInput;
cout << "\nStudent ID Student Name Course Code Credit Points\n\n";
cout << setiosflags(ios::left);
bool found = false;
for(i = 0; i < NUMRECS; i++)
{
if(stdIDInput == stdRec[i].studentID)
{
found = true;
cout << setw(12) << stdRec[i].studentID
<< setw(21) << stdRec[i].studentName
<< setw(13) << stdRec[i].courseCode
<< setw(5) << stdRec[i].creditPoint << endl;
}
}
if(!found)
{
cout << "Not Found.\n";
}
cout << "Would you like to search for a student again? Y or N.\n";
cin >> choice;
}while(choice != 'N');
pause ();
break;
case 2: // this searches for students enrolled in a course, searches via course code
int courseCodeIn;
do
{
cout << "Please type in a valid course code (the SCM offers courses 3506, 3633, 3634 \nand 3639) to display students in that course, then press enter.\n"
<< "Note: there may or may not be students enrolled ina a course,\nin the case of the latter, 'Not Found' will be displayed.";
cin >> courseCodeIn;
cout << "\nStudent ID Student Name Course Code Credit Points\n\n";
cout << setiosflags(ios::left);
bool found = false;
for(i = 0; i < NUMRECS; i++)
{
if(courseCodeIn == stdRec[i].courseCode)
{
found = true;
cout << setw(12) << stdRec[i].studentID
<< setw(21) << stdRec[i].studentName
<< setw(13) << stdRec[i].courseCode
<< setw(5) << stdRec[i].creditPoint << endl;
}
}
if(!found)
{
cout << "Not Found.\n";
}
cout << "Would you like to search via a course code again? Y or N.\n";
cin >> choice;
}while(choice != 'N');
pause ();
break;
case 3: // lists students eligible to graduate
cout << "\nStudent ID Student Name Course Code Credit Points\n\n";
cout << setiosflags(ios::left);
for(i = 0; i < NUMRECS; i++)
{
if(stdRec[i].creditPoint == 240)
{
cout << setw(12) << stdRec[i].studentID
<< setw(21) << stdRec[i].studentName
<< setw(13) << stdRec[i].courseCode
<< setw(5) << stdRec[i].creditPoint << endl;
}
}
pause ();
break;
case 4: // lists all students
cout << "\nStudent ID Student Name Course Code Credit Points\n\n";
cout << setiosflags(ios::left);
for (i = 0; i < NUMRECS; i++)
cout << setw(12) << stdRec[i].studentID
<< setw(21) << stdRec[i].studentName
<< setw(13) << stdRec[i].courseCode
<< setw(5) << stdRec[i].creditPoint << endl;
pause ();
break;
case 5: // allows the user to alter a student's course code and/or credit points
int courseCodeUpdate, creditPointsUpdate;
char decision;
do
{
cout << "Please type in the student ID number\nof the student you want to search,\nthen press enter.\n";
cin >> stdIDInput;
cout << "\nStudent ID Student Name Course Code Credit Points\n\n";
cout << setiosflags(ios::left);
bool found = false;
for(i = 0; i < NUMRECS; i++)
{
if(stdIDInput == stdRec[i].studentID)
{
found = true;
cout << setw(12) << stdRec[i].studentID
<< setw(21) << stdRec[i].studentName
<< setw(13) << stdRec[i].courseCode
<< setw(5) << stdRec[i].creditPoint << endl;
}
}
if(!found)
{
cout << "Not Found.\n";
}
if(found == true)
{
do
{
cout << "Please type in the number\n of the corresponding choice below, then press enter.\n\n"
<< "Z. Exit\nA. Update course code\nB. Update credit points\nC. Update both course code and credit points\n";
cin >> decision;
if (decision == 'A')
{
cout << "Please type in the updated course code.\n";
cin >> courseCodeUpdate;
stdRec[i].courseCode = courseCodeUpdate;
cout << "The student's course code has now been updated to "
<< stdRec[i].courseCode << endl;
}
else if (decision == 'B')
{
cout << "Please type in the updated credit points.\n";
cin >> creditPointsUpdate;
stdRec[i].creditPoint = creditPointsUpdate;
cout << "The student's credit points have now been updated to "
<< stdRec[i].creditPoint << endl;
}
else if (decision == 'C')
{
cout << "Please type in the updated course code.\n";
cin >> courseCodeUpdate;
stdRec[i].courseCode = courseCodeUpdate;
cout << "The student's course code has now been updated to "
<< stdRec[i].courseCode
<< "\nNow please type in the updated credit points.\n";
cin >> creditPointsUpdate;
stdRec[i].creditPoint = creditPointsUpdate;
cout << "The student's credit points have now been updated to "
<< stdRec[i].creditPoint << endl;
}
else
{
cout << "No valid character from options Z, A, B, or C have been chosen.";
}
} while (decision != 'Z');
}
cout << "\nWould you like to search for a student again? Y or N.\n";
cin >> choice;
} while (choice != 'N');
pause ();
break;
// case 6 is still in the works and chucks up errors, so I've removed it temporarily
case 7: // this allows the user to delete a student's record
char decision2, choice2;
int studentCount;
studentCount = NUMRECS;
do
{
cout << "Please type in the student ID number\nof the student you want to search,\nthen press enter.\n";
cin >> stdIDInput;
cout << "\nStudent ID Student Name Course Code Credit Points\n\n";
cout << setiosflags(ios::left);
bool found = false;
for(i = 0; i < NUMRECS; i++)
{
if(stdIDInput == stdRec[i].studentID)
{
found = true;
cout << setw(12) << stdRec[i].studentID
<< setw(21) << stdRec[i].studentName
<< setw(13) << stdRec[i].courseCode
<< setw(5) << stdRec[i].creditPoint << endl;
}
}
if(!found)
{
cout << "Not Found.\n";
}
if(found == true)
{
do
{
cout << "Would you like to delete this student's record? Y/N.\n";
cin >> decision2; // ***HERE! It crashes just after the decision is input and I'm pretty sure my loop for overwriting the deleting record is wrong so...
stdRec[i].studentID = stdRec[i+1].studentID;
stdRec[i].studentName = stdRec[i+1].studentName;
stdRec[i].courseCode = stdRec[i+1].courseCode;
stdRec[i].creditPoint = stdRec[i+1].creditPoint;
} while (decision2 != 'N');
}
cout << "Would you like to search and/or delete another student's record?.\n";
cin >> choice2;
} while (choice2 != 'N');
pause ();
break;
default:
cout << "No options from choices one to seven have been typed.";
pause ();
break;
}
} while (option!=0);
return 0;
}
Just case 7 (so you don't have to worry about the rest of the code getting in the way):
case 7:
char decision2, choice2;
int studentCount;
studentCount = NUMRECS;
do
{
cout << "Please type in the student ID number\nof the student you want to search,\nthen press enter.\n";
cin >> stdIDInput;
cout << "\nStudent ID Student Name Course Code Credit Points\n\n";
cout << setiosflags(ios::left);
bool found = false;
for(i = 0; i < NUMRECS; i++)
{
if(stdIDInput == stdRec[i].studentID)
{
found = true;
cout << setw(12) << stdRec[i].studentID
<< setw(21) << stdRec[i].studentName
<< setw(13) << stdRec[i].courseCode
<< setw(5) << stdRec[i].creditPoint << endl;
}
}
if(!found)
{
cout << "Not Found.\n";
}
if(found == true)
{
do
{
cout << "Would you like to delete this student's record? Y/N.\n";
cin >> decision2;
stdRec[i].studentID = stdRec[i+1].studentID;
stdRec[i].studentName = stdRec[i+1].studentName;
stdRec[i].courseCode = stdRec[i+1].courseCode;
stdRec[i].creditPoint = stdRec[i+1].creditPoint;
} while (decision2 != 'N');
}
cout << "Would you like to search and/or delete another student's record?.\n";
cin >> choice2;
} while (choice2 != 'N');
pause ();
break;
Any direction as to how I can change my loop/s would be MUCH appreciated! Please and thank-you!