Linear Search not working properly
May 23, 2009 at 4:19am UTC
I have programmed my linear search to find/display student records. However, I have noticed that there is an error in my program. The problem is that whatever value I enter for the student ID, it will always display "record does not exist". Even if I type in an valid id (such as "P1001"), it still displays "record does not exist".
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
#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=2;
float totalMarks=0;
float averageMark;
string id;
int mark;
int j;
int found = linearSearch(student,id);
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;
}
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;
cout << "Student ID already exists" << endl;
break ;
case 3:
cout << "Find marks for ID -> " ;
cin >> id;
if (found!=-1)
{
cout << "-------------" << endl;
cout << "Student ID: " << student[found].studentID << endl;
cout << "Mark/Grade: " << student[found].studentMark << " (" << student[found].studentGrade << ")" << endl;
cout << "-------------" << endl;
}
else
cout << "Record does not exist" << endl;
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;
}
Anybody have any suggestions?
May 23, 2009 at 5:45am UTC
You call linearSearch() on line 29 but input id on line 59.
May 23, 2009 at 8:02am UTC
I have re-arranged the coding as follows:
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
#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 id);
int main()
{
int menu;
StudentRecord student[] = {
{"P1001" ,78.50,'D' },
{"P1002" ,66,'C' }
};
int totalStudents=2;
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;
}
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;
cout << "Student ID already exists" << endl;
break ;
case 3:
cout << "Find marks for ID -> " ;
cin >> id;
int found = linearSearch(student,id);
if (found!=-1)
{
cout << "-------------" << endl;
cout << "Student ID: " << student[found].studentID << endl;
cout << "Mark/Grade: " << student[found].studentMark << " (" << student[found].studentGrade << ")" << endl;
cout << "-------------" << endl;
}
else
cout << "Record does not exist" << endl;
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 id)
{
int retValue(-1);
for (int i=0;i<totalStudents;++i)
{
if (students[i].studentID == id)
{
retValue=i;
break ;
}
}
return retValue;
}
But i still get the following errors:
- In function `int main()':
- 71 jump to case label
- 59 crosses initialization of `int found'
How can I fix these errors?
May 23, 2009 at 3:27pm UTC
That's what happens when you declare variables inside a switch case. When you need to do that, just surround the case with {}.
For example:
1 2 3 4 5
case 1:
{
int a=0;
}
break ;
Topic archived. No new replies allowed.