Using struct and linear search to display student record

May 22, 2009 at 12:19pm
Okay, I've spent the last 4 hours trying to figure this out and all I get is constant error after error. I have gone through numerous forums, websites, textbooks and yet still have not found any solutions.

All i want my program to do is to allow the user to search for a student record (that he/she has entered). The result will then display the record of the search (Grade/Mark).

Here is my code:
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
#include <iostream>
#include <string>
using namespace std;

struct StudentRecord
{
       string studentID;
       float studentMark;
       char studentGrade;
} student;
int i;
int totalStudents;
int linearSearch (string[],int,string);


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;
                if(linearSearch(student[].studentID,totalStudents,id))
                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 searchlist(string id [],int total,string value)

{
    int i = -1;
    while (++i < totalStudents)
    if (student[].studentID == value) return i;
    else
    return -1;
}



And here are my errors:
- In function `int main()':
- 58 expected primary-expression before ']' token
- 59 no match for 'operator[]' in 'student[i]'
- In function `int searchlist(std::string*, int, std::string)':
- 86 expected primary-expression before ']' token


SOMEBODY PLEASE HELP ME!!!
May 22, 2009 at 4:08pm
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
#include <iostream>
#include <string>
using namespace std;

struct StudentRecord
{
       string studentID;
       float studentMark;
       char studentGrade;
} student;
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;
                if(linearSearch(student, id))
                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;
}


Last edited on May 22, 2009 at 4:09pm
May 22, 2009 at 8:15pm
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 . . .
May 23, 2009 at 12:37am
Thank you very much guys for your help.

I have adjusted the codes in my program however I have noticed that there is a minor error in Denis' coding. I have tried fixing it but without success. The error is that whatever student ID I enter, it always display "P1001". Even if i type in some random ID such as "fgdfgfd" it still displays studentID P1001.

Anybody have any suggestions?
Last edited on May 23, 2009 at 12:40am
Topic archived. No new replies allowed.