Take a look at this minor adaptation. It uses a functor and std::find_if to create a process for finding students by their record. The program will probably evolve into using dynamic arrays so eventually you'd need to change the call to std::find_if accordingly. The (student + 2) represents a pointer to one past the end of the array used by std::find_if to stop looking and will return a value equal to (student + 2) if it didn't find the value. It looks to me like it would be a good idea to use a std::vector to contain the records eventually as well as a few of the commonly used std::algorithms to perform common tasks.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
struct StudentRecord
{
string studentID;
float studentMark;
char studentGrade;
} student;
// I created a functor that allows you to specify an id to search for.
// it can be used with the std::find_if algorithm
struct FindStudentIdFtr
{
// Allow the functor to be constructed with a value
FindStudentIdFtr(std::string& id) : id_(id) { }
//
bool operator()(StudentRecord& theStudent)
{
return (theStudent.studentID == id_);
}
const std::string id_;
};
int i;
int totalStudents;
int linearSearch (StudentRecord* students, const string studentID);
int main()
{
int menu;
StudentRecord student[] = {
{"P1001",78.50,'D'},
{"P1002",66,'C'}
};
int totalStudents=0;
float totalMarks=0;
float averageMark;
string id;
int mark;
int j;
cout << "MAIN MENU\n"
<< "0. Exit 1. Statistics\n"
<< "2. Enter mark 3. Find mark\n"
<< "------------------------------\n"
<< "Your choice -> ";
cin >> menu;
while (menu != 0)
{
switch (menu)
{
case 1:
for(int j=0;j<totalStudents;j++)
{
totalMarks+=student[j].studentMark;
totalStudents++;
}
averageMark=totalMarks/totalStudents;
cout << "Number of records: " << totalStudents <<endl;
cout << "Mean or average: " << averageMark <<endl;
break;
case 2:
cout << "Enter a Student Record: " <<endl;
cout << "Student ID -> ";
cin >> id;
break;
case 3:
cout << "Find marks for ID -> ";
cin >> id;
// use std::find_If and there is no reason to write your own function or
// or for loop. Just be careful that as your upgrade the program that you
// will eventually be using a dynamic array if you plan to allow building
// student records
if(std::find_if(student, student + 2, FindStudentIdFtr(id)) != student + 2)
cout << "Student ID: " << student[i].studentID << endl;
else
cout << "Record does not exist";
break;
default:
cout << "Invalid selection. Please make a selection between 0-3.\n"
<< endl;
break;
}
system("Pause");
cout << "MAIN MENU\n"
<< "0. Exit 1. Statistics\n"
<< "2. Enter mark 3. Find mark\n"
<< "----------------------------\n"
<< "Your choice -> ";
cin >> menu;
}
return 0;
}
int linearSearch (StudentRecord* students, const string studentID)
{
int retValue(-1);
for(int i = 0; i < totalStudents; ++i)
{
if(students[i].studentID == studentID)
{
retValue = i;
break;
}
}
return retValue;
}
|
Here is a record of the output:
MAIN MENU
0. Exit 1. Statistics
2. Enter mark 3. Find mark
------------------------------
Your choice -> 3
Find marks for ID -> P1002
Student ID: P1001
Press any key to continue . . .
MAIN MENU
0. Exit 1. Statistics
2. Enter mark 3. Find mark
----------------------------
Your choice -> 3
Find marks for ID -> P1001
Student ID: P1001
Press any key to continue . . .
MAIN MENU
0. Exit 1. Statistics
2. Enter mark 3. Find mark
----------------------------
Your choice -> 3
Find marks for ID -> P1005
Record does not existPress any key to continue . . .