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;