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.
stderrto somewhere else? - Some programmer dudeprintString(errString);then (2)printf("%d %s\n",processedMove, errString);andfprintf(stderr, "Move %d is invalid %s\n", processedMove, errString);? Is this intended? - David C. RankinprintMovefunction might write out of bounds (cannot say without seeing a MTR) - M.M