Thread: wordsearch

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

    wordsearch

    i am writing a wordsearch program to find words in a puzzle (go figure). so far, all i have is everything i need opened but i dont no where to go from here......any suggestions would be great....thanx

    Code:
    /* A program to solve word search puzzles. This program will contain 3
    ** Different files, an Output, WordSearch, and WordList file. */
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    main ()
    {
     int i,x;
     char WordSearch[15][15];
     char WordList[10];
     FILE *ptr1 = NULL;
     FILE *ptr2 = NULL;
     FILE *ptr3 = NULL;
    /* Opening the txt file with the puzzle in it */
     /* The puzzle will be opened in a 2-D array and then closed */
     ptr1 = fopen("WordSearch.txt" , "r");
     if (!ptr1)
     {
      printf("File not Found");
      exit (0);
     }
     
     for (i=0; i<15; i++)
     {
      for (x=0; x<15; x++)
       {
           fscanf(ptr1 , "%c" , &WordSearch[i][x]);
        if (WordSearch[i][x] == '\n' || WordSearch[i][x] == ' ')
        {
         x--;
         continue;
        }
        
        printf (" %c", WordSearch[i][x]);
       }
        printf("\n");
     }
     printf("\n");
     fclose(ptr1);
     ptr1 = NULL;
    /* Opening the txt file with the words to search for in it */
     /* The word list will be left opened so it is easy to access */
     ptr2 = fopen("WordList.txt" , "r");
     if (!ptr2)
     {
      printf("File not Found");
      exit (0);
     } 
      printf("\n");
      for (x=0; x<12; x++) 
      {
       fscanf(ptr2 , "%s" , WordList);
       printf("%s\n" , WordList); }
      printf("\n\n\n\n\n\n\n\n");
    /* Opening the txt file to store the output in */
    /* The output will be left open to keep storing data in it */
     ptr3 = fopen("Output.txt" , "w");
     if (!ptr3)
     {
      printf("File not Found");
      exit (0);
     }
     return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So what, we're to teach you basic logic? How to break jobs down into smaller pieces, or what exactly? Write it out on paper. Break each step into smaller steps. Repeat until you have the smallest steps you can think of. Turn it into code.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    34
    alright so i figured out what to do about my code but what i have so far wont run...any help would be apprecieated...

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define WORDSEARCH "WordSearch.txt" // The Puzzle
    #define WORDLIST   "WordList.txt"   // The List of Words
    #define ROWS 15 // The # of Rows
    #define COLS 15 // The # of Columns
    
    typedef struct {
    	int row;
    	int col;
    } pos_st;
    
    
    /* Declaring the Functions */
    void Print_Puzzle ( FILE *ptr1 , char WordSearch [ROWS][COLS]);
    
    /* Main is used to open all the files and to scan the puzzle and the words 
    ** into the program. It then receives the functions. */
    main ()
    {
     int i,located;
     char WordSearch[ROWS][COLS];
     char temp[50]; //temporary char to hold the words
     char *nptr;
     FILE *ptr1 = NULL;
     FILE *ptr2 = NULL;
     FILE *ptr3 = NULL;
     
     /* Opening the txt file with the puzzle in it */
     /* The puzzle will be opened in a 2-D array and then closed */
     ptr1 = fopen( WORDSEARCH , "r");
     if (!ptr1)
     {
      printf("File not Found");
      exit (0);
     }
     /* Opening the txt file with the words to search for in it */
     /* The word list will be left opened so it is easy to access */
     ptr2 = fopen(WORDLIST , "r");
     if (!ptr2)
     {
      printf("File not Found");
      exit (0);
     } 
     /* Opening the txt file to store the output in */
     /* The output will be left open to keep storing data in it */
     ptr3 = fopen("Output.txt" , "w");
     if (!ptr3)
     {
      printf("File not Found");
      exit (0);
     } 
     
     Print_Puzzle(ptr1 , WordSearch);
     fclose(ptr1);
     ptr1 = NULL;
     
     for (i=0; !(feof(ptr2)); i++)
     {
    	 fgets(temp,sizeof temp, ptr2);
    	 if ( (nptr = strchr (temp, '\n')) != NULL)
    	 {
    		 *nptr = '\0';
    		 located = find (WordSearch, temp);
    	 }
     }
     return 0;
    }
    
    void Print_Puzzle( FILE *ptr1, char wordsearch[ROWS][COLS])
    {
    	int row, column, i;
    	printf("subscript by [across,vertical]\n\n\n     ");
    	for (i=0; i< COLS ; i++)
    	{
    		printf("%i", i);
    	}
    	printf("\n  %c", 218);
    
    	for (i=0; i<= ROWS ; i++)
    	{
    		printf("%c", 196);
    	}
    
    	printf("\n");
    	for( row = 0; row < ROWS; row++)
    	{
    		printf(" %i%c ", row, 179);
    		for (column = 0; column <= COLS; column++)
    		{
    			wordsearch[row][column] = fgetc(ptr1);
    			printf("%c", wordsearch[row][column]);
    		}
    	}
    }

  4. #4
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    I was messing around with your code, and trying to be as imaginative as possible. I know this may not help, but there still is a chance! Btw this doesn't do much:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    #define ROWS 15
    #define COLS 15
    
    typedef struct {
        FILE *fpPuzzle;
        FILE *fpList;
        FILE *fpOutput;
    } OurFiles;
    
    /* Declaring the Functions */
    void Print_Puzzle(void);
    void initializeFiles(void);
    void badError(const char *szError);
    
    char WordSearch[ROWS][COLS];
    OurFiles *files;
    
    /* Main is used to open all the files and to scan the puzzle and the words
    ** into the program. It then receives the functions. */
    int main(void)
    {
        int located;
        char temp[255];             //temporary char to hold the words
        char *nptr;
    
        files = malloc(1 * sizeof(*files));
        if (files == NULL)
            badError("AAAHHH!!!");  // A professional error message
    
        initializeFiles();
    
        /* Get that file stuff! */
        Print_Puzzle();
    
        /* We don't need the puzzle file anymore */
        fclose(files->fpPuzzle);
        files->fpPuzzle = NULL;
    
        int x, y;
        for (x = 0; x < ROWS; ++x) {
            for (y = 0; y < COLS; ++y) {
                fputc(WordSearch[x][y], files->fpOutput);
            }
            fputc('\n', files->fpOutput);
        }
    
        fclose(files->fpList);
        files->fpList = NULL;
        fclose(files->fpOutput);
        files->fpOutput = NULL;
    
        return 0;
    }
    
    void initializeFiles(void)
    {
    
        const char fileNames[3][20] = {
            "WordPuzzle.txt",
            "WordList.txt",
            "WordOutput.txt"
        };
        const char fileAccess[3][4] = {
            "r",
            "r",
            "w"
        };
    
        files->fpPuzzle = NULL;
        files->fpList = NULL;
        files->fpOutput = NULL;
    
        int size = sizeof(fileNames) / sizeof(*fileNames);
        int i;
        FILE **ptr;
        for (i = 0; i < size; ++i) {
            switch (i) {
            case 0:
                ptr = &(files->fpPuzzle);
                break;
            case 1:
                ptr = &(files->fpList);
                break;
            case 2:
                ptr = &(files->fpOutput);
                break;
            default:
                badError("Out of bounds file initialization.");
            }
    
            *ptr = fopen(fileNames[i], fileAccess[i]);
            if (!*ptr) {
                badError("Could not open file");
            }
        }
    }
    
    void badError(const char *szError)
    {
        printf("ERROR: %s\n", szError);
        exit(1);
    }
    
    void Print_Puzzle(void)
    {
    
        int row, column;
        printf("%d x %d\n" "-----------------\n", COLS, ROWS);
    
        if (!files->fpPuzzle) {
            badError("File Puzzle isn't working");
        }
    
        for (row = 0; row < ROWS; ++row) {
            for (column = 0; column < COLS; ++column) {
                do {
    
                    WordSearch[row][column] = getc(files->fpPuzzle);
    
    
                    if (WordSearch[row][column] == EOF)
                        badError("File Puzzle not long Enough");
                } while (!isalpha(WordSearch[row][column]));
    
                printf("%c ", WordSearch[row][column]);
            }
            putchar('\n');
        }
    }

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by egomaster69
    alright so i figured out what to do about my code but what i have so far wont run...any help would be apprecieated...
    You know, you really need to start figuring out how to ask questions. It is really annoying seeing any of your posts, because I can now automaticly assume it will have absolutely no relevant information in it.

    What do you mean "it won't run"? That doesn't tell me anything. Does it compile? If so, then it "runs". It may not do what you like, but if it compiles, then it does in fact run. Does it give you a segfault when it runs? Does it give you any out put at all? Does it give errors and warnings instead of compiling?

    I can safely speak for the majority of the long time posters when I say none of us want to read your entire program, or even copy-paste your program into an editor to try and compile it when you give us nothing to go on.

    "It doesn't run" doesn't even make me take a glance at your code.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wrong Output
    By egomaster69 in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 06:44 PM
  2. wordsearch
    By linuxdude in forum C Programming
    Replies: 2
    Last Post: 02-20-2004, 09:13 PM
  3. wordsearch solver, need help
    By lakai02 in forum C Programming
    Replies: 3
    Last Post: 01-14-2003, 02:55 PM