• 0

Whats wrong with this Linear Search ?


Question

hi, I am doing a linear search in my C++ program. Bascially i have a list of students and i should be able to search by student name. I did write a Linear Search code, as below. I am not able to run it correctly !!! Any help would be appreciated...

Thanks in Advance...

// Variables

int read_array (string StudentNameArray[],int StudentId[],float StudentGpa[]);

void sort_array (string StudentNameArray[], int StudentId[], float StudentGpa[]);

/*******************************************************************

Searching Array

Linear search

********************************************************************/

int i;

string s;

for (i = 0 ; i != s ; i++) // Searching Using Student Name

{

{

if ( StudentNameArray == s)

pos = i

};

if (pos > -1);

{

cout << 'Find Student' << pos;

}

else

cout << 'Not Found' ;

}

Link to comment
Share on other sites

9 answers to this question

Recommended Posts

  • 0

You can still use the == operator to compare strings...

What are the actual errors you get? Post the compile errors.

For instance in the for loop, you have an extra curly bracket, are missing a semicolon and have an unnecessary semicolon at the end.

Link to comment
Share on other sites

  • 0
You can still use the == operator to compare strings...

You can? Since when? Or is this a C++ / string class quirk that overloads == to actually compare the strings, rather than simply checking the pointers are equal.

Link to comment
Share on other sites

  • 0
hi, I am doing a linear search in my C++ program. Bascially i have a list of students and i should be able to search by student name. I did write a Linear Search code, as below. I am not able to run it correctly !!! Any help would be appreciated...

Thanks in Advance...

// Variables

int read_array (string StudentNameArray[],int StudentId[],float StudentGpa[]);

void sort_array (string StudentNameArray[], int StudentId[], float StudentGpa[]);

/*******************************************************************

Searching Array

Linear search

********************************************************************/

int i;

string s;

for (i = 0 ; i != s ; i++) // Searching Using Student Name

{

{

if ( StudentNameArray == s)

pos = i

};

if (pos > -1);

{

cout << 'Find Student' << pos;

}

else

cout << 'Not Found' ;

}

I don't know C++ but the guys above say that indeed C++ overloads the == operator so I'll take their word.

	for (i = 0; i != s; i++)

This is wrong.

	for (i = 0; StudentNameArray[i] != s; i++)

This is more correct but still wrong.

You need to compare i to the array length so something like i != 19 if you have an array of 20 elements.

Link to comment
Share on other sites

  • 0
You can? Since when? Or is this a C++ / string class quirk that overloads == to actually compare the strings, rather than simply checking the pointers are equal.

For char[] or char * you need to use some sort of compare method (strncmp preferably) but std::string provides an equality operator.

Link to comment
Share on other sites

  • 0
For char[] or char * you need to use some sort of compare method (strncmp preferably) but std::string provides an equality operator.

Yeah thought it would be something like that. I'm done much more C programming than C++ so it really stood out :p

Link to comment
Share on other sites

  • 0

Here are the errors i am getting ...

2008\projects\students\student.cpp(61) : error C2562: 'main' : 'void' function returning a value

\

2008\projects\students\student.cpp(29) : see declaration of 'main'

2008\projects\students\student.cpp(64) : error C2059: syntax error : ']'

2008\projects\students\student.cpp(165) : error C2059: syntax error : 'for'

2008\projects\students\student.cpp(165) : error C2143: syntax error : missing ')' before ';'

2008\projects\students\student.cpp(165) : error C2143: syntax error : missing ';' before '!='

2008\projects\students\student.cpp(165) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

2008\projects\students\student.cpp(165) : error C2086: 'int i' : redefinition

2008\projects\students\student.cpp(162) : see declaration of 'i'

2008\projects\students\student.cpp(165) : error C2143: syntax error : missing ';'

before '++'

1>2008\projects\students\student.cpp(165) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

1>2008\projects\students\student.cpp(165) : error C2086: 'int i' : redefinition

1>2008\projects\students\student.cpp(162) : see declaration of 'i'

1>2008\projects\students\student.cpp(165) : error C2059: syntax error : ')'

1> 2008\projects\students\student.cpp(166) : error C2143: syntax error : missing ';' before '{'

2008\projects\students\student.cpp(166) : error C2447: '{' : missing function header (old-style formal list?)

1>2008\projects\students\student.cpp(175) : error C2015: too many characters in constant

1>\projects\students\student.cpp(178) : error C2015: too many characters in constant

Link to comment
Share on other sites

  • 0

The posts above already fix some of those. The first one is due to you declaring main() as void, change it to int main(). Then just work your way down, correcting each of the syntax errors.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.