Hi all,

When the user enters a word, isKnownWord() checks whether the word is in the dictionary but even when I enter a word which I know is in the dictionary, into the terminal, it always outputs no! Can somebody please explain why it is doing this?

isKnownWord in sc class

bool isKnownWord(char* aWord) {
    struct wordRecord* wordRecPtr = wordList;
    bool found=false;
    while (!found && wordRecPtr) {
      if (strcmp(aWord, wordRecPtr->word)==0) found=true;
      else wordRecPtr=wordRecPtr->next;
    }
    return found;
  };

main method snippet:

char* words[] = {(char*)"hello", (char*)"Womble", (char*)"goodbye"};

  char inputWord[80];
  
  caseSensitiveSpellChecker sc;
  //caseSensitiveSpellChecker * sc = new caseSensitiveSpellChecker();
   
  for (int i=0; i<sizeof(words)/sizeof(char*); i++)  sc.addWord(words[i]);

  printf("Enter word ('END' to end):  ");
  scanf("%s", inputWord);

  while (strcmp(inputWord, "END")) {
    if (sc.isKnownWord(inputWord)) printf("yes\n");
    else printf("no\n");
    printf("Enter word ('END' to end):  ");
    scanf("%s", inputWord);
    
  }

Aphrodite

Use a debugger

Put a breakpoint at the start of isKnownWord

Single step the code to follow the path it is actually taking.
When it goes somewhere unexpected, you've found a bug.

Use a debugger

Put a breakpoint at the start of isKnownWord

Single step the code to follow the path it is actually taking.
When it goes somewhere unexpected, you've found a bug.

I'm using gedit on fedora so it doesn't actually allow me to put breaks in but I have put in a printf output to ensure that it is entering the isKnownWord() method, which it does. So it must then definitely be the while statement:

while (!found && wordRecPtr) {
bool found=false;
    [B]  if (strcmp(aWord, wordRecPtr->word)==0) found=true;[/B]
      else wordRecPtr=wordRecPtr->next;
    }

But can't see what is wrong with the bold line!

What are you talking about - gedit is an editor, not a debugger.

You edit the code with the editor

You then compile it with say
gcc prog.c

You then debug it with
gdb a.out

$ gcc -g prog.c
$ gdb a.out
GNU gdb 6.8-debian
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i486-linux-gnu"...
(gdb) help
List of classes of commands:

aliases -- Aliases of other commands
breakpoints -- Making program stop at certain points
data -- Examining data
files -- Specifying and examining files
internals -- Maintenance commands
obscure -- Obscure features
running -- Running the program
stack -- Examining the stack
status -- Status inquiries
support -- Support facilities
tracepoints -- Tracing of program execution without stopping the program
user-defined -- User-defined commands

Type "help" followed by a class name for a list of commands in that class.
Type "help all" for the list of all commands.
Type "help" followed by command name for full documentation.
Type "apropos word" to search for commands related to "word".
Command name abbreviations are allowed if unambiguous.
(gdb) help breakpoints
Making program stop at certain points.

List of commands:

awatch -- Set a watchpoint for an expression
break -- Set breakpoint at specified line or function
catch -- Set catchpoints to catch events
clear -- Clear breakpoint at specified line or function
commands -- Set commands to be executed when a breakpoint is hit
condition -- Specify breakpoint number N to break only if COND is true
delete -- Delete some breakpoints or auto-display expressions
delete breakpoints -- Delete some breakpoints or auto-display expressions
delete checkpoint -- Delete a fork/checkpoint (experimental)
delete display -- Cancel some expressions to be displayed when program stops
delete mem -- Delete memory region
delete tracepoints -- Delete specified tracepoints
disable -- Disable some breakpoints
disable breakpoints -- Disable some breakpoints
disable display -- Disable some expressions to be displayed when program stops
disable mem -- Disable memory region
disable tracepoints -- Disable specified tracepoints
enable -- Enable some breakpoints
enable delete -- Enable breakpoints and delete when hit
---Type <return> to continue, or q <return> to quit---q
Quit
(gdb) break bar
Breakpoint 1 at 0x804837a: file prog.c, line 4.
(gdb) run
Starting program: /home/user/a.out 

Breakpoint 1, bar () at prog.c:4
4	  printf("bar?\n");
(gdb) bt
#0  bar () at prog.c:4
#1  0x08048393 in foo () at prog.c:8
#2  0x080483ab in main () at prog.c:12
(gdb) si
0x08048381	4	  printf("bar?\n");
(gdb) q
The program is running.  Exit anyway? (y or n) y

Along with single stepping, you can print data, so you can see what it's really up to.

My guess is your linked list is broken.
Either it has no nodes at all, or all the words it has are pointing to the same place in memory.

You may want to change the following line!
To a caseless compare! You may merely have a case issue! Also do the same for END just in-case its not in upper-case!

//      if (strcmp(aWord, wordRecPtr->word)==0) found=true;
      if (stricmp(aWord, wordRecPtr->word)==0) found=true;

And make sure you keep the bool bFound outside the loop! Not inside as I noticed in post#3!

@wildgoose:
stricmp() is a non-standard function and therefore isn't portable as well.
However, everyone can quickly put together his own stricmp() function, or use some kind of workaround to compare strings in a case-insensitive manner.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.