Hi, I'm having a small hickup with my program. Everything works fine except this bit I'm going to mention.
One of the functions that my program has to do is return to menu when an empty line is entered. as you will see in the code below, this works fine for the perfect square method. but when i do it in the tic tack toe method it doesn't work.
here are my codes(I'm not going to list all the files, just the necessary ones):
Utility files
Code:
void readRestOfLine()
{
int c;
/* Read until the end of the line or end-of-file. */
while ((c = fgetc(stdin)) != '\n' && c != EOF)
;
/* Clear the error and end-of-file flags. */
clearerr(stdin);
}
int getInteger(int* integer, unsigned length, char* prompt, int min, int max)
{
int finished = FALSE;
char tempString[TEMP_STRING_LENGTH + 2];
int tempInteger = 0;
char* endPtr;
/* Continue to interact with the user until the input is valid. */
do
{
/* Provide a custom prompt. */
printf("%s", prompt);
/* Accept input. "+2" is for the \n and \0 characters. */
fgets(tempString, length + 2, stdin);
if( tempString[0] == '\n')
{
return SUCCESS;
}
/* A string that doesn't have a newline character is too long. */
if (tempString[strlen(tempString) - 1] != '\n')
{
printf("Input was too long.\n");
readRestOfLine();
}
else
{
/* Overwrite the \n character with \0. */
tempString[strlen(tempString) - 1] = '\0';
/* Convert string to an integer. */
tempInteger = (int) strtol(tempString, &endPtr, 10);
/* Validate integer result. */
if (strcmp(endPtr, "") != 0)
{
printf("Input was not numeric.\n");
}
else if (tempInteger < min ||tempInteger > max)
{
printf("Input was not within range %d - %d.\n", min, max);
}
else
{
finished = TRUE;
}
}
} while (finished == FALSE);
/* Make the result integer available to calling function. */
*integer = tempInteger;
return SUCCESS;
}
int getString(char* string, unsigned length, char* prompt)
{
int finished = FALSE;
char* tempString;
/* Allocate temporary memory. */
if ((tempString = malloc(sizeof(char) * (length+2))) == NULL)
{
fprintf(stderr, "Fatal error: malloc() failed in getString().\n");
exit(EXIT_FAILURE);
}
/* Continue to interact with the user until the input is valid. */
do
{
/* Provide a custom prompt. */
printf("%s", prompt);
/* Accept input. "+2" is for the \n and \0 characters. */
fgets(tempString, length + 2, stdin);
/* A string that doesn't have a newline character is too long. */
if (tempString[strlen(tempString) - 1] != '\n')
{
printf("Input was too long.\n");
readRestOfLine();
}
else
{
finished = TRUE;
}
} while (finished == FALSE);
/* Overwrite the \n character with \0. */
tempString[strlen(tempString) - 1] = '\0';
/* Make the result string available to calling function. */
strcpy(string, tempString);
/* Deallocate temporary memory. */
free(tempString);
return SUCCESS;
}
methods for menu options:
Code:
void perfectSquares(int * optionStats, int i)
{
int num1 = 0;
float num2;
float num3;
int test2;
int min = 1;
int max = 1000000;
char prompt[PROMPT_LENGTH+1];
printf("Perfect Squares\n");
printf("\n");
sprintf(prompt,"Enter a positive integer (%d - %d): ", min, max);
getInteger(&num1, MAX_INPUT_PS, prompt, min, max);
num2 = sqrt(num1);
test2 = sqrt(num1);
num3 = num2 - test2;
if(num1>0)
{
if(num3 == 0)
{
printf("'");
printf("%d", num1);
printf("'");
printf(" is perfect square!\n");
int num4 = sqrt(num1);
int ps1 = pow(((num4)+1),2);
int ps2 = pow((num4-1),2);
printf("Perfect square before: ");
printf("%d",ps2);
printf("\n");
printf("Perfect square after: ");
printf("%d",ps1);
printf("\n\n");
}
else
{
printf("'");
printf("%d", num1);
printf("'");
printf(" is not a perfect square!\n");
int num4 = sqrt(num1);
int ps1 = pow(((num4)+1),2);
int ps2 = pow((num4-1),2);
printf("Perfect square before: ");
printf("%d",ps2);
printf("\n");
printf("Perfect square after: ");
printf("%d",ps1);
printf("\n\n");
}
}
return;
}
/* See requirement #3 "ASCII to Binary Generator".
*/
void asciiToBinary(int * optionStats, char *string)
{
char string1[INPUT_LENGTH + 1];
char prompt[PROMPT_LENGTH + 1];
int i;
int y;
int x;
int length;
printf("ASCII to Binary Generator \n\n");
sprintf(prompt, "Enter a string (max %d characters): ", INPUT_LENGTH);
getString(string1, INPUT_LENGTH, prompt);
printf("Binary representation: ");
for(i=0; i<strlen(string1); i++)
{
x = (int)string1[i];
for(y = 7; y>=0; y--) {
if((1<<y)&x) printf("1");
else printf("0");
}
printf(" ");
}
printf("\n");
return;
}
/* See requirement #4 "Matching Brackets".
*/
void matchingBrackets(int * optionStats, char *string)
{
}
/* See requirement #5 "Tic Tac Toe Game".
*/
void ticTacToe(int * optionStats)
{
int player = 0;
int winner = 0;
int i;
int min = 1;
int max = 9;
char prompt[PROMPT_LENGTH+1];
int choice =0;
int row = 0;
int column = 0;
int line = 0;
char board[3][3] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'}
};
for(i=0; i<9 && winner == 0; i++)
{
printf("\n\n");
printf("---+---+---\n");
printf(" %c | %c | %c\n", board[0][0], board[0][1], board[0][2]);
printf("---+---+---\n");
printf(" %c | %c | %c\n", board[1][0], board[1][1], board[1][2]);
printf("---+---+---\n");
printf(" %c | %c | %c\n", board[2][0], board[2][1], board[2][2]);
printf("---+---+---\n");
player = i%2 +1;
do
{
sprintf(prompt,"\nPlayer # %d select square: " ,player);
getInteger(&choice, MAX_INPUT_TTT, prompt, player);
row = --choice/3;
column = choice%3;
}
while(choice<0 || choice>9 || board[row][column]>'9');
{
board[row][column] = (player == 1) ? 'X' : '0';
if((board[0][0]==board[1][1] && board[0][0]==board[2][2]) ||
(board[0][2]==board[1][1] && board[0][2]==board[2][0]))
winner = player;
else
for(line = 0; line <=2; line++)
{
if((board[line][0]==board[line][1]&& board[line][0]==board[line][2])||
(board[0][line]==board[1][line] && board[0][line]==board[2][line]))
winner = player;
}
}
}
printf("\n\n");
printf("---+---+---\n");
printf(" %c | %c | %c\n", board[0][0], board[0][1], board[0][2]);
printf("---+---+---\n");
printf(" %c | %c | %c\n", board[1][0], board[1][1], board[1][2]);
printf("---+---+---\n");
printf(" %c | %c | %c\n", board[2][0], board[2][1], board[2][2]);
printf("---+---+---\n");
if(winner == 0)
printf("Draw");
else
printf("Player %d is the winner!", winner);
return;
}
/* See requirement #6 "Formating Text" of the assignment specs.
*/
void formattingText(int * optionStats, int lineLength, char *string)
{
}
/* See requirement #7 "Session summary" of the assignment specs.
* Function sessionSummary() reports the program usage.
*/
void sessionSummary(int * optionStats)
{
}