Thread: Program not working as expected

  1. #16
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    As i can see on line 55, you want the returned value from 'onMap' output as an character.
    So, the function 'onMap' should return a char.
    There for, i have edited the struct and the function as follow:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    struct COORD {
        int xV;
        int yV;
        char floor;
    };
     
    char onMap(const int x, const int y, const struct COORD *z, const int num)
    {
        int i;
    
        for ( i = 0 ; i < num ; i++) {
            if(z[i].xV == x && z[i].yV == y) {
                return z[i].floor;
            }
        }
        return '.';
    }
    The function has also become the new argument 'num', This argument should be the number of valid entries in array.
    I do that, because a function can't determine how big an array is.
    The member 'floor' in the struct is now of type char.
    If there is a valid entry for the requested coordinate, the function return this char.
    If nothing is found, it returns '.' (also a char).


    Now to your main program.
    If you open a file, you should allways check if the file was successfully opened.
    And you should allways close a file if you don't need it anymore.
    Your usage of 'fscanf' was wrong. 'fscanf' (also 'scanf' and friends) return allways the number of correctly scaned values.
    This means in your case 3 values.
    I have also updated the line to read the rest of the line (check against EOF).
    The output loop at the end is heavy updated.
    It is now a outer loop for the lines and a inner loop for the columns.
    Code:
    int main(void)
    {
    
        FILE *input;
        char *filename = "C:\\Users\\John\\Desktop\\input.txt";
    
        if ((input = fopen(filename, "r")) == NULL) {
            fprintf(stderr, "Can't open file '%s'! exit.\n", filename);
            return 1;
        }
    
        int x, y, le;
        int count = 0;
        char tile[5];
        struct COORD coordinates[50];
    
        while (fscanf(input, "%i %i %4s", &x, &y, tile) == 3) {
            coordinates[count].xV = x;
            coordinates[count].yV = y;
            coordinates[count].floor = tile[(strlen(tile)-1)];
            count++;
            while((le = fgetc(input)) != '\n' && le != EOF);
        }
        fclose(input);
    
        printf ("Count: %d\n", count); // for debuging
    
        int col, row;
    
        for(row = 1 ; row < 31 ; row++) {
            for(col = 1 ; col < 31 ; col++) {
                printf("%c", onMap(col, row, coordinates, count));
            }
            printf ("%d||\n", row); // for debuging
    //      printf ("\n");          // regular
        }
    
        return 0;
    }
    If you have any question, feel free to ask.
    Last edited by WoodSTokk; 07-02-2016 at 10:29 PM.
    Other have classes, we are class

  2. #17
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Quote Originally Posted by Tien Nguyen View Post
    Jesus Christ! Like I said it is a hard problem. I had to use not only my 4 year equivalency but also my training when I was 9 years old with graphics using matrixs and such.
    If this problem too hard for you, then don't fill up this forum with helpless text.
    I have never learned programming at school. I learned by my self al the way and have corrected this program in less then 30 minutes.
    Please read about pointers and struct, because (like jimblumberg sayed before), its very helpfull and one of the important core feature of C.
    Other have classes, we are class

  3. #18
    Registered User
    Join Date
    Jul 2016
    Posts
    10
    Quote Originally Posted by WoodSTokk View Post
    As i can see on line 55, you want the returned value from 'onMap' output as an character.
    So, the function 'onMap' should return a char.
    There for, i have edited the struct and the function as follow:

    If you have any question, feel free to ask.
    Thank you! This really cleared up the way scanf works and some of the other issues I had with my program. Also ignore Tien I think he is a troll.

  4. #19
    Registered User
    Join Date
    Jul 2016
    Posts
    10
    Quote Originally Posted by jimblumberg View Post
    The following line is incorrect and should cause a compile error:
    Code:
    coordinates[count].floor = tile[(strlen(tile)-1)];
    You can't use assignment with C-strings you need to copy the strings with something like strcpy().


    I made a few changes to your program to work on just the file reading.


    Jim
    Thank you Jim for cleaning up the code and taking the time to give me direction/comments. I was able to complete the program with your help. Thanks again for taking the time!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. && not working as expected
    By TonyBalony in forum C Programming
    Replies: 4
    Last Post: 12-14-2011, 12:30 PM
  2. Program not working as expected
    By perrrson in forum C Programming
    Replies: 3
    Last Post: 10-02-2010, 01:49 PM
  3. Pipes not working as expected
    By Shadow_Fiend in forum C Programming
    Replies: 1
    Last Post: 05-16-2010, 05:55 AM
  4. IntersectRect() not working as expected?
    By dxfoo in forum Windows Programming
    Replies: 1
    Last Post: 09-05-2006, 04:52 PM
  5. cin.get(); doesn't seem to be working as expected
    By Diablo84 in forum C++ Programming
    Replies: 5
    Last Post: 03-30-2005, 07:00 PM

Tags for this Thread