Wrong Output

This is a discussion on Wrong Output within the C Programming forums, part of the General Programming Boards category; I finished writing a wordsearch program and it compiles without any errors or warnings. When I run the program, only ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    34

    Wrong Output

    I finished writing a wordsearch program and it compiles without any errors or warnings. When I run the program, only the Print_Puzzle function displays output. The FIND and SEARCH function are no-where to be found. The FIND function should display where each word begins and ends. If the word is not found, it should display the default message... Thanx in advance...Hope thats descriptive enough for ya Quazh

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <string.h>
    #define WORDSEARCH "WordSearch.txt"//The puzzle
    #define WORDLIST   "WordList.txt" //The list of Words
    #define ROWS    15                 //# of Rows
    #define COLS    15               //# of Columns
    
    typedef struct 
    {
        int row;
        int col;
    } position;
    
    
    //Declaring the Functions that are going to be used
    void Print_Puzzle (FILE *wrdsearch ,FILE *wrdlist,char wordsearch[ROWS][COLS]);
    int FIND (char grid[ROWS][COLS],char *word);
    int SEARCH (char grid[ROWS][COLS],char *word,position *start,position *end);
    
    int main ( ) 
    {
    
        FILE *wrdlist; 
        FILE *wrdsearch;
        FILE *solutions;
    
        int i;
        int x;
    
        char wordtmp[80];  //Temp is to hold words from the WordList
        char *nptr;
        char grid[ROWS][COLS];
    
    //Opening the WordSearch
     wrdsearch = fopen( WORDSEARCH , "r");
     if (!wrdsearch)
     {
      printf("File not Found");
      exit (0);
     }
    //Opening the WordList        
    wrdlist = fopen( WORDLIST , "r");
     if (!wrdlist)
     {
      printf("File not Found");
      exit (0);
     }
    
    //Opening the Output
    solutions = fopen( "output.txt" , "w");
     if (!solutions)
     {
      printf("File not Found");
      exit (0);
     }
    
        Print_Puzzle(wrdsearch,wrdlist,grid);
        fclose(wrdsearch);
        wrdsearch = NULL ;
    
        for ( i = 0; !(feof(wrdlist)); i++ )
        {
            fgets(wordtmp,sizeof wordtmp,wrdlist);
    
            if ( (nptr = strchr (wordtmp,'\n' ) ) != NULL )
            {
                *nptr = '\0';
                x = FIND ( grid, wordtmp );
            }
        }/* Strchr() returns a pointer to the first occurrence of character 
    ** located within s. If character c does not occur in the string, 
    ** strchr() returns a null pointer. */
        return 0;
    }
    //PRINTING OUT THE PUZZLE AND THE WORDS, CAN PRINT ANY SIZE PUZZLE
    void Print_Puzzle(FILE *wrdsearch,FILE *wrdlist,char WordSearch[ROWS][COLS])
    {
        int row , column, i;
        char WordList[80];
    
        printf("THE PUZZLE...size %i by %i\n", ROWS, COLS);
        printf("____________________________\n");
        for ( row=0 ; row < ROWS ; row++)
        {
            for( column = 0; column < COLS ; column++ )
            {
                fscanf(wrdsearch, "%c", &WordSearch[row][column]);
                    if(WordSearch[row][column] == '\n' ||
                       WordSearch[row][column] == ' '     )
                    {
                        column--;
                        continue;
                    }
                printf("%c ", WordSearch[row][column]);
            }
            printf("\n");
        }   printf("\n\n\n");
        //PRINTING OUT THE WORDS 
        printf("The Words to Search For\n");
        printf("_______________________\n");
        for ( i = 0; !(feof(wrdlist)); i++ )
         {
            fscanf(wrdlist, "%s", WordList);
            printf("%s\n", WordList);
         }
    }
    // The find function searches a word recieved from main 
    // Starting at each position in the grid
    int FIND ( char grid[ROWS][COLS], char *word ) 
    {
        int row,column;
    
        printf("The subscripts are by [across,vertical]\n\n\n");
        for ( row=0 ; row<ROWS ; row++ ) 
        {
            for ( column=0 ; column<COLS ; column++ ) 
            {
                position  start = { row, column };
                position  end;
    
                if ( SEARCH( grid, word, &start, &end ) ) 
                {
                    printf( "%s starts at [%d,%d] and ends at [%d,%d]\n\n",
                        word, start.row, start.col, end.row, end.col );
                    
                    if ( !(SEARCH( grid, word, &start, &end )))
                        printf("%s IS NOT IN THE PUZZLE",word);
    
                    return 0;
                }
            }
        }
        
        return 1;
    }
    // The Find Function sends SEARCH a start position and 
    // Search will look for the word in 8 possible directions
    int SEARCH (char grid[ROWS][COLS],char *word,position *start,position *end) 
    {
        int row , column ;
    
        for ( row = -1 ; row <= 1 ; row++ ) 
        {
            for ( column = -1 ; column <= 1 ; column++ ) 
            {
                if ( row != 0 || column != 0 ) 
                {
                    char *wrdp = word;
                         *end  = *start;
    
    /* This While loop checks to see that the letters match 
    ** and that the end position is not out of the puzzle */
      while ( grid[end->row][end->col] == *wrdp &&
                   end->row >= 0 && end->row < ROWS 
                                 &&
                   end->col >= 0 && end->col < COLS ) 
      {
                           wrdp++;
                        if ( *wrdp == '\0' )
                        {
                            return 1;                /* There is a match */
                        }
                /* This will check the next 
                ** position in the current
                ** direction for colums and rows */
                        end->col += column;  
                        end->row += row;             
      }
                }
            }
        }
        return 0;               /* The Word is not found / is not in Puzzle */
    }
    output:
    Code:
    THE PUZZLE...size 15 by 15
    ____________________________
    Y P H W T E V S C T N A K Y O
    E O O J N O S I O P M K C A B
    X R V Y T D K J X M X F Y J H
    D W Q Q T O G P O X T W X L B
    Q Z M C H C D C Y M X B S Z D
    Z E Z F Y U R A P G M W H O H
    B T O P J E E L Y T U R O W W
    O B V Y R Y L S A V W G R T Q
    P H I S H J B Z L H N O T H V
    G R J Q E E F A Z F H L Y S O
    Z O G B H E Q E I U H L M G V
    Y U M A B O F L B R P E B X Q
    H K P R B R T J Z J I H Z K L
    D S D P U E P H I K P S V W P
    C X K Z R G V T X U Q G R V T
    
    
    
    The Words to Search For
    _______________________
    HELLO
    COMMA
    FILTER
    PHISH
    POISION
    TODAY
    WORD
    BACK
    PUZZLE
    SHORT
    CREEP
    RADIOHEAD

  2. #2
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    don't use feof in a control loop

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    34
    I tried to take feof out of the loop but it still doesn't work. I replaces feof with 12 in main() and the output adds one line from the FIND function. It add the "subscripts are by [column, row]".

    Code:
    for ( i=0; i<12 ; i++ )
        {
            fgets(wordtmp,sizeof wordtmp,wrdlist);
    
            if ( (nptr = strchr (wordtmp,'\n' ) ) != NULL )
            {
                *nptr = '\0';
                x = FIND ( grid, wordtmp );
            }
        }
    the code was just a snippit from main()...nothing has really changed

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Find should look more like this:
    Code:
    int FIND ( char grid[ROWS][COLS], char *word ) 
    {
        int row,column;
    
        printf("The subscripts are by [across,vertical]\n\n\n");
        for ( row=0 ; row<ROWS ; row++ ) 
        {
            for ( column=0 ; column<COLS ; column++ ) 
            {
                position  start = { row, column };
                position  end;
    
                if ( SEARCH( grid, word, &start, &end ) ) 
                {
                    printf( "%s starts at [%d,%d] and ends at [%d,%d]\n\n",
                        word, start.row, start.col, end.row, end.col );
                    return 1;
                }
            }
        }
        
        printf("%s IS NOT IN THE PUZZLE",word);
        return 0;
    }

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    34
    I have edited my code but i still can't seem to find the problem. The output that printed out does not even make sense. Judging from the output though, i think the bug is in my search function or when i send the words to FIND from main()...

    Code:
    /* Preprocessor directives */
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    /* Defining words for easier referance */
    #define WORDSEARCH "WordSearch.txt"    /* The puzzle        */
    #define WORDLIST   "WordList.txt"      /* The list of Words */
    #define ROWS    15                       /* # of Rows         */
    #define COLS    15                      /* # of Columns      */
    
    typedef struct 
    {
        int row;
        int col;
    } position;
    
    /* Declaring the Functions that are going to be used */
    int FIND (char grid[ROWS][COLS],char *word, FILE *solutions);
    int SEARCH (char grid[ROWS][COLS],char *word,position *start,position *end);
    
    main ( ) 
    {
        int i;
        int x;
        char buffer[100];  /* Buffer is to hold words from the WordList */
        char *nptr;
        char grid[ROWS][COLS];
        FILE *wrdsearch;
        FILE *wrdlist;
        FILE *solutions;
    
    /* Opening the WordSearch */
     wrdsearch = fopen( WORDSEARCH , "r");
     if (!wrdsearch)
     {
      printf("File not Found");
      exit (0);
     }
    /* Opening the WordList */        
    wrdlist = fopen( WORDLIST , "r");
     if (!wrdlist)
     {
      printf("File not Found");
      exit (0);
     }
    /* Opening the Output */
    solutions = fopen( "output.txt" , "w");
     if (!solutions)
     {
      printf("File not Found");
      exit (0);
     }
    
        fclose(wrdsearch);
        wrdsearch = NULL ;
    
        for ( i=0; i<12 ; i++ )
        {
            fgets(buffer,sizeof buffer,wrdlist);
    
            if ( (nptr = strchr (buffer,'\n' ) ) != NULL )
            {
                *nptr = '\0';
                x = FIND ( grid,buffer,solutions );
            }
        }
    
        return 0;
    }
    
    /* FIND()
    * Description:
    * Will search for a word starting at each position in the grid. It will send
    * the SEARCH function a position to begin searching from.  */
    int FIND (char grid[ROWS][COLS], char *word, FILE *solutions) 
    {
        int row,column;
        
        printf("\n\n");
        fprintf(solutions,"\n\n");
        printf("The subscripts are by [row,column]\n\n");
        fprintf(solutions, "The subscripts are by [row,column]\n\n");
        for ( row=0 ; row<ROWS ; row++ ) 
        {
            for ( column=0 ; column<COLS ; column++ ) 
            {
                position  start = { row, column };
                position  end;
    
                if ( SEARCH( grid, word, &start, &end ) ) 
                {
            printf("%s,\n starts at [%d,%d] and ends at [%d,%d]\n\n",
                      word, start.row, start.col, end.row, end.col );
            fprintf(solutions,"%s,\n starts at [%d,%d] and ends at [%d,%d]\n\n",
                      word, start.row, start.col, end.row, end.col );
                    return 1;
                }
            }
        }
        
        printf("%s IS NOT IN THE PUZZLE\n",word);
        fprintf(solutions, "%s IS NOT IN THE PUZZLE\n",word);
        return 0;
    }
    /* SEARCH()
    * Description:
    * Search Function will look for the word in 8 possible directions. */
    int SEARCH (char grid[ROWS][COLS],char *word,position *start,position *end) 
    {
        int row , column ;
    
        for ( row=-1 ; row<= 1 ; row++ ) 
        {
            for ( column=-1 ; column<= 1 ; column++ ) 
            {
                if ( row != 0 || column != 0 ) 
                {
                    char *wrdp = word;
                         *end  = *start;
    
    /* This While loop checks to see that the letters match 
    ** and that the end position is not out of the puzzle */
      while ( grid[end->row][end->col] == *wrdp &&
                                  end->row >= 0 && end->row < ROWS 
                                                &&
                                  end->col >= 0 && end->col < COLS ) 
      {
                              wrdp++;
                        if ( *wrdp == '\0' )
                        {
                            return 1;              /* TRUE / There is a match */
                        }
                /* This will check the next 
                ** position in the current
                ** direction for colums and rows */
                        end->col += column;  
                        end->row += row;             
      }
                }
            }
        }
        return 0;         /* FALSE / The Word is not found / is not in Puzzle */
    }
    OUTPUT
    Code:
    The subscripts are by [row,column]
    
    ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
    ╠╠╠╠ IS NOT IN THE PUZZLE
    Thank-you in advance

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Why did you take out Print_Puzzle? That's what actually reads the puzzle file and stores it into grid. Otherwise you have an empty grid. You could split it into read puzzle and print puzzle functions.

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    34
    Oh I didnt take it out, i just edited it out to make the code shorter on the post. BTW i figured out what was wrong with my code. When i was sending the words from main, something wasn't going right. So in the Print_Puzzle function, i sent the words along while printing them out.

    Code:
    void Print_Puzzle(FILE *wrdsearch,FILE *wrdlist,char WordSearch[ROWS][COLS],
                      FILE *solutions)
    {
        int row , column, i;
        char WordList[80];
    
        printf("NOTE: SUBSCRIPTS ARE BY [row,column]\n\n");
        fprintf(solutions, "NOTE: SUBSCRIPTS ARE BY [row,column]\n\n");
    
           printf("THE PUZZLE...size %i by %i\n", ROWS, COLS);
        fprintf(solutions,"THE PUZZLE...size %i by %i\n", ROWS, COLS);
        
        printf("____________________________\n");
        fprintf(solutions,"____________________________\n");
    
        for ( row=0 ; row < ROWS ; row++)
        {
            for( column = 0; column < COLS ; column++ )
            {
                fscanf(wrdsearch, "%c", &WordSearch[row][column]);
                    if(WordSearch[row][column] == '\n' ||
                       WordSearch[row][column] == ' '     )
                    {                    /* Skip new lines and spaces */
                        column--;
                        continue;
                    }
                printf("%c ", WordSearch[row][column]);
                fprintf(solutions,"%c ",WordSearch[row][column]);
            }
            printf("\n");
            fprintf(solutions,"\n");
        }   printf("\n\n\n");
            fprintf(solutions,"\n\n\n");
    
        /* PRINTING OUT THE WORDS */
            
        printf("The Words to Search For\n");
        printf("_______________________\n");
    
        fprintf(solutions,"The Words to Search For\n");
        fprintf(solutions,"_______________________\n");
        for ( i = 0; !(feof(wrdlist)); i++ )
         {
            fscanf(wrdlist, "%s", WordList);
            printf("%s\n", WordList);
            fprintf(solutions,"%s\n", WordList);
            FIND ( WordSearch,WordList,solutions );  /* Send the FIND function
                                                       the words */
         }
    }
    There ...1 line of code replaced about 6
    Thanks for your help all though

  8. #8
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    This took me a long time!! Another way to think of things, read the comments and the logic. I'm not perfect, but it's just another way of doing things. And hey, if you like my style of coding, check out this guy's pillars of coding:
    http://www.google.com/search?num=100...ld&btnG=Search

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <string.h>
    
    #define ROWS    15
    #define COLS    15
    
    // The position of the word
    typedef struct 
    {
       int row;
       int column;
    } GridPosition;
    
    
    // Input should be all caps?
    typedef enum
    {
       CAPS_OFF,
       CAPS_ON
    } CapsOption;
    
    
    // Print out the puzzle
    void Print_Puzzle( FILE *ws, FILE *wl );
    
    // Find word in puzzle
    int FindInPuzzle( const char *w );
    
    // Search word in puzzle
    int SearchInPuzzle( const char *w, const GridPosition *s, GridPosition *e );
    
    // Get line of input
    int getLine( FILE *s, char l[], CapsOption co );
    
    // Puzzle Grid
    char G[ROWS][COLS];
    
    
    int main ( )
    {
       // wl = Word List file
       // ws = Word Search file
       
       FILE *wl;
       FILE *ws;
       
       // wt = Temperary word for the wordlist
       
       char wt[80];
    
       // Open the wordsearch file for reading
       
       ws = fopen( "wordSearch.txt" , "r" );
       if ( !ws )
       {
          puts( "Could not open wordSearch file" );
          return 1;
       }
       
       // Open the wordlist file for reading
       
       wl = fopen( "wordList.txt", "r" );
       if ( !wl )
       {
          puts( "Could not open wordList file" );
          return 1;
       }
    
       // Print out the puzzle in terminal
    
       Print_Puzzle( ws, wl );
    
       // Have the user enter a word, and we find
       // out where the word starts/ends
    
       puts( "Please enter a word to find:" );
       while( getLine( stdin, wt, CAPS_ON ) )
       {
          if( strlen( wt ) < 2 ) continue;
          FindInPuzzle( wt );
          puts( "Please enter a word to find:" );
       }
    
       // Close all of the file pointers
    
       fclose( ws );
       fclose( wl );
    
       // Return success
       
       return 0;
    }
    
    
    
    // Get line of input
    int getLine(
       FILE *s,         // The stream to get the line from
       char l[],        // The character array that needs a line of input
       CapsOption co)   // Should all of the input be upper case?
    {
       // lt = temperary string to copy from
       // Make sure malloc worked
    
       char *lt = malloc( 255 * sizeof( *lt ) );
       if(lt == NULL)
       {
          l[0] = '\n';
          return 0;
       }
    
       // lc = location of where we are on the line
       // c = the temp character
    
       int lc = 0;
       int c;
    
       // Start inputting those characters from the user!
    
       while( ( c=fgetc( s ) ) != '\n' && c != EOF )
       {
          if (co == CAPS_ON)
          {
             lt[lc++] = toupper(c);
             continue;
          }
          lt[lc++] = c;
       }
          
       lt[lc] = '\0';
    
       // Make sure EOF has not occured
    
       if( c == EOF )
       {
          l[0] = '\0';
          return 0;
       }
    
       // Copy the characters to the input
    
       strcpy( l, lt );
    
       // Free the malloc'ed memory
    
       free( lt );
    
       // Exit successfully
    
       return 1;
    }
    
    
    
    
    
    // Print out the puzzle
    void Print_Puzzle(
       FILE *ws,  // The word search file pointer
       FILE *wl ) // The word list file pointer
    {
       // r = row for looper
       // c = column for looper
       
       int r=0, c=0;
    
       // wt = temperary word
       // ct = temperary character from word search input
       
       char wt[80];
       int ct;
    
       // Print out header
       
       printf("THE PUZZLE...size %i by %i\n", ROWS, COLS);
       puts("------");
    
       // Print out the puzzle from wordPuzzle.txt
       
       for( r=0; r < ROWS; r++ )
       {
          for( c=0; c < COLS; c++ )
          {
             [color=blue]// Make sure the character from the word search file is
             // Alphabetical and also not End of File
             
             do{ ct = fgetc( ws ); } while( !isalpha( ct ) && ct != EOF );
    
             // If we are at the end of the file, the puzzle is too short
             // on wordPuzzle.txt
             
             if( ct == EOF )
             {
                puts( "Puzzle is too short" );
                exit( 1 );
             }
    
             // Put the character into the array
    
             G[r][c] = toupper( ct );
    
             // Print out the character that was taken from the wordsearch file
    
             printf("%c ", G[r][c] );
          }
    
          putchar('\n');
       }
             
       // Print out the words that we should search for
       // This data is from the wordList.txt file
       
       puts(   "\n\n"
             "The Words to Search For\n"
             "------" );
       while( getLine( wl, wt, CAPS_OFF ) )
       {
          printf( "%s:\n", wt );
          FindInPuzzle(wt);
          puts("------");
       }
    }
    
    
    
    
    
    
    // The find function searches a word recieved from main
    // Starting at each position in the grid
    int FindInPuzzle (
       const char *w )   // The word we're trying to find in the grid
    {
       // Rows / columns
       
       int r, c;
    
       // start/end word positions
    
       GridPosition *s, *e;
       s = malloc(1 * sizeof(*s));
       e = malloc(1 * sizeof(*e));
       if(!e || !s)
       {
          puts( "Son of a bastard" );
          exit( 1 );
       }
    
       // Search for the word in the puzzle grid
       
       printf("Locating word...\n");
       for ( r=0; r < ROWS; r++ )
       {
          for ( c=0; c < COLS; c++ )
          {
             // Make sure the word's first letter matches the grid's
             // letter at the current point before proceeding
    
             if( *w != G[r][c] ) continue;
    
             // Set where the starting position is
             
             s->row = r;
             s->column = c;
    
             // Notify the user you're investigating a certain position
    
             printf("Investigating position [%d, %d]...\n", c, r);
    
             // Start searching from the starting position!
             
             if( SearchInPuzzle( w, s, e ) )
             {
                printf( "%s starts at [%d,%d] and ends at [%d,%d]\n\n",
                   w, s->column, s->row, e->column, e->row );
    
                return 0;
             }
          }
       }
    
       // We couldn't find the word in the puzzle grid!
       
       printf( "\"%s\" could not be found.\n\n", w );
       return 1;
    }
    
    
    
    
    
    
    // Search for the word starting at the designated starting point
    int SearchInPuzzle(
       const char *w,          // The word we're searching for on the grid
       const GridPosition *s,  // The position where we should starting searching for the word
       GridPosition *e )       // The position that the word ends on
    {
       // Create our temperary starting position
       // ...we'll be changing it a lot and we don't want to edit the master copy
    
       GridPosition s1 = *s;
    
       // l = word current location (for verifying characters)
    
       int l=0;
    
       // Check for word straight across
    
       while( s1.column+1 != COLS                         // Make sure we're not going off the grid
             && l+1 != strlen( w )                        // Make sure the length of the string is out (SUCCESS)
             && G[s1.row][++( s1.column )] ==  w[++l] );  // Make sure the current char on the grid is equal to the word
    
       // The word was found!
       
       if( l+1 == strlen( w ) )
       {
          e->column = s1.column;
          return 1;
       }
    
       // Reset word position & temperary GridPosition for going straight down
       
       l=0;
       s1 = *s;
       
       // Check for the word straight down
    
       while( s1.row+1 != ROWS                               // Make sure we're not going off the grid
             && l+1 != strlen( w )                           // Make sure the length of the string is out (SUCCESS)
             && G[++( s1.row )][s1.column] ==  w[++l] );     // Make sure the current char on the grid is equal to the word
    
       // The word was found!
    
       if( l+1 == strlen( w ) )
       {
          e->row = s1.row;
          return 1;
       }
    
       // The word was not found, exit error
       
       return 0;
    }
    Last edited by Kleid-0; 01-28-2005 at 06:46 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wrong Output!
    By kolliash in forum C++ Programming
    Replies: 6
    Last Post: 06-19-2008, 08:55 AM
  2. Something Wrong with my function in Linux!
    By Matus in forum C Programming
    Replies: 5
    Last Post: 04-30-2008, 11:00 PM
  3. Getting wrong output from a class
    By orikon in forum C++ Programming
    Replies: 11
    Last Post: 11-18-2005, 07:58 PM
  4. Why is the output of this wrong?
    By Tokimasa in forum C++ Programming
    Replies: 4
    Last Post: 11-30-2004, 01:58 PM
  5. Leap year program prints wrong output
    By Guti14 in forum C Programming
    Replies: 8
    Last Post: 08-24-2004, 12:56 PM

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