0

So my fifteen.c code compiles just fine and runs the .txt files with no issue. The code returns a "ftw!" and exit code 0 when executing the program with those .txt files. The issue is when I run the check50 test script, I get this error:

:( fifteen.c compiles. expected exit code 0, not 1

The only info I'm given is:

Log running sed -i='' '/#include /a \ #define usleep(x)' fifteen.c... running clang -std=c11 -S -std=c99 -Wall -Werror -Wno-deprecated-declarations fifteen.c -o fifteen.S... checking that program exited with status 0...

My fifteen.c code is as follows:

#define _XOPEN_SOURCE 500

#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>

#include "helper.h"

// constants
#define DIM_MIN 3
#define DIM_MAX 9

int main(int argc, string argv[])
{

// ensure proper usage
if (argc != 2)
{
    printf("Usage: fifteen d\n");
    return 1;
}

// ensure valid dimensions
int d = atoi(argv[1]);
    // initialize the board

if (d < DIM_MIN || d > DIM_MAX)
{
    printf("Board must be between %i x %i and %i x %i, inclusive.\n",
        DIM_MIN, DIM_MIN, DIM_MAX, DIM_MAX);
    return 2;
}

// open log

FILE *file = fopen("log.txt", "w");
if (file == NULL)
{
    return 3;
}

// greet user with instructions
greet();

int board[d][d]; //generic 2D array definition
int zero[2]; //this holds the location of the zero {i, j}

init(d, board, zero); //BOOM this works

// accept moves until game is won
while (true)
{
    // clear the screen
    clear(); //this is already implemented

    // draw the current state of the board
    draw(d, board, zero); //BOOM appears to work

    // log the current state of the board (for testing)
    for (int i = 0; i < d; i++)
    {
        for (int j = 0; j < d; j++)
        {
            fprintf(file, "%i", board[i][j]);
            if (j < d - 1)
            {
                fprintf(file, "|");
            }
        }
        fprintf(file, "\n");
    }
    fflush(file);

    // check for win
    if (won(d, board)) //in theory this works
    {
        printf("ftw!\n");
        return 0;
    }

    // prompt for move
    printf("Tile to move: ");
    int tile = get_int();

    // quit if user inputs 0 (for testing)
    if (tile == 0)
    {
        break;
    }

    // log move (for testing)
    fprintf(file, "%i\n", tile);
    fflush(file);

    // move if possible, else report illegality
    if (!move(tile, d, board, zero))
    {
        printf("\nIllegal move.\n");
        sleep(1);
    }

    // sleep thread for animation's sake
    sleep(1);
}

// close log
fclose(file);

// success
return 0;
}

I can include my helper.c and helper.h files if anyone thinks they may be the problem. Doing some reading, one person had an issue where they had a private function 'drawHorizontalLine' that was causing this error, but I do not have this function. I also read that check50 for this program replaces the 'draw' function, and mine currently only prints the numbers, nothing else fancy (and passes the .txt file tests, returning 0 for a win).

Does anything stand out as to why I'm getting this error? The log seems to indicate that it may be something to do the 'sleep()' function, but I've tried commenting that out and it still doesn't compile with check50.

As mentioned earlier, the log says the following:

-Wno-deprecated-declarations

And I'm wondering if I have some deprecated declarations I'm unaware of, according to the check50 script. Thanks to anyone in advance, and I apologize for the lengthy post.

UPDATE: Cliff B was correct, you cannot change the int main() function at all. I ended up including one extra function, which obviously required another prototype, but that was fine and the check50 script worked fine. Also, because board[][] and d were declared as global variables, the init(), draw(), etc., prototypes did not require alteration at all (note that altering the prototypes originally required altering int main() function calls). Just a tip for anyone scratching their head the way I was.

1

The fifteen.c file was distributed in the pset and was not supposed to be altered in any way. The only file that should be changed is helpers.c. Did you alter fifteen.c? Clearly, yes. That explains the error message. It isn't saying that your program executes and returns 1 or 0, it says that it didn't compile, so the compiler returned 1.

The compile failed because check50 only uploads helpers.c and compiled using your helpers.c and the original fifteen.c and helpers.h. Your helpers.c doesn't work with the other original files.

Time to go back and redo the problem. fifteen.c needs to remain unchanged and all of your coding needs to be done in helpers.c.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

| improve this answer | |
  • Thanks Cliff. One follow-on question: the problem set explicitly said that we could alter the function prototypes. Given this, I'm then assuming I need to keep all 'board' and 'zero' variables declared and isolated to the helper.c file? – Joe Post Dec 1 '17 at 1:08
  • That would be best. It really isn't necessary to alter them in order to make them work. While the instructions say you can change them, it usually leads to problems. Guess that's why fifteen won't be part of the course next year. ;-) – Cliff B Dec 1 '17 at 1:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .