Hello guys, I've tried searching the internet on how to cout a struct in an array via a binary search, but with no success. I've only learned how to do a linear search and I sort of understand how to do a binary search but: how do I cout the found student record/ results of the search? Any help at all would awesome! I'm not so good with making functions but, my current 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:
        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:
        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:
        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:
        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:
        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:
        int first = 0;
        int last = NUMRECS;
        int mid = (first + last)/2;
        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;
        while (first <= last && !found)
        {
            if (stdRec[mid] == stdIDInput)
            {
                found = true;
                cout << "Blub lub lub";
            }
        }    
        pause ();
        break;
    case 7:
        char decision2, choice2;
        int 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;
    default:
      cout << "No options from choices one to seven have been typed.";
      pause ();
      break;
    }
    
  } while (option!=0);
  return 0;
}

but just case 6 where I'm stuck at the moment:

case 6:
        int first = 0;
        int last = NUMRECS;
        int mid = (first + last)/2;
        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;
        while (first <= last && !found)
        {
            if (stdRec[mid] == stdIDInput)
            {
                found = true;
                cout << "Blub lub lub";
            }
        }    
        pause ();
        break;

Recommended Answers

All 22 Replies

Ok. You have my attention. And I would love to help, but before we get there, please sprinkle some liberal comments on you code so I know what everything you are trying to do. Comments are very important to people looking at your code, who didnt have a hand in writing it. Also, can you clean it up a little bit, ive gotten the following errors compiling:

bs.cxx: In function 'int main()':
bs.cxx:235:38: error: no match for 'operator==' in 'stdRec[mid]
bs.cxx:243:18: error: jump to case label
bs.cxx:232:21: error: crosses initialization of 'bool found'
bs.cxx:227:20: error: crosses initialization of 'int mid'
bs.cxx:226:20: error: crosses initialization of 'int last'
bs.cxx:225:20: error: crosses initialization of 'int first'
bs.cxx:285:13: error: jump to case label
bs.cxx:245:20: error: crosses initialization of 'int studentCo
bs.cxx:232:21: error: crosses initialization of 'bool found'
bs.cxx:227:20: error: crosses initialization of 'int mid'
bs.cxx:226:20: error: crosses initialization of 'int last'
bs.cxx:225:20: error: crosses initialization of 'int first'

Fix/Remove whatever is causing those problem, comment some stuff, and well go from there.

Ok. You have my attention. And I would love to help, but before we get there, please sprinkle some liberal comments on you code so I know what everything you are trying to do. Comments are very important to people looking at your code, who didnt have a hand in writing it. Also, can you clean it up a little bit, ive gotten the following errors compiling:

bs.cxx: In function 'int main()':
bs.cxx:235:38: error: no match for 'operator==' in 'stdRec[mid]
bs.cxx:243:18: error: jump to case label
bs.cxx:232:21: error: crosses initialization of 'bool found'
bs.cxx:227:20: error: crosses initialization of 'int mid'
bs.cxx:226:20: error: crosses initialization of 'int last'
bs.cxx:225:20: error: crosses initialization of 'int first'
bs.cxx:285:13: error: jump to case label
bs.cxx:245:20: error: crosses initialization of 'int studentCo
bs.cxx:232:21: error: crosses initialization of 'bool found'
bs.cxx:227:20: error: crosses initialization of 'int mid'
bs.cxx:226:20: error: crosses initialization of 'int last'
bs.cxx:225:20: error: crosses initialization of 'int first'

Fix/Remove whatever is causing those problem, comment some stuff, and well go from there.

Well, that's just it! I don't know how to fix the problems, but it's just all in case 6 that the problems arise! Here's my code with case 6 removed (it compiles and runs when I remove case 6) and I've added more comments so you know what else is going on, but case 6 is the only one I'm having trouble with, everything else works fine(although I haven't tested case 7's functionality yet, it's still on trial):

#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}}; // The data I'm using
  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 student ID
        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 via their 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: // this lists all 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://this displays 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://this allows for the user to alter a students' 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;
//** insert case 6 here
    case 7: //this is supposed to delete a students' 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;            
                    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;
}

but again, here's just my current case 6 which chucks up a bunch of errors:

case 6: //I'm trying to use a binary search to see if their is a match with the current stored data and the user's input, then I'd like to be able to add the student record of the student that the user has input, but straightening up the binary search comes first!
        int first = 0;
        int last = NUMRECS;
        int mid = (first + last)/2;
        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;
        while (first <= last && !found)
        {
            if (stdRec[mid] == stdIDInput)
            {
                found = true;
                cout << "Blub lub lub";
            }
        }    
        pause ();
        break;

Oh, quick look I realized something. You need to use brackets {} within your case statements to delimit the scope of the variables which are declared and initialized to the scope of those case statements only. That should chop down some errors.
ex.
case 6:
{


break;
}

It will then give you a single error,
test.cxx: In function 'int main()':
test.cxx:236:24: error: no match for 'operator==' in 'stdRec[mid] == stdIDInput'

This is seeking equality between stdRec[mid], which is a structure, in this case {"16666666", "Jack Williams", 3506, 180}, and stdIDInput, which is string. Depending on what you are trying to do with this, I dont see that operator working that way.

What is it you were intending to compare exactly?

on second thought, enclose all of you switch statements within brackets.

Oh, quick look I realized something. You need to use brackets {} within your case statements to delimit the scope of the variables which are declared and initialized to the scope of those case statements only. That should chop down some errors.
ex.
case 6:
{


break;
}

It will then give you a single error,
test.cxx: In function 'int main()':
test.cxx:236:24: error: no match for 'operator==' in 'stdRec[mid] == stdIDInput'

This is seeking equality between stdRec[mid], which is a structure, in this case {"16666666", "Jack Williams", 3506, 180}, and stdIDInput, which is string. Depending on what you are trying to do with this, I dont see that operator working that way.

What is it you were intending to compare exactly?

erm well I was trying to code so that the user had the option to add a new student record (student ID, student name, course code, credit points), but before that, I needed to use a binary search to see whether the student ID the user inputs already exists in the array of structs! Would it be fixed if I changed things around a bit? I've kind of been working on it and would this work?

case 6:
        int first = 0;
        int last = NUMRECS;
        int mid = (first + last)/2;
        int studentCount, y;
        studentCount = NUMRECS;
        char decision3;
        
        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)
        {
            if (stdRec[mid].studentID == stdIDInput)
            {
                found = true;
                cout << "\nStudent ID  Student Name         Course Code  Credit Points\n\n";
                cout << setiosflags(ios::left);
                cout << "This student ID already exists in the system.\n"
                     << stdRec[mid] << endl;
            }
            else if (stdRec[mid].studentID > stdIDInput)
            {
                last = mid -1;
            }
            else
            {
                first = mid +1;
            }
        }
        if (!found)
        {
            do
            {
                cout << "Would you like to add the student record? Y/N\n";
                cin >> decision3;
                studentCount++
                //insert loop to shift all records one up here
            }while(decision3 ! = 'N')
        }
        pause ();
        break;

While compiler error messages are a bit terse, they generally tell you where the compiler found a problem. In this case:

test.cxx:236:24: error: no match for 'operator==' in 'stdRec[mid] == stdIDInput'

says that the error was found at line 236, and possibly column 24. You need to compare the ID member of your record against the input value, the same as you've done in your binary-search code:

if (stdRec[mid].studentID == stdIDInput)

Also, sometimes the error is actually above the specified line (such as a missing ; at the end of the previous line of code, or a missing " or } some other terminator for a pair of enclosing symbols), the line in the error message is merely where the compiler couldn't figure out what to do next.

While compiler error messages are a bit terse, they generally tell you where the compiler found a problem. In this case:

says that the error was found at line 236, and possibly column 24. You need to compare the ID member of your record against the input value, the same as you've done in your binary-search code:

if (stdRec[mid].studentID == stdIDInput)

Also, sometimes the error is actually above the specified line (such as a missing ; at the end of the previous line of code, or a missing " or } some other terminator for a pair of enclosing symbols), the line in the error message is merely where the compiler couldn't figure out what to do next.

Well, the code compiles and runs with no error messages, I thought this code would work, but it still doesn't! The search section works fine, it's just when it doesn't find a match it just stops! What I'd like is that if a match to a student ID is not found, the user has the option to add new student information to the record. Any ideas? Relevant code:

case 6://add a student record
        {
        int first = 0;
        int last = NUMRECS;
        int mid = (first + last)/2;
        int studentCount, newStdCourseCode, newStdCreditPoint;
        studentCount = NUMRECS;
        char decision3, option2;
        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)//search for a student ID match
        {
            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)//***HERE I don't know why this doesn't work! Any suggestions???
        {
            cout << "Would you like to add the student to the records? Y/N\n";
            cin >> decision3;
            if(decision3 == 'Y')
            {
                last = NUMRECS;
                studentCount++;
                stdRec[last+1].studentID = stdIDInput;
                cout << "Please input the new student's name.\n";
                cin >> newStdName;
                stdRec[last+1].studentName = newStdName;
                cout << "Please input the new student's course code.\n";
                cin >> newStdCourseCode;
                stdRec[last+1].courseCode = newStdCourseCode;
                cout << "Please input the new student's credit points.\n";
                cin >> newStdCreditPoint;
                stdRec[last+1].creditPoint = newStdCreditPoint;
                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;
             }
        }
        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;
        }

Aha! When you recompute first or last, make sure you update mid at the same time (after lines 30 and 34, respectively)!

When adding a new student record, make sure that you've allocated enough records in your array. You need to separately keep track of MAXRECS (the total number of records allocated in your array) and NUMRECS (the number you're currently using). If NUMRECS == MAXRECS, then you have no additional space and can't add a new student. If this is something you need to address for this assignment, we can discuss dynamic memory allocation and/or using a std::vector<> instead of a static array. Otherwise, just print an error message to the user. Also, it doesn't do much good to assign studentCount from NUMRECS, and then not eventually increment NUMRECS. One variable for this purpose is sufficient.

Aha! When you recompute first or last, make sure you update mid at the same time (after lines 30 and 34, respectively)!

When adding a new student record, make sure that you've allocated enough records in your array. You need to separately keep track of MAXRECS (the total number of records allocated in your array) and NUMRECS (the number you're currently using). If NUMRECS == MAXRECS, then you have no additional space and can't add a new student. If this is something you need to address for this assignment, we can discuss dynamic memory allocation and/or using a std::vector<> instead of a static array. Otherwise, just print an error message to the user. Also, it doesn't do much good to assign studentCount from NUMRECS, and then not eventually increment NUMRECS. One variable for this purpose is sufficient.

Ahhh... so if last = mid+1 or if first = mid+1, mid doesn't = (first + last)/2 anymore? Or do I have to restate that after lines 30 and 34? Also, the search does work and it returns the results of the search if a match is found, it's just when a match isn't found that everything just stops. Is it my

if(!found)

statement? I tried changing it to

if(found == false)

but its still does the same thing! And we haven't learnt about vectors or anything like that just quite yet...

You only set mid once, and that was outside of any loop, so it doesn't update when last or first changes.

You only set mid once, and that was outside of any loop, so it doesn't update when last or first changes.

Ahhhh, okay, thanks for clearing up my understanding!

You have to respecify mid. It should still be (first+last)/2, but its value doesn't change just because you go behind its back and change first and/or last, any more than if you had arbitrarily said mid = 5; ... once you assign a value to a variable, it keeps that value until you assign it a different value. So if first and last start out as, for example, 0 and 20 (if you have 21 students in your array), then mid starts out as (0+20)/2 = 10, but it stays at 10 until you recompute what the new midpoint is between the revised-first or revised-last.

