Hi guys, what I'm trying to do is allow the user to add a new student record to the system. If the user inputs a student ID already existing in the system, it doesn't accept the ID, but if it's a new student ID, it asks the user if they want to add the student to the system. When the user opts 'Y', it crashes! It's not supposed to! Here's the relevant code:
case 6://add a student record
{
int first = 0;
int last = NUMRECS;
int mid;
int studentCount, newStdCourseCode, newStdCreditPoint;
studentCount = NUMRECS;
char decision3, option2, optAgain5, optAgain6, optAgain7, optAgain8, optAgain9, optAgain10, optAgain11, optAgain12;
string newStdName;
do
{
cout << "Please type in the student ID number\nof the student you want to input,\nthen press enter.\n";
cin >> stdIDInput;
bool found = false;
while (first <= last && !found)//checks if there's a match
{
mid = (first + last)/2;
if (stdRec[mid].studentID == stdIDInput)
{
found = true;
cout << "This student ID already exists in the system.\n";
cout << "\nStudent ID Student Name Course Code Credit Points\n\n";
cout << setiosflags(ios::left);
cout << setw(12) << stdRec[mid].studentID
<< setw(21) << stdRec[mid].studentName
<< setw(13) << stdRec[mid].courseCode
<< setw(5) << stdRec[mid].creditPoint << endl;
}
else if (stdRec[mid].studentID > stdIDInput)
{
last = mid -1;
}
else
{
first = mid +1;
}
}
if (!found)
{
cout << "Would you like to add the student to the records? Y/N\n";
cin >> decision3;
if(decision3 == 'Y')//***HERE!!! This is when it crashes if you opt 'Y' and I don't know how to fix it!
{
last = NUMRECS;
studentCount++;
if((stdIDInput > "17000010") && (stdIDInput < "99999999"))
{
stdRec[last+1].studentID = stdIDInput;
cout << "Please input the new student's name.\n";
getline (cin, newStdName);
stdRec[last+1].studentName = newStdName;
do
{
cout << "\n\nPlease input the new student's course code (the SCM offers courses 3506, 3633, 3634 \nand 3639).\n";
cin >> newStdCourseCode;
if ((newStdCourseCode == 3506) || (newStdCourseCode == 3633) || (newStdCourseCode == 3634) || (newStdCourseCode == 3639))
{
stdRec[last+1].courseCode = newStdCourseCode;
}
else
{
cout << "Not a valid course code. SCM only offers courses 3506, 3633, 3634 or 3639.\n"
<< "Would you like to try again? Y/N";
cin >> optAgain5;
if(optAgain5 == 'N')
{
break;
}
}
}while (optAgain5 = 'Y');
do
{
cout << "Please input the new student's credit points. Valid credit points can range from 0-240 as multiples of 10.\n";
cin >> newStdCreditPoint;
if ((newStdCreditPoint == 0) || (newStdCreditPoint == 10) || (newStdCreditPoint == 20) || (newStdCreditPoint == 30) || (newStdCreditPoint == 40) || (newStdCreditPoint == 50) || (newStdCreditPoint == 60) || (newStdCreditPoint == 70) || (newStdCreditPoint == 80) || (newStdCreditPoint == 90) || (newStdCreditPoint == 100) || (newStdCreditPoint == 110) || (newStdCreditPoint == 120) || (newStdCreditPoint == 130) || (newStdCreditPoint == 140) || (newStdCreditPoint == 150) || (newStdCreditPoint == 160) || (newStdCreditPoint == 170) || (newStdCreditPoint == 180) || (newStdCreditPoint == 190) || (newStdCreditPoint == 200) || (newStdCreditPoint == 210) || (newStdCreditPoint == 220) || (newStdCreditPoint == 230) || (newStdCreditPoint == 240))
{
stdRec[last+1].creditPoint = newStdCreditPoint;
}
else
{
cout << "Not a valid credit point. Valid credit points can range from 0-240 as multiples of 10.\n"
<< "Would you like to try again? Y/N\n";
cin >> optAgain9;
if (optAgain9 == 'N')
{
break;
}
}
}while (optAgain9 = 'Y');
cout << "The student's information has now been added to the records.\n";
cout << "\nStudent ID Student Name Course Code Credit Points\n\n";
cout << setiosflags(ios::left);
cout << setw(12) << stdRec[last+1].studentID
<< setw(21) << stdRec[last+1].studentName
<< setw(13) << stdRec[last+1].courseCode
<< setw(5) << stdRec[last+1].creditPoint << endl;
}
if((stdIDInput > "16666666") && (stdIDInput < "17000010"))
{
stdRec[last+1].studentID = stdRec[last].studentID;
stdRec[last+1].studentName = stdRec[last].studentName;
stdRec[last+1].courseCode = stdRec[last].courseCode;
stdRec[last+1].creditPoint = stdRec[last].creditPoint;
stdRec[last].studentID = stdIDInput;
cout << "Please input the new student's name.\n";
getline (cin, newStdName);
stdRec[last].studentName = newStdName;
do
{
cout << "\n\nPlease input the new student's course code (the SCM offers courses 3506, 3633, 3634 \nand 3639).\n";
cin >> newStdCourseCode;
if ((newStdCourseCode == 3506) || (newStdCourseCode == 3633) || (newStdCourseCode == 3634) || (newStdCourseCode == 3639))
{
stdRec[last].courseCode = newStdCourseCode;
}
else
{
cout << "Not a valid course code. SCM only offers courses 3506, 3633, 3634 or 3639.\n"
<< "Would you like to try again? Y/N";
cin >> optAgain6;
if(optAgain6 == 'N')
{
break;
}
}
}while (optAgain6 = 'Y');
do
{
cout << "Please input the new student's credit points. Valid credit points can range from 0-240 as multiples of 10.\n";
cin >> newStdCreditPoint;
if ((newStdCreditPoint == 0) || (newStdCreditPoint == 10) || (newStdCreditPoint == 20) || (newStdCreditPoint == 30) || (newStdCreditPoint == 40) || (newStdCreditPoint == 50) || (newStdCreditPoint == 60) || (newStdCreditPoint == 70) || (newStdCreditPoint == 80) || (newStdCreditPoint == 90) || (newStdCreditPoint == 100) || (newStdCreditPoint == 110) || (newStdCreditPoint == 120) || (newStdCreditPoint == 130) || (newStdCreditPoint == 140) || (newStdCreditPoint == 150) || (newStdCreditPoint == 160) || (newStdCreditPoint == 170) || (newStdCreditPoint == 180) || (newStdCreditPoint == 190) || (newStdCreditPoint == 200) || (newStdCreditPoint == 210) || (newStdCreditPoint == 220) || (newStdCreditPoint == 230) || (newStdCreditPoint == 240))
{
stdRec[last].creditPoint = newStdCreditPoint;
}
else
{
cout << "Not a valid credit point. Valid credit points can range from 0-240 as multiples of 10.\n"
<< "Would you like to try again? Y/N\n";
cin >> optAgain10;
if (optAgain10 == 'N')
{
break;
}
}
}while (optAgain10 = 'Y');
cout << "The new student has now been added to the record as below.\n";
cout << "\nStudent ID Student Name Course Code Credit Points\n\n";
cout << setiosflags(ios::left);
cout << setw(12) << stdRec[last].studentID
<< setw(21) << stdRec[last].studentName
<< setw(13) << stdRec[last].courseCode
<< setw(5) << stdRec[last].creditPoint << endl;
}
if((stdIDInput > "15000000") && (stdIDInput < "16666666"))
{
stdRec[last+1].studentID = stdRec[last].studentID;
stdRec[last+1].studentName = stdRec[last].studentName;
stdRec[last+1].courseCode = stdRec[last].courseCode;
stdRec[last+1].creditPoint = stdRec[last].creditPoint;
stdRec[last].studentID = stdRec[last-1].studentID;
stdRec[last].studentName = stdRec[last-1].studentName;
stdRec[last].courseCode = stdRec[last-1].courseCode;
stdRec[last].creditPoint = stdRec[last-1].creditPoint;
stdRec[last-1].studentID = stdIDInput;
cout << "Please input the new student's name.\n";
getline (cin, newStdName);
stdRec[last-1].studentName = newStdName;
do
{
cout << "\n\nPlease input the new student's course code (the SCM offers courses 3506, 3633, 3634 \nand 3639).\n";
cin >> newStdCourseCode;
if ((newStdCourseCode == 3506) || (newStdCourseCode == 3633) || (newStdCourseCode == 3634) || (newStdCourseCode == 3639))
{
stdRec[last-1].courseCode = newStdCourseCode;
}
else
{
cout << "Not a valid course code. SCM only offers courses 3506, 3633, 3634 or 3639.\n"
<< "Would you like to try again? Y/N";
cin >> optAgain7;
if(optAgain7 == 'N')
{
break;
}
}
}while (optAgain7 = 'Y');
do
{
cout << "Please input the new student's credit points. Valid credit points can range from 0-240 as multiples of 10.\n";
cin >> newStdCreditPoint;
if ((newStdCreditPoint == 0) || (newStdCreditPoint == 10) || (newStdCreditPoint == 20) || (newStdCreditPoint == 30) || (newStdCreditPoint == 40) || (newStdCreditPoint == 50) || (newStdCreditPoint == 60) || (newStdCreditPoint == 70) || (newStdCreditPoint == 80) || (newStdCreditPoint == 90) || (newStdCreditPoint == 100) || (newStdCreditPoint == 110) || (newStdCreditPoint == 120) || (newStdCreditPoint == 130) || (newStdCreditPoint == 140) || (newStdCreditPoint == 150) || (newStdCreditPoint == 160) || (newStdCreditPoint == 170) || (newStdCreditPoint == 180) || (newStdCreditPoint == 190) || (newStdCreditPoint == 200) || (newStdCreditPoint == 210) || (newStdCreditPoint == 220) || (newStdCreditPoint == 230) || (newStdCreditPoint == 240))
{
stdRec[last-1].creditPoint = newStdCreditPoint;
}
else
{
cout << "Not a valid credit point. Valid credit points can range from 0-240 as multiples of 10.\n"
<< "Would you like to try again? Y/N\n";
cin >> optAgain11;
if (optAgain11 == 'N')
{
break;
}
}
}while (optAgain11 = 'Y');
cout << "The new student has now been added to the record as below.\n";
cout << "\nStudent ID Student Name Course Code Credit Points\n\n";
cout << setiosflags(ios::left);
cout << setw(12) << stdRec[last-1].studentID
<< setw(21) << stdRec[last-1].studentName
<< setw(13) << stdRec[last-1].courseCode
<< setw(5) << stdRec[last-1].creditPoint << endl;
}
if((stdIDInput > "0") && (stdIDInput < "15000000"))
{
stdRec[last+1].studentID = stdRec[last].studentID;
stdRec[last+1].studentName = stdRec[last].studentName;
stdRec[last+1].courseCode = stdRec[last].courseCode;
stdRec[last+1].creditPoint = stdRec[last].creditPoint;
stdRec[last].studentID = stdRec[last-1].studentID;
stdRec[last].studentName = stdRec[last-1].studentName;
stdRec[last].courseCode = stdRec[last-1].courseCode;
stdRec[last].creditPoint = stdRec[last-1].creditPoint;
stdRec[last-1].studentID = stdRec[last-2].studentID;
stdRec[last-1].studentName = stdRec[last-2].studentName;
stdRec[last-1].courseCode = stdRec[last-2].courseCode;
stdRec[last-1].creditPoint = stdRec[last-2].creditPoint;
stdRec[last-2].studentID = stdIDInput;
cout << "Please input the new student's name.\n";
getline (cin, newStdName);
stdRec[last-2].studentName = newStdName;
do
{
cout << "\n\nPlease input the new student's course code (the SCM offers courses 3506, 3633, 3634 \nand 3639).\n";
cin >> newStdCourseCode;
if ((newStdCourseCode == 3506) || (newStdCourseCode == 3633) || (newStdCourseCode == 3634) || (newStdCourseCode == 3639))
{
stdRec[last-2].courseCode = newStdCourseCode;
}
else
{
cout << "Not a valid course code. SCM only offers courses 3506, 3633, 3634 or 3639.\n"
<< "Would you like to try again? Y/N";
cin >> optAgain8;
if(optAgain8 == 'N')
{
break;
}
}
}while (optAgain8 = 'Y');
do
{
cout << "Please input the new student's credit points. Valid credit points can range from 0-240 as multiples of 10.\n";
cin >> newStdCreditPoint;
if ((newStdCreditPoint == 0) || (newStdCreditPoint == 10) || (newStdCreditPoint == 20) || (newStdCreditPoint == 30) || (newStdCreditPoint == 40) || (newStdCreditPoint == 50) || (newStdCreditPoint == 60) || (newStdCreditPoint == 70) || (newStdCreditPoint == 80) || (newStdCreditPoint == 90) || (newStdCreditPoint == 100) || (newStdCreditPoint == 110) || (newStdCreditPoint == 120) || (newStdCreditPoint == 130) || (newStdCreditPoint == 140) || (newStdCreditPoint == 150) || (newStdCreditPoint == 160) || (newStdCreditPoint == 170) || (newStdCreditPoint == 180) || (newStdCreditPoint == 190) || (newStdCreditPoint == 200) || (newStdCreditPoint == 210) || (newStdCreditPoint == 220) || (newStdCreditPoint == 230) || (newStdCreditPoint == 240))
{
stdRec[last-2].creditPoint = newStdCreditPoint;
}
else
{
cout << "Not a valid credit point. Valid credit points can range from 0-240 as multiples of 10.\n"
<< "Would you like to try again? Y/N\n";
cin >> optAgain12;
if (optAgain12 == 'N')
{
break;
}
}
}while (optAgain12 = 'Y');
}
else
{
cout << "Not a valid student ID. It cannot be less than zero and more than eight digits long.\n";
}
}
}
cout << "Would you like to search and/or add another student record? Y/N\n";
cin >> option2;
if(option2 == 'N')
{
break;
}
}while (option2 == 'Y');
pause ();
break;
}
And and all direction would be awesome! Please and thanks!