0
votes

I am writing a C program that takes in a text file describing a game of Solitaire. The program will then take in a set of moves, described by the user in the text file, and go through the moves, modifying the state of the game.

Currently, I am processing the moves, if there is an invalid move, I stop a while loop, and print to standard error the move in the format "Move M is illegal: (move)".

char* errString = malloc(sizeof(char) * 10);
errString[9] = '\0';
printString(errString);
int processedMove = 0;
int somethingWrong = 1;


while (processedMove < movesStack.size) {

    somethingWrong = processMove(movesStack.cards[processedMove].rank, movesStack.cards[processedMove].suit, &clubsFoundationStack, &diamondsFoundationStack, &heartsFoundationStack, &spadesFoundationStack, &colSevenDown, &colSixDown, &colFiveDown, &colThreeDown, &colFourDown, &colTwoDown, &colOneDown, &colSevenUp, &colSixUp, &colFiveUp, &colThreeUp, &colFourUp, &colTwoUp, &colOneUp, &stockDown, &stockUp, &limitValue, &turnValue);

    if (somethingWrong != 1) {


        printMove(movesStack.cards[processedMove].rank, movesStack.cards[processedMove].suit, &errString);
        printString(errString);
        processedMove++;
        printf("%d %s\n",processedMove, errString);
        fprintf(stderr, "Move %d is invalid %s\n", processed Move, errString);

        break;
    }
    processedMove++;
}

The above is in my main method. printString will be given below, it simply prints the given string.

processMove, takes the stacks of cards and process a move, it will return -1 if the move is invalid, and -2 if the move has a formatting error.

printMove, takes a rank, and suit, and a string, and writes the error to the given string, this would be printed in the fprintf statement.

After running the above code, I am left with this output, you can see the first call of printString(errString), followed by the second call, after errString has been modified by printMove function. Then finally you see the printf statement, which prints the value of processedMove and errString.


Commencing the printing of the string with indices
        c[0]: h
        c[1]: o
        c[2]: 
Commencing the printing of the string on one line
        ho

Commencing the printing of the string with indices
        c[0]: 5
        c[1]: -
        c[2]: >
        c[3]: 2
Commencing the printing of the string on one line
        5->2
6 5->2

the function printString

void printString(char* c) {
    if (c == NULL) {
        printf("string is null\n");
        return;
    }
    printf("\nCommencing the printing of the string with indices\n");
    for (int i = 0; i < strlen(c); i++) {
        if (c[i] == '\n') {
            printf("        c[%d]: newline\n", i);
            continue;
        }

        printf("        c[%d]: %c\n", i, c[i]);
    }

    printf("Commencing the printing of the string on one line \n");
    printf("        ");
    for (int i = 0; i < strlen(c); i++) {
        if (c[i] == '\n' || c[i] == ' ') {
            continue;
        }
        printf("%c", c[i]);
    }
    printf("\n");


}

and the function printMove

void printMove(char f, char s, char** errString) {
    char* ret = malloc(sizeof(char) * strlen(*errString));
    if (f == '.' && s == '.') {
        ret[0] = '.';
        ret[1] = '\0';

    }
    else if (f == 'r' && s == 'r') {
        ret[0] = 'r';
        ret[1] = '\0';

    }
    else {
        ret[0] = f;
        ret[1] = '-';
        ret[2] = '>';
        ret[3] = s;
        ret[4] = '\0';
    }

    *errString = ret;
}

Thank you for your time, and any solution is welcome.

1
The code you show won't build. Please edit your question to include a minimal reproducible example. - Some programmer dude
As for your problem, how do you run your program? In what environment? If you run in an IDE perhaps it's redirecting stderr to somewhere else? - Some programmer dude
I am using Visual Studio, I run it by compiling it in cmd prompt and then running the executable through command prompt, - cmfeltz
It looks like you are outputting three times, (1) printString(errString); then (2) printf("%d %s\n",processedMove, errString); and fprintf(stderr, "Move %d is invalid %s\n", processedMove, errString);? Is this intended? - David C. Rankin
The printMove function might write out of bounds (cannot say without seeing a MTR) - M.M

1 Answers

-2
votes

So the problem here is, that it you cannot use your own variables in a stream to stderr.