Linear Search

I need help with an assignment. For one of the question it states "When Find mark is selected, the user will enter a student ID and the program will then locate the mark corresponding to the given ID and display the record". The record consists displaying the student ID, the corresponding mark, and the grade. I have tried coding this but I just keep getting error messages when compiling it.

Some of the errors I get include the following:
- "cannot convert 'std::string' to 'int' for argument '1' to 'int linearSearch'"
- "ISO C++ forbids comparison between pointer and integer"
And the list goes on..

Heres what my coding looks like at the moment:

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
#include <iostream>
#include <string>
using namespace std;

int linearSearch(int [], int [], int);

int main()
{
    int menu;
    string studentID[] = {"P1001", "P1002"};
    float studentMark[] = {78.50, 66};
    int totalStudents=0;
    float totalMarks=0;
    float averageMark;
    string id;
    int mark;
    char studentGrade;
    
    if (studentMark >= 85)
       studentGrade = 'H';
    else if (studentMark < 85 && >= 75)
       studentGrade = 'D';
    else if (studentMark < 75 && >= 65)
       studentGrade = 'C';
    else if (studentMark <65 && >= 50)
       studentGrade = 'P';
    else
       studentGrade = 'F';
  
    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<2;j++)
                 {
                   totalMarks+=studentMark[j];
                   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 -> " << endl;
                cin >> id;
                mark=linearSearch(studentID, studentMark, id);
                if (mark>-1)
                   cout << "Student ID: " << studentID <<endl;
                   cout << "Mark/Grade :" << studentMark << " (" << studentGrade << ")" << endl;
                else
                   cout << "Student 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;
}


It would be of great appreciation if somebody could help me please.
Last edited on
Is ID supposed to be a number? And if so, why is studentID an std::string (line 15)?
first you declared studentMark as an 2 element array. studentMark[0] has a value of 78.5 studentMark[1] has a value of 66. If you want to use studentMark in any thing you must state what element you want, except passing the whole array to a function.


1
2
3
4
5
6
7
8
9
10
    if (studentMark[0] >= 85)
       studentGrade = 'H';
    else if (studentMark[0] < 85 && studentMark[0] >= 75)
       studentGrade = 'D';
    else if (studentMark[0] < 75 && studentMark[0] >= 65)
       studentGrade = 'C';
    else if (studentMark[0] <65 && studentMark[0] >= 55)
       studentGrade = 'P';
    else
       studentGrade = 'F';


changing this will give you less errors. there are a few more.
Last edited on
ID is the student ID that the user enters to display the mark. It consists of both an int and char.
E.g. "P1001".

What is the main problem that is causing the error and how can i fix it??
Last edited on
Topic archived. No new replies allowed.