Thread: Program not working as expected

  1. #1
    Registered User
    Join Date
    Jul 2016
    Posts
    10

    Program not working as expected

    So I have this program that analyzes a file that has entries of coordinates and then puts them in to an array of structs and then prints out a map of either blank spaces represented by "." or a character if there is a corresponding set of coordinates in the input file. Currently the output is printing $ signs and only a handful of the tiles that it should. I know there is a lot that is probably wrong in my code (this is my first program) but any advice/guidance on what is going wrong would be helpful. Thanks

    Code:
    struct COORD{
    int xV;
    int yV;
    char *floor;
    };
    
    
    char* onMap(int x, int y, struct COORD z[]){
        int arraySize = sizeof(z);
        int i;
        char *empty = ".";
    
    
        for(i=0; i<arraySize; i++){
            if(z[i].xV == x && z[i].yV == y){
                return z[i].floor;
            }
        }
        return empty;
    }
    
    
    int main(){
    
    
        FILE *input = fopen("C:\\Users\\John\\Desktop\\input.txt","r");
    
    
        int x;
        int y;
    
    
        int count = 0;
        char tile[5];
        struct COORD coordinates[50];
    
    
        while (fscanf(input, "%i %i %s", &x, &y, tile) != EOF) {                //creates an struct array of all map positions
                    coordinates[count].xV = x;
                    coordinates[count].yV = y;
                    coordinates[count].floor = tile[(strlen(tile)-1)];
                    count++;
                    while(fgetc(input) != '\n'){};
        }
    
    
        int yCoor =1;
        int i;
        bool mapping = true;
    
    
        while(mapping)
        {
            for(i = 1; i<31; i++){
                printf("%c", onMap(i, yCoor, coordinates));
                if(i==30 && yCoor<30){
                    printf("%i||\n", yCoor);
                    yCoor++;
                    i=0;
                }//increases the x value & resets y
                if(yCoor==30 && i==30){
                    mapping = false;
                }
            }
        }
       return 0;
    }
    Last edited by xants; 07-02-2016 at 08:13 PM.

  2. #2
    Registered User
    Join Date
    Jul 2016
    Posts
    10
    This is what the current output looks like.
    Program not working as expected-output-png

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Please post a sample of your input file.

    Where are you allocating memory for that pointer inside your structure?



    Jim

  4. #4
    Registered User
    Join Date
    Jul 2016
    Posts
    10
    Hey Jim, honestly I don't know where I am allocating the memory. Here is what the input file is like:

    1 20 @@
    2 21 @A
    3 22 @#
    4 23 @1
    5 22 @@
    6 22 @@
    7 22 @@
    8 22 @@
    9 23 @Z Here be trolls – not!
    10 23 @+
    12 23 @@
    13 24 @@
    11 22 @@
    14 22 @2
    15 21 @1
    16 20 @@
    17 19 @@
    18 20 @@
    19 19 @@
    20 18 @@
    21 17 @*
    22 16 @*
    23 15 @%
    24 14 @7
    25 13 @9
    26 12 @-
    27 11 @(
    28 10 @)
    29 9 @"
    14 23 @!
    30 8 @^
    22 18 @@
    23 18 @@
    23 19 @@
    24 19 @@
    25 20 @@
    26 21 @@
    28 11 @@
    28 12 @@
    28 13 @@
    27 14 @@
    26 15 @Z

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I don't know where I am allocating the memory.
    Did you write the program?

    You need to allocate memory for that pointer. Why are you using a pointer instead statically allocated C-string?

    What do the different values in your file represent?

    What do you expect your output to look like with that input file?


    Jim

  6. #6
    Registered User
    Join Date
    Jul 2016
    Posts
    10
    Quote Originally Posted by jimblumberg View Post
    Did you write the program?

    You need to allocate memory for that pointer. Why are you using a pointer instead statically allocated C-string?

    What do the different values in your file represent?

    What do you expect your output to look like with that input file?


    Jim
    1. Yes I did write the program, not really sure how pointers work, if you are talking about the struct COORD coordinates line I am not sure how to dynamically reallocate memory. I think with the realloc() function it could work but not sure how to implement it.

    2. The second character of the @@ represents a tile on a 30x30 grid at the coordinates that precede the two characters.

    3. i & yCoor is the tile that is actively being printed, x&y and tile are the temp values that fscanf is picking up and placing into the COORD coordinates array, onMap compares the array with the active coordinates (i/yCoor) and determines if a blank space should be printed or a special character should be printed.

    4. I expect this:

    ..............................
    ..............................
    ..............................
    ..............................
    ..............................
    ..............................
    ..............................
    .............................^
    ............................".
    ...........................)..
    ..........................(@..
    .........................-.@..
    ........................9..@..
    .......................7..@...
    ......................%..Z....
    .....................*........
    ....................*.........
    ...................@.@@.......
    ................@.@...@@......
    @..............@.@......@.....
    .A............1..........@....
    ..#.@@@@[email protected]................
    ...1....Z+.@.!................
    ............@.................
    ..............................
    ..............................
    ..............................
    ..............................
    ..............................
    ..............................

    Thanks for taking the time to go over this.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    My suggestion is that you print your array of structure, after you read the file, to insure you're actually reading the file correctly.

    By the way what compiler are you using and what #include files are you including?

    Does your program even compile? It fails to compile on my machine.

    Jim

  8. #8
    Registered User
    Join Date
    Jul 2016
    Posts
    10
    Quote Originally Posted by jimblumberg View Post
    My suggestion is that you print your array of structure, after you read the file, to insure you're actually reading the file correctly.

    By the way what compiler are you using and what #include files are you including?

    Does your program even compile? It fails to compile on my machine.

    Jim
    I am using Code::Blocks and

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <stdbool.h>

    these are the include files. When I created a little for loop to print out my coordinates array everything was accurate so I am pretty sure I am picking up the right information, it is just the function onMap and the while loop at the bottom that isn't working properly. I figured out why the $ sign was being printed and corrected that.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    When I created a little for loop to print out my coordinates array everything was accurate so I am pretty sure I am picking up the right information
    Where did you print out this information? Please show the code.

    Jim

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    As I said I can't reproduce your problem because the program doesn't even compile on my machine. You should be getting some warnings if not errors from your compiler that need to be fixed.

    ||=== Build: Debug in test.c (compiler: GNU GCC Compiler) ===|
    main.c||In function ‘main’:|
    main.c|45|error: assignment makes pointer from integer without a cast|
    main.c|61|warning: format ‘%c’ expects argument of type ‘int’, but argument 2 has type ‘char *’ [-Wformat=]|
    ||=== Build failed: 1 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|
    And please post your current code that contains that little print loop.


    Jim

  11. #11
    Registered User
    Join Date
    Jul 2016
    Posts
    10
    I deleted it a while ago, but now when I try and recode it it doesn't seem to work... it was something along the lines of

    Code:
    for(j = 0; j<sizeof(coordinates);j++){
            printf("%i    %i     %s\n", coordinates[j].xV, coordinates[j].yV, coordinates[j].floor);
        }

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Post your current code, all of it!

    You should be using "counter" not "sizeof(coordinates)" in that for loop, counter holds the number of records read.

    Jim

  13. #13
    Registered User
    Join Date
    Jul 2016
    Posts
    10
    Quote Originally Posted by jimblumberg View Post
    Post your current code, all of it!

    You should be using "counter" not "sizeof(coordinates)" in that for loop, counter holds the number of records read.

    Jim
    That is all my code currently, I deleted all my test loops... feeling really stupid now. I dont think the array is even being filled.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <stdbool.h>
    
    
    struct COORD{
    int xV;
    int yV;
    char *floor;
    };
    
    
    
    
    char *onMap(int x, int y, struct COORD z[]){
        int arraySize = sizeof(z);
        int i;
    
    
        for(i=0; i<arraySize; i++){
            if(z[i].xV == x && z[i].yV == y){
                return z[i].floor;
            }
        }
        return ".";
    }
    
    
    
    
    int main(){
        FILE *input = fopen("C:\\Users\\John\\Desktop\\input.txt","r");
        int x;
        int y;
    
    
        int count = 0;
        char tile[5];
        struct COORD coordinates[50];
    
    
        while (fscanf(input, "%i %i %s", &x, &y, tile) != EOF) {  //creates an struct array of all map positions
                    coordinates[count].xV = x;
                    coordinates[count].yV = y;
                    coordinates[count].floor = tile[(strlen(tile)-1)];
                    count++;
                    while(fgetc(input) != '\n'){};
        }
    
    
        int j;
        for(j = 0; j<count; j++){
            printf("%i    %i     %s\n", coordinates[j].xV, coordinates[j].yV, coordinates[j].floor);
        }
    
    
    
    
        int yCoor =1;
        int i;
        bool mapping = true;
    
    
    
    
        while(mapping)
        {
            for(i = 1; i<31; i++){
                printf("%s", onMap(i, yCoor, coordinates));
                if(i==30 && yCoor<30){
                    printf("%i||\n", yCoor);
                    yCoor++;
                    i=0;
                }//increases the x value & resets y
                if(yCoor==30 && i==30){
                    mapping = false;
                }
            }
        }
       return 0;
    }

  14. #14
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    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.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <stdbool.h>
    
    struct COORD
    {
        int xV;
        int yV;
        char floor[5];  ///// Using a statically allocated array instead of that pointer (to avoid needing to use dynamic memory allocation).
    };
    
    int main() {
        FILE *input = fopen("input.txt", "r");
    
        int count = 0;
        struct COORD coordinates[50];
    
        // No need for all of those "temporary" variables, just scan the items
        // directly into the structure variables. Also not that I used the optional
        // width specifier on your string to insure you never overflow the string.
        while((fscanf(input, "%i %i %4s", &coordinates[count].xV,
                                         &coordinates[count].yV,
                                         coordinates[count].floor) != EOF) &&
                                         count < 50)  // Notice this addition, to insure you don't overflow your array.
        {   //creates an struct array of all map positions
            count++;
            // Skip everything else on the line.
            while(fgetc(input) != '\n') {};
        }
    
        for(int j = 0; j < count; j++)
        {
            printf("%i    %i     %s\n", coordinates[j].xV, coordinates[j].yV, coordinates[j].floor);
        }
    
        return 0;
    }

    Jim

  15. #15
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Also note that there are several problems in the code that I removed. Once you understand the changes I made to the code I presented it may be easier to understand the rest of the problems.

    By the way your compiler should be warning you about the problems, if it isn't generating any warnings you need to increase your compiler warning levels.

    Jim

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