Thread: terminating a while loop with a character

  1. #1
    Registered User
    Join Date
    Apr 2007

    terminating a while loop with a character

    Hi, I'm trying to write a simple spell-check program where the user enters one word at a time, but I'm having trouble terminating the program when the user is finished. I would like the program to finish accepting input when they user enters a period. This is what I've got, any help would be appreciated.

    #define SIZE 46000
    #define WORD_SIZE 128
    int main( void )
    	// Reads linux.words into buffer array
    	FILE *dict;
    	dict = fopen( "linux.words", "r" );
    	if( ! dict )
    		printf( "Failed to input linux.words\n" );
                    return 1;
    	char buffer[SIZE][WORD_SIZE];
            int i = 0;
            while ( fgets( buffer[i] , SIZE, dict ) != NULL ) {
    	char word[99][29];
    	int j;
    	// This for loop reads one word at a time from the user
    	// and stores the words in an array to be encoded later.
    	for( j=0; j<100; j++ )
    		printf( "Enter a word( . to quit )\n" );
    		fgets( word[j], 31, stdin );
    			if( (strcmp( word[j], "." )) == 1 )
    	return 0;
    With this the program only takes one word and then quits.


  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    United States
    Yes well
    if( (strcmp( word[j], "." )) == 1 )
    It likely has to do with this. It would be rare for word[j] to contain a string that is just a period. That is the array &#123; '.', '\0' &#125; and not just the character '.' like you were thinking. Something like strchr would serve your purposes better.

    And while you're at it, learn how strcmp works. 1 is not a match.

  3. #3
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Luxembourg, Europe
    Spell checker ... wow, that brings back a lot of memories. From using simple arrays to tries and the all powerful graph model to store a dictionary to finally dynamic programming and levenshtein distance to find the correct word.

    This could become interesting.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > for( j=0; j<100; j++ )
    This overflows the number of words, by one word.

    > fgets( word[j], 31, stdin );
    This overflows the length of a word by a couple of characters.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Visual Studio Express / Windows SDK?
    By cyberfish in forum C++ Programming
    Replies: 23
    Last Post: 01-22-2009, 02:13 AM
  2. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  3. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  4. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  5. Help with this small C program
    By Havax in forum C Programming
    Replies: 4
    Last Post: 10-31-2005, 03:18 PM