-1

So I am currently working on a project which requires me to take the words of the array from the user and then ask them to search for a word in the array. The program must show the position of the word in the array. Here's what I did:

#include <iostream.h>
#include <conio.h>

main()
{
 char studentsList[30][20];
 int size, flag = 0, pos;
 cout << "Enter the size of the array: ";
 cin >> size;
 cout << "Enter yhe names of the students: \n";
 for(int i = 0; i < size; i++)
 {
  cout << "Student no. " << i + 1 << ": ";
  cin >> studentsList[i];
 }
 for(int m = 0; m < size; m++)
 {
  cout << studentsList[m] << "\t";
 }
 char searchName[20];
 cout << "type the name you need to search: ";
 cin >> searchName;
 for(i = 0; i < size; i++)
 {
  if(studentsList[i] == searchName)
  {
   flag = 1;
   pos = i + 1;
  }
 }
 if(flag == 0)
  cout << "Term not found.";
 else
  cout << "Term found at position " << pos;
 getch();
}

I am not able to catch what's wrong in the code. It always gives the output as 'Term not found.' Help will be appreciated!

  • 1
    This is not C++. It won't compile for several reasons. Your problem can be easily solved with a help of debugger when you fix your code so it can be compiled. – mpiatek Feb 27 '17 at 15:21
  • 4
    Please see [this post](http://stackoverflow.com/questions/8004237/how-do-i-properly-compare-strings-in-c) on how to compare C strings. Note that your life would be easier if you used `std::string`. – Rakete1111 Feb 27 '17 at 15:22
  • Neither of the header files you use are part of Standard C++. Wherever you are learning this stuff from, learn it from somewhere else. –  Feb 27 '17 at 15:27
  • I feel like this exact question is asked once every day? daja vu :D – xander Feb 27 '17 at 15:27
  • Offtopic: 1) the `main` function returns `int`. **Always**. 2) `iostream.h` has been converted to `iostream`, without the extension, *a long time ago*. – Thomas Matthews Feb 27 '17 at 15:29
  • The `cin >> searchname` has potential to overflow the character array. Use `std::getline` instead. – Thomas Matthews Feb 27 '17 at 15:32
  • Thank you so much for your inputs, I'm currently using Turbo C7 and it compiles just fine, but something is missing causing program failure. – Mohil Khare Feb 27 '17 at 15:32
  • Simply stated, `studentsList[i] == searchName` is **comparing pointers** not text. Use `strncmp`. – Thomas Matthews Feb 27 '17 at 15:33
  • @MohilKhare [here](http://cpp.sh/8tw2) you can find it rewritten. Please uninstall that Turbo thing, use [one of the recommended compilers](https://isocpp.org/get-started) and start [learning C++](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – mpiatek Feb 27 '17 at 16:06

1 Answers1

-2

I think the problem is on the way you are using equality check.

it is in if(studentsList[i] == searchName)

The == is comparing the memory address. Instead, since you are using char[] it is better to make a utility function to check the equality char by char.

bool arrcmp( unsigned char arr1[], unsigned char arr2[] ) {
    int i = 0;
    int match = 0;

    if(strlen(arr1) != strlen(arr2)) {
        return false;
    }
    for( i=0; i < strlen(arr1); ++i ) {
        if( arr1[i] != arr2[i] ){
            match = 1;
            break;
        }
    }
    return match==0;
}
Simon K. Gerges
  • 3,097
  • 36
  • 34
  • This won't compile. You probably meant `bool arrcmp`. However don't reinvent the wheel - he can use `std::strncmp` or `std::string` & `std::find` – mpiatek Feb 27 '17 at 17:01
  • Also your implementation is purely wrong - what happens when strings are shorter than 8? What happens if they are longer but difference starts at 9th character? – mpiatek Feb 27 '17 at 17:07
  • Thanks for your comments, just fixed it, sorry I was writing fast :) – Simon K. Gerges Feb 27 '17 at 19:04