Hey guys.
I am creating a small type and search program and it compiles ok. If I enter the correct word it displays the character string fine then terminates. However, if I enter an incorrect word, it freezes and throws a seg-fault. After using the debugger, the error line is the if statement header. Here is the code:
Code:
#include <iostream>
#include <cstring>
// main function - driver //////////////////////////////////////////////////////
//
int main ( void )
{
int i;
char *pSearchGrid[][ 2 ] =
{
"pencil", "A writing device",
"amelle", "One of the Sugababes" ,
"computer", "Hardware device that uses software" ,
"phone", "Device that makes and recieves telephone calls" ,
"picture", "Drawn or computerized image",
"", ""
};
char word[ 80 ];
std::cout << "Enter a word to find: ";
std::cin >> word;
for ( i = 0; pSearchGrid[ i ][ 0 ]; i++ )
{
if ( !strcmp ( pSearchGrid[ i ][ 0 ], word ))
{
std::cout << std::endl << pSearchGrid[ i ][ 1 ] << std::endl;
break;
}
}
if ( !*pSearchGrid[ i ][ 0 ] ) // SEG FAULT DEBUG BREAK HERE
{
std::cout << word << " not found...\n";
}
std::cin.get(); // freeze console output window
std::cin.ignore();
return 0; // return value from int main
}
I have looked over it and cannot see why it keeps breaking here. Can you see what I have done wrong? I appreciate any help.