Thread: Help with C programming

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    1

    Unhappy Help with C programming

    So the plan is to create a wordsearch by reading in characters from a file into an array. This is my code so far, nowhere near finished but have encountered a problem already:

    -----------------------------
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    #define ROWS 20
    #define COLUMNS 20
    
    
    int main()
    {
    
    
    FILE *myfile;
    char wordsearch[ROWS][COLUMNS];
    char c;
    int x,y;
    
    
    myfile = fopen("words.txt","r");
    
    
    if (myfile == NULL)
        {
        printf("Could Not Open File\n");
        }
    
    
        else {
            printf("WORDSEARCH\n");
        }
    printf("\n");
    
    
    x=0;
    y=0;
    c=getc(myfile);
    
    
    while (c != EOF){
        if (c != ' '){
            wordsearch[y][x] = c;
    
    
            x++;
            if (y>=20){
                x=0;
                y++;
            }
        }
    }
    
    
    for (y=0;y<COLUMNS;y++){
        for (x=0;x<ROWS;x++){
            printf("%c",wordsearch[y][x]);
        }
    }
    
    
    
    
    return 0;
    }
    --------------------------

    My question is to do with the section highlighted in red... I can't really see why it is not printing out my array (wordsearch), it's probably very basic but I have no C experience so am not too confident in what I'm doing. Any help with this bit would be much appreciated the rest I can work on afterwards.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    First, I would clean up your indentation a bit, the easier it is to read your code, the fewer mistakes you will make, and the easier it will be to spot them if you do. If it looks good on your screen, then take a minute to tidy it up on the forum (edit your post), so it's easy for us to read, thus making it easier for us to help you.

    There's a few problems here:
    Code:
    while (c != EOF){
        if (c != ' '){
            wordsearch[y][x] = c;
    
            x++;
            if (y>=20){
                x=0;
                y++;
            }
        }
    }
    • Don't use magic numbers. You defined ROWS and COLUMNS, so make sure you use those constants everywhere.
    • You seem to be getting x and y mixed up. You check if y is >= 20 ,but then reset x instead, and keep incrementing y. You actually declare your array as ROWS and COLUMNS, so I suggest r and c, or row and col(umn) for variable names, to keep things straight. Note, you declare your array in row-major form (i.e. [ROWS][COLUMNS] instead of the other way around), so that helps remind you to use wordsearch[r][c].
    • You should try using a function like isspace or isalpha (#include <ctype.h>) to check for valid word search characters instead of (c != ' '). You may encounter other invalid characters, like a new line, which your current code would include.
    • You should check that x and y are valid right before you actually access the array, not after. That will prevent any buffer overflows.
    • Your program needs to exit immediately if it can't open the file, otherwise you try to read from an unopened file (myfile will be NULL). That means getc will attempt to dereference memory at NULL, which results in undefined behavior. That technically means anything could happen, from nothing at all to reformatting your hard drive, but in reality, it probably means your program will crash.
    • You should print a new line after every row of letters you print out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-11-2012, 01:03 AM
  2. Replies: 4
    Last Post: 12-11-2011, 04:25 PM
  3. small programming job VCPP / Object Oriented Programming
    By calgonite in forum Projects and Job Recruitment
    Replies: 10
    Last Post: 01-04-2006, 11:48 PM
  4. Total newb to programming here... Question about the many programming languages. Ty!
    By tsubotakid1 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 10-05-2003, 10:32 AM

Tags for this Thread