#Need help with something

139 messages · Page 1 of 1 (latest)

austere veldtBOT
#

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.

swift pewter
#

is thier any work around for this

austere veldtBOT
#

<@undefined>

Please Do Not Delete Posts!

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.

mental oriole
#

Close the thread

swift pewter
mental oriole
#

Just post it.

swift pewter
#
{
    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

mental oriole
#

Its because you are returning from main, and once you return the program exits.

swift pewter
mental oriole
#

Create a different function?

#

To validate maze, once it is opened

swift pewter
#

Success return code is 0 -
Bad args return code is 1
Bad file return code is 2
Invalid maze return code is 3

mental oriole
#

If the file doesnt open you return some number if it does open (in else clause) you can validate the maze

swift pewter
mental oriole
#

The return 0 specifies that the program exited normally.

#

You can remove the else clause altogether because it has no impact on flow

swift pewter
#

you are saying this should work fine

mental oriole
#

Yup

swift pewter
#

it gives this outPUT

swift pewter
# mental oriole Yup

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

mental oriole
#

You can put return 0, in the end outside of any if and else

swift pewter
# mental oriole 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))

mental oriole
#

Error?

swift pewter
#

Success return code is 0 - FAIL

#

the test cases fails

mental oriole
#

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.

swift pewter
#

but i want it to do both the checks

#

valid maze also and if the file has opened also

mental oriole
#

Ok so even if file isnt opened you'd still want to check whether if the maze is valid or not?

swift pewter
#

cause the maze is in the text file

mental oriole
#

Thats what im telling you, it doesnt make sense to do both checks even if the first one failed(returned 2).

mental oriole
#

Just like i told you, the return 0 at the end(which means everything goes ok)

mental oriole
#

Should be

swift pewter
#

@inland sandal could you help

inland sandal
swift pewter
#

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

inland sandal
#

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.

swift pewter
swift pewter
inland sandal
#

You need to read the maze after the declaration of the variables that you pass to it.

swift pewter
#
{
    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

inland sandal
#
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.

swift pewter
#

it should return 2 right

#

when the file has not been opened

inland sandal
#

Well, the numbering you assign to failure modes is all down to you.

swift pewter
#

the task says its
Successful running 0
Argument error 1
File error 2
Invalid maze 3

inland sandal
#

Yup, 2 then.

swift pewter
#

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

inland sandal
#

I think I see your problem.

swift pewter
#

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

swift pewter
inland sandal
#

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.

swift pewter
#

see

#

if i do this

#
    {
        return 2;
    }
    else{
        return 0;
    }
    // Validate the maze
    if (!validate_maze(maze, height, width))
    {
        return 3;
    }```
inland sandal
#

Dude, no.

swift pewter
#

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;
}
inland sandal
#

No, no, no.

swift pewter
inland sandal
#

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;
}
inland sandal
#

Why are you calling read_maze_file() twice?

swift pewter
#

and i need it to return 2 for a bad file

inland sandal
#

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.

swift pewter
inland sandal
#

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.

swift pewter
inland sandal
#

Have you posted this question?
Will I find it if I scroll up?

swift pewter
inland sandal
#

Ideally edit your post at the top (the one you deleted) and insert it there.

inland sandal
#

No, the assignment is not wrong.
It also does not provide you with any specific details on how it performs the automated testing.

swift pewter
#

my code should just return the values

inland sandal
#

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?

swift pewter
#

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

inland sandal
#

In what scenario should it return 2?

#

Actually, we can find out.

swift pewter
#

file has a erro

#

then it should return 2

inland sandal
#

Can you read any output from your program while it is subjected to autograder?

swift pewter
inland sandal
#

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.

swift pewter
#

yes could you help

inland sandal
#

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.

swift pewter
#

this seems fine right

inland sandal
#

The game loop surely gets in the way of the automated testing?
Does the autograder inject the WASD movements?

swift pewter
#

!solbed

#

!solved