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.