As far as "just stops", it's actually getting stuck in an infinite loop at that point, because if it doesn't find it at the first value of mid, then first or last is assigned a new value, but mid doesn't change, so first & last also don't change after that point, and it keeps checking the original position of mid (which still doesn't match), and reassigning the same modified value to first or last, depending on which side it should check next.

You have to respecify mid. It should still be (first+last)/2, but its value doesn't change just because you go behind its back and change first and/or last, any more than if you had arbitrarily said mid = 5; ... once you assign a value to a variable, it keeps that value until you assign it a different value. So if first and last start out as, for example, 0 and 20 (if you have 21 students in your array), then mid starts out as (0+20)/2 = 10, but it stays at 10 until you recompute what the new midpoint is between the revised-first or revised-last.

As far as "just stops", it's actually getting stuck in an infinite loop at that point, because if it doesn't find it at the first value of mid, then first or last is assigned a new value, but mid doesn't change, so first & last also don't change after that point, and it keeps checking the original position of mid (which still doesn't match), and reassigning the same modified value to first or last, depending on which side it should check next.

Ohhhhh, well thanks for clearing that up! As far as my program goes, it crashes now, but only when I want to input a new student into the record... do you think you could tell me what I'm doing wrong?

case 6://adding 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)//see if there's a match with current student IDs
        {
            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 the program crashes, but 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 sorry if you had to wade through all the code, I couldn't think of a more succinct way to do it!

if (!found)
and
if (found == false)

are the same thing. the opposite would be

if(found) 
and
if(found == true)

.
Sorry, I havent been able to take a deep look into your code, busy with my own binary search tree projects o.O

Line 77 (and the other ones like it) could be replaced much more succinctly with:

if (newStdCreditPoint >= 0  &&  newStdCreditPoint <= 240  &&  newStdCreditPoint % 10 == 0)

The %-operator (called "modulo") returns the remainder left over after dividing the first number by the second. In this case the expression val % 10 == 0 says "val can be divided evenly by ten with nothing left over".

I suspect your crash might be happening at line 48 rather than line 42, but I'm not running your code, so I don't know. There's nothing wrong with the code where you've marked it. At line 48, I'm concerned that you might be trying to assign to an student-record past the end of your array.

Your logic for error-checking your input is bewildering. Instead of looping while the response is 'Y' and breaking if the answer is 'N' (which do the same thing, so only one would be necessary), loop while the input value is invalid (and still break if the user doesn't want to try again. Then use the validity to decide whether to prompt for the next value:

bool idValid = false;
bool courseCodeValid = false;
bool creditPointValid = false;
do {
    // prompt for id
    // get id from user
    if ( /* id is valid */ )
        idValid = true;
    else {
        // prompt for try-again
        // get try-again from user
        if ( /* try-again is not 'y' */ )
            break;
    }
} while (! idValid);

if (idValid) {
    // proceed to next thing to input
}

if (idValid && ... ) {
    ...
}

Line 77 (and the other ones like it) could be replaced much more succinctly with:

if (newStdCreditPoint >= 0  &&  newStdCreditPoint <= 240  &&  newStdCreditPoint % 10 == 0)

The %-operator (called "modulo") returns the remainder left over after dividing the first number by the second. In this case the expression val % 10 == 0 says "val can be divided evenly by ten with nothing left over".

I suspect your crash might be happening at line 48 rather than line 42, but I'm not running your code, so I don't know. There's nothing wrong with the code where you've marked it. At line 48, I'm concerned that you might be trying to assign to an student-record past the end of your array.

Your logic for error-checking your input is bewildering. Instead of looping while the response is 'Y' and breaking if the answer is 'N' (which do the same thing, so only one would be necessary), loop while the input value is invalid (and still break if the user doesn't want to try again. Then use the validity to decide whether to prompt for the next value:

bool idValid = false;
bool courseCodeValid = false;
bool creditPointValid = false;
do {
    // prompt for id
    // get id from user
    if ( /* id is valid */ )
        idValid = true;
    else {
        // prompt for try-again
        // get try-again from user
        if ( /* try-again is not 'y' */ )
            break;
    }
} while (! idValid);

if (idValid) {
    // proceed to next thing to input
}

if (idValid && ... ) {
    ...
}

Ah! That saves so much room! But... I don't know how to assign more room to the array...

For now, start with your array bigger than you need at first, and keep track of how big you made it, and how many valid records are actually in it. If you run out of room, print an error. Don't think of it as "adding records", think of it as "filling in the empty records that are already there", if that helps.

const int MAXRECS = 20;  // or however many you need
StudentRecord StdRec[MAXRECS];  // all empty at this point
int NUMRECS = 0;  // there are no filled records in the array

...

// user wants to add a record
if (NUMRECS == MAXRECS) {
    cout << "Sorry, there's no more room to add a new student record" << endl;
}
else {
    // find insertIndex where to add a new record so that the array stays sorted:
    // such that stdRec[insertIndex-1].studentID < stdIDInput < stdRec[insertIndex].studentID
    // move records with (ID > newID) up one position -- opposite of when you delete
    for (int i = NUMRECS-1;  i >= insertIndex;  i--)
        stdRec[i+1] = stdRec[i];
    // copy data into freed-up record
    stdRec[insertIndex].studentID = stdIDInput;
    ...
    // update current number of used records
    NUMRECS += 1;
}

If you then want to make your array bigger instead of printing an error, your array allocation should look something like this (note that MAXRECS is no longer "const"):

int MAXRECS = 20;
int NUMRECS = 0;
StudentRecord *stdRec = new StudentRecord[MAXRECS];

...

if (NUMRECS == MAXRECS) {
    // need more space
    int NEWMAXRECS = MAXRECS * 2;  // let's get twice as many
    // reserve bigger array of records
    StudentRecord *newStdRec = new StudentRecord[NEWMAXRECS];
    // copy existing records into new space
    for (int i = 0;  i < NUMRECS;  i++)
       newStdRec[i] = stdRec[i];
    // delete old records-space now that we're done with it
    delete [] stdRec;
    // point the original name to the new array, and update the size
    stdRec = newStdRec;
    MAXRECS = NEWMAXRECS;
}

Note that you can treat a pointer-to-a-record the same as an array-of-records, as long as you've allocated the space for the array. In fact, in C and C++, if you declare SomeType arrayVar[someSize]; then arrayVar is identical to &arrayVar[0] , the pointer to the beginning (zero'th) element in the array.

If you then want to make your array bigger instead of printing an error, your array allocation should look something like this (note that MAXRECS is no longer "const"):

int MAXRECS = 20;
int NUMRECS = 0;
StudentRecord *stdRec = new StudentRecord[MAXRECS];

...

if (NUMRECS == MAXRECS) {
    // need more space
    int NEWMAXRECS = MAXRECS * 2;  // let's get twice as many
    // reserve bigger array of records
    StudentRecord *newStdRec = new StudentRecord[NEWMAXRECS];
    // copy existing records into new space
    for (int i = 0;  i < NUMRECS;  i++)
       newStdRec[i] = stdRec[i];
    // delete old records-space now that we're done with it
    delete [] stdRec;
    // point the original name to the new array, and update the size
    stdRec = newStdRec;
    MAXRECS = NEWMAXRECS;
}

Note that you can treat a pointer-to-a-record the same as an array-of-records, as long as you've allocated the space for the array. In fact, in C and C++, if you declare SomeType arrayVar[someSize]; then arrayVar is identical to &arrayVar[0] , the pointer to the beginning (zero'th) element in the array.

Uhhh... I think I kind of get it... although we haven't learned pointers yet so... sorry for being annoying with the questions, but could you please explain it simpler- if possible? Thanks so much for your help so far!

For now, start with your array bigger than you need at first, and keep track of how big you made it, and how many valid records are actually in it. If you run out of room, print an error. Don't think of it as "adding records", think of it as "filling in the empty records that are already there", if that helps.

const int MAXRECS = 20;  // or however many you need
StudentRecord StdRec[MAXRECS];  // all empty at this point
int NUMRECS = 0;  // there are no filled records in the array

...

// user wants to add a record
if (NUMRECS == MAXRECS) {
    cout << "Sorry, there's no more room to add a new student record" << endl;
}
else {
    // find insertIndex where to add a new record so that the array stays sorted:
    // such that stdRec[insertIndex-1].studentID < stdIDInput < stdRec[insertIndex].studentID
    // move records with (ID > newID) up one position -- opposite of when you delete
    for (int i = NUMRECS-1;  i >= insertIndex;  i--)
        stdRec[i+1] = stdRec[i];
    // copy data into freed-up record
    stdRec[insertIndex].studentID = stdIDInput;
    ...
    // update current number of used records
    NUMRECS += 1;
}

Ahhhhh!!! This is what I needed! I knew I could do the opposite of how I deleted records, but I didn't know how to do the loop! I understand everything except how to get insertIndex... that's pretty much it... but would I be able to find it by using the relational operators < and > like in my original code? Like if stdIDInput > 0 && < 15000000, insertIndex = 0? or 1?

Same way you do the binary search in other places, or (for case 6) since you've already done the search, you should know about where you are.

insertIndex is the value of i where (StdRec[i-1] < stdIDInput && stdIDInput < StdRec[i]) . Keep in mind the edge conditions stdIDInput < StdRec[0] (new record goes at the beginning, there's no StdRec[-1]) and stdIDInput > StdRec[NUMRECS-1] (new record goes at the end, there's no StdRec[NUMRECS]). Do your binary search by hand on a set of 3 records and various new values for stdIDInput. Or add a debug cout << ... line after your while() loop. What are the values of first & last at the end of the while() loop, when you've failed to find the new ID? inputIndex will be one of these values, or very close to it.

Same way you do the binary search in other places, or (for case 6) since you've already done the search, you should know about where you are.

insertIndex is the value of i where (StdRec[i-1] < stdIDInput && stdIDInput < StdRec[i]) . Keep in mind the edge conditions stdIDInput < StdRec[0] (new record goes at the beginning, there's no StdRec[-1]) and stdIDInput > StdRec[NUMRECS-1] (new record goes at the end, there's no StdRec[NUMRECS]). Do your binary search by hand on a set of 3 records and various new values for stdIDInput. Or add a debug cout << ... line after your while() loop. What are the values of first & last at the end of the while() loop, when you've failed to find the new ID? inputIndex will be one of these values, or very close to it.

Ahaha, thanks for tolerating my incompetence up to this point! I've changed my case 6 on the advice of my lecturer and it *sort of* works... it's just when the user tries to input the new student's name, the prompt is there, but the part where the program waits for input is totally skipped over. I know cin doesn't take spaces as input, so I used getline(), but this problem is all I have left to contend with! Here's the relevant code:

case 6: // adding a student record
        {
        int first = 0;
        int last = NUMRECS-1;
        int mid;
        int newStdCourseCode, newStdCreditPoint, insertIndex;
        char decision3, option2, optAgaina, optAgainb;
        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) // searches for 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')
            {
            
                insertIndex = 0;
                while (stdRec[insertIndex].studentID < stdIDInput)//finds where the input ID should go
                {
                    insertIndex++;
                }
                for (i = NUMRECS-1; i >= insertIndex; i--)//shuffles all the above IDs up one to make room
                {
                    stdRec[i+1] = stdRec[i];
                }
                stdRec[insertIndex].studentID = stdIDInput; // inserts new student info
                cout << "Please input the new student's name.\n";
                getline (cin,newStdName); // ***HERE! The program doesn't wait and skips over this part :(
                stdRec[insertIndex].studentName = newStdName;
                do
                {
                    cout << "Please input the new student's course code\n(the SCM offers courses 3506, 3633, 3634 and 3639).\n";
                    cin >> newStdCourseCode;
                    if ((newStdCourseCode == 3506) || (newStdCourseCode == 3633) || (newStdCourseCode == 3634) || (newStdCourseCode == 3639)) 
                    {
                        stdRec[insertIndex].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 >> optAgaina;
                        if(optAgaina == 'N')
                        {
                            break;
                        }
                    }
                }while (optAgaina = '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 <= 240)  &&  (newStdCreditPoint % 10 == 0))
                    {
                        stdRec[insertIndex].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 >> optAgainb;
                        if (optAgainb == 'N')
                        {
                            break;
                        }
                    }
                }while (optAgainb = 'Y');
            NUMRECS += 1;  
            }
        }
        cout << "The new student record has now been added as below:\n\n";
        cout << "\nStudent ID  Student Name         Course Code  Credit Points\n\n";
        cout << setiosflags(ios::left);
        cout << setw(12) << stdRec[insertIndex].studentID
             << setw(21) << stdRec[insertIndex].studentName
             << setw(13) << stdRec[insertIndex].courseCode
             << setw(5)  << stdRec[insertIndex].creditPoint << endl;
        cout << "\nWould you like to search and/or add another student record? Y/N\n";
        cin >> option2;
        if(option2 == 'N')
        {
            break;
        }
        }while (option2 == 'Y');
        
        pause ();
        break;
    }

If I get this one little bug fixed- it is done! I've searched on google, but couldn't find any problems similar to this one... any suggestions would be greatly appreciated! Please and thank-you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.