There's this game that I play and there's a terminal application that lets you control elevator doors. I've decided to recreate it in C for fun.
I have a text file (floors.txt) that looks like this:
0
0
1
2
2
2
2
x
and the idea for this particular part of the code is to look through this list and go ok: floor zero is 0, that means unlocked,print that. Now there is a newline, print a "\n". Then floor 1 is unlocked, newline, floor 2 is state 1 - locked, newline, etc. My code for making this work is:
FILE *floorpointer;
char floorstat;
and later on,
floorpointer = fopen("floors.txt", "r");
while ((floorstat = fgetc(floorpointer)) != EOF)
{
if (floorstat = '\n')
{
printf("\n");
}
else if (floorstat = '0')
{
printf("\tfloor <>: unlocked");
}
else if (floorstat = '1')
{
printf("\tfloor <>: locked");
}
else if (floorstat = '2')
{
printf("\tfloor <>: locked - requires code");
}
else if (floorstat = 'x')
{
printf("\n\tselect floor to reconfigure [0-<>]: ");
}
else
{
printf("something has gone terribly wrong! debug time!");
}
}
(the <> are just placeholders, ignore that.) So, the problem is that instead of getting
floor <>: unlocked
floor <>: unlocked
floor <>: locked
floor <>: locked - requires code
floor <>: locked - requires code
floor <>: locked - requires code
floor <>: locked - requires code
select floor to reconfigure [0-<>]:
...I get 16 newlines. I've replaced everything between the while brackets with printf("%c", floorstat), and It works correctly, printing the contents of floors.txt. I'm not entirely sure what the problem is, but maybe I'm just approaching it from entirely the wrong angle. Any help would be appreciated.
Aucun commentaire:
Enregistrer un commentaire