Problem with if statement

This is a discussion on Problem with if statement within the C Programming forums, part of the General Programming Boards category; I am running an if-else statement to output an error when a word can't be found in a dictionary file. ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    13

    Problem with if statement

    I am running an if-else statement to output an error when a word can't be found in a dictionary file. The program reads word 1 from the user text file and then loops through a dictionary file to find the word. The problem is that for every word in the dictionary that doesn't match, it outputs the error. How do I make it output the error after it has scanned the whole dictionary??

    Code:
        
    while(fscanf(fp2,"%s", wordcheck)!=EOF)//Reads word from text file into array//    {
        
            for (i=0; wordcheck[i]; i++)
            {
                wordcheck[i] = tolower(wordcheck[i]);//makes all characters lower case//
            }
            fseek(fp1,0,SEEK_SET);
    
    
            printf("%s", wordcheck);//debugger
            
            while(fscanf(fp1,"%s", worddict))
            {    
    
    
                if(strcmp(wordcheck, worddict)==0)//compare strings//
                {
                printf("This word: %s is in the dictionary\n", wordcheck);//debugger//
                break;
                }
                
                else
                {
                printf("This word %s is not in the dictionary", wordcheck);
                }
            }    
    
    
        }

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,833
    Get rid of the "This word %s is not in the dictionary" statement.
    Set a variable notfound = 1 at the top. Within the if (strcmp... set notfound = 0 (when you know the word was found). At the bottom, outside of the loop, you could have gotten there either by breaking out or you ran out of file. The variable notfound will tell you if the word was never matched.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    13
    Thanks for that but I'm not sure what you mean?

    I placed the notfound=1 as a variable at the top and set it to equal 0 if the word was found but I need the program to output wrong words to the user.

    How do I do that?

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    25
    I do not understand why there is a "break" in the if/else blocks.

    It also seems like the code is reading from a file into an array while also reporting on the word. Have you tried breaking these two processes into two separate functions? Or, doing them separately somehow else?
    Last edited by agxphoto; 12-20-2011 at 08:35 PM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,641
    Quote Originally Posted by agxphoto
    I do not understand why there is a "break" in the if/else blocks.
    Think of the effect of break on the enclosing while loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ; problem near if statement
    By orhanli1 in forum C Programming
    Replies: 3
    Last Post: 01-17-2011, 06:42 AM
  2. Problem with if statement
    By Sharifhs in forum C Programming
    Replies: 3
    Last Post: 08-11-2010, 05:02 PM
  3. problem with if statement
    By eagle0eyes in forum C Programming
    Replies: 13
    Last Post: 05-27-2010, 01:12 PM
  4. problem if statement
    By irncty99 in forum C++ Programming
    Replies: 2
    Last Post: 07-14-2003, 09:33 AM
  5. having a problem with a FOR statement
    By jonesy in forum C Programming
    Replies: 3
    Last Post: 10-01-2001, 01:24 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21