When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
139 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
is thier any work around for this
<@undefined>
Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. In the future you can use !solved to close a post and mark a post as solved.
Close the thread
hey i need some help
Just post it.
alright this is my maze game file
{
if (argc != 2)
{
printf("Usage: %s <maze_file>\n", argv[0]);
return 1;
}
char maze[MAX_HEIGHT][MAX_WIDTH];
int height = 0, width = 0;
int player_row, player_col;
if (!read_maze_file(argv[1], maze, &height, &width, &player_row, &player_col))
{
return 2;
}
else
{
return 0;
}
// Validate the maze
if (!validate_maze(maze, height, width))
{
return 3;
}
the issue here is
i need to return 0 if the file has been opened successfully
but if i return 0 , it does not return 3 is the maze is not valid in the file
and if i remove the return 0 statement , the return 3 statment works fine but the test case for if the file has been opened succefully fails as it doesnt return 0
Its because you are returning from main, and once you return the program exits.
yes but i cant think of any other way to do it
so i can create a new function and check all the 3 things
Success return code is 0 -
Bad args return code is 1
Bad file return code is 2
Invalid maze return code is 3
If the file doesnt open you return some number if it does open (in else clause) you can validate the maze
alright then when should i return 0 if i put that in the else case
You dont need to
The return 0 specifies that the program exited normally.
You can remove the else clause altogether because it has no impact on flow
if (!read_maze_file(argv[1], maze, &height, &width, &player_row, &player_col))
{
return 2;
}
// Validate the maze
if (!validate_maze(maze, height, width))
{
return 3;
}
you are saying this should work fine
Yup
it gives this outPUT
Success return code is 0 - FAIL
Bad args return code is 1 - PASS
Bad file return code is 2 - Error opening file: No such file or directory
PASS
Invalid maze return code is 3 - PASS
and when i put the return 0 in else condition it says Success return code is 0 - PASS
and Invalid maze return code is 3 - FAIL
You can put return 0, in the end outside of any if and else
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("Usage: %s <maze_file>\n", argv[0]);
return 1;
}
char maze[MAX_HEIGHT][MAX_WIDTH];
int height = 0, width = 0;
int player_row, player_col;
if (!read_maze_file(argv[1], maze, &height, &width, &player_row, &player_col))
{
return 2;
}
// Validate the maze
if (!validate_maze(maze, height, width))
{
return 3;
}
// Game loop
char direction;
int print_maze_flag = 0; // Flag to control maze printing
printf("Use WASD keys to move. Press 'M' to see the maze.\n");
while (maze[player_row][player_col] != 'E')
{
if (print_maze_flag) // Print maze only if the flag is set
{
print_maze(maze, height, width, player_row, player_col);
}
printf("Enter direction (W/A/S/D) or 'M' to see the maze: ");
scanf(" %c", &direction);
if (direction == 'M' || direction == 'm')
{
print_maze_flag = 1; // Set the flag to print the maze
continue;
}
print_maze_flag = 0; // Reset the flag to prevent printing the maze
move_player(maze, height, width, &player_row, &player_col, direction);
}
// Player reached exit
printf("Congratulations! You have reached the exit.\n");
return 0;
}
see i have a return 0 at the end of the main function
it still gives the error , the only way it passes if i return 0 in the else condition of if (!read_maze_file(argv[1], maze, &height, &width, &player_row, &player_col))
Error?
the error meaning
Success return code is 0 - FAIL
the test cases fails
It (your old version) checks if the file has opened if it has it just returns 0, which means there is no additional processing the program just closes after checking if the file has opened or not.
yes
but i want it to do both the checks
valid maze also and if the file has opened also
Ok so even if file isnt opened you'd still want to check whether if the maze is valid or not?
no without opening the file how will it check if the maze is valid or not right?
cause the maze is in the text file
Thats what im telling you, it doesnt make sense to do both checks even if the first one failed(returned 2).
then how should i go about it
Just like i told you, the return 0 at the end(which means everything goes ok)
so this should be fine
.
?
@mental oriole
Should be
doenst work still says failed
@inland sandal could you help
Sorry, late to the party.
What fails? Building the code?
Or does it fail at runtime?
hello friend , my test case fails
if i remove the return 0 statement return 3 works but return 0 doesnt
and if i keep both only return 0 passes and return 3 fails
Success return code is 0 - FAIL
Bad args return code is 1 - PASS
Bad file return code is 2 - PASS
Invalid maze return code is 3 - PASS
Should you not...
int res = read_maze_file(argv[1], maze, &height, &width, &player_row, &player_col);
if (res != 0)
{
return res;
}
etc. ?
If you were to set your compiler to super pedantic, then it would object to you casting an int return type to a boolean expression.
am i not supposed to add this in main\
You need to read the maze after the declaration of the variables that you pass to it.
{
FILE *file = fopen(filename, "r");
if (!file)
{
perror("Error opening file");
return 0;
}
else
{
printf("File opened successfully.\n");
}
*height = 0;
*width = 0;
// Read maze from file
char ch;
while ((ch = fgetc(file)) != EOF)
{
if (ch == '\n')
{
(*height)++;
*width = 0;
}
else
{
maze[*height][*width] = ch;
if (ch == 'S')
{
*player_row = *height;
*player_col = *width;
}
(*width)++;
}
}
// Check if the last line needs to be processed
if (*width > 0)
{
(*height)++; // Increment height if the last line is not empty
}
fclose(file);
return 1;
}``` or should i add it in my read file
so it should be added in this function
because this is where they have been declared
if (!file)
{
perror("Error opening file");
return 0;
}
This is unconventional.
A function return an int - or an enum - would typically treat zero as "all good".
You returning zero when it fails to open the file goes against that convention.
Well, the numbering you assign to failure modes is all down to you.
the task says its
Successful running 0
Argument error 1
File error 2
Invalid maze 3
Yup, 2 then.
yes but even before doing this my text case 2 was passing
when i changed it to 2 it failed it was working fine before cause in my main function i am returning 2
my issue here is
I think I see your problem.
if the file opens succefully it should return 0 and return 3 if its a invalid maze
and return 2 if its a bad file
yes what is it
Try returning 1 when it fails to open the file.
BUT, notice the final return statement in read_maze_file().
That seems wrong to me.
see
if i do this
{
return 2;
}
else{
return 0;
}
// Validate the maze
if (!validate_maze(maze, height, width))
{
return 3;
}```
Dude, no.
it gives this output
but if i do this
if (!read_maze_file(argv[1], maze, &height, &width, &player_row, &player_col))
{
return 2;
}
// Validate the maze
if (!validate_maze(maze, height, width))
{
return 3;
}
No, no, no.
In your main()...
int res = read_maze_file(argv[1], maze, &height, &width, &player_row, &player_col);
if (res != 0)
{
return res;
}
As said here: #1235379913301950515 message
Let read_maze_file() decide what the error code is. Not your main().
Inside read_maze_file() you do
int read_maze_file(char *filename, char maze[MAX_HEIGHT][MAX_WIDTH], int *height, int *width, int *player_row, int *player_col)
{
FILE *file = fopen(filename, "r");
if (!file)
{
perror("Error opening file");
return 1; // bad argument
}
// other code, then finally
return 0;
}
ill try this
this is the main
Why are you calling read_maze_file() twice?
cause the second if returns 2 right
and i need it to return 2 for a bad file
No, that "2" would happen if the file did exist, but contained dud data.
I haven't seen you return 2 from read_maze_file().
But in any case, you call the function just once.
okay ill get rid of that if condition
That "Submission Checker" will call your program several times, each time with different parameters.
Those success codes are from each run.
It's not your aim to make all these codes happen from one single run.
That would be impossible.
so you are saying the question is wrong
Have you posted this question?
Will I find it if I scroll up?
i can dm it to you
Ideally edit your post at the top (the one you deleted) and insert it there.
oh okay
No, the assignment is not wrong.
It also does not provide you with any specific details on how it performs the automated testing.
the automated testing is done by the autograder
my code should just return the values
But only ONE.
It can return only one value.
So.... to test all possible return values, it makes sense that the autograder will run your program multiple times.
Right?
it should return 0 if the maze is valid and the file has opened
and it should return 3 if the file has opened and maze is inval;id
yes
if the file has not opened at all
file has a erro
then it should return 2
Can you read any output from your program while it is subjected to autograder?
no we cant only pass and fail
The assignment does not clearly say what you should return in what scenario.
Yet the autograder clearly has a set of configurations and expects very specific return values for these.
That makes yours a guessing game.
Example: if the file does not exist, is that an argument error? Or a file error?
We can get there.
But it will be trial-and-error.
yes could you help
Well, only so far.
Either way, all your function calls from main() should follow the same style as I have explained before...
int res = read_maze_file(argv[1], maze, &height, &width, &player_row, &player_col);
if (res != 0)
{
return res;
}
// Validate the maze
res = validate_maze(maze, height, width);
if (res != 0)
{
return res;
}
The main() function itself should just return 0 at the very end.
this seems fine right
The game loop surely gets in the way of the automated testing?
Does the autograder inject the WASD movements?