Last active
December 26, 2015 22:49
-
-
Save mitchfriedman/7226113 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <string.h> | |
#include <time.h> | |
#include <stdlib.h> | |
#include <ctype.h> | |
int GetUserAnswer(int *userAnswer); | |
void PrintMenu(); | |
void FlushStdin(); | |
int DetermineGameMode(int *mode); | |
int GetAdditionCard(); | |
int GetMutliplicationCard(); | |
int main() { | |
int returnCode = 0; | |
int rounds; | |
int gameMode; | |
int correctAnswer; | |
int userAnswer; | |
int score = 0; | |
srand(time(NULL)); | |
/*int numCorrect = 0;*/ | |
printf("Welcome to the Flash Card Game\n"); | |
do { | |
PrintMenu(); | |
returnCode = DetermineGameMode(&gameMode); | |
if(returnCode == 1) { | |
printf("Incorrect Input. Try again.\n"); | |
} | |
} | |
while(returnCode == 1); | |
if(gameMode == 3) { | |
printf("Thanks for playing. Goodbye.\n"); | |
return(0); | |
} | |
else { | |
returnCode = 0; | |
for(rounds = 0; rounds < 6; rounds++) { | |
printf("Your current score is %d out of %d\n", score, rounds); | |
if(gameMode == 2) { | |
correctAnswer = GetAdditionCard(); | |
do { | |
if(returnCode == -1) { | |
printf("Incorrect Input. Try again.\n"); | |
} | |
returnCode = GetUserAnswer(&userAnswer); | |
} | |
while(returnCode == -1); | |
if(userAnswer == correctAnswer) { | |
score++; | |
printf("Correct!\n"); | |
} | |
else { | |
printf("Incorrect\n"); | |
} | |
} | |
if(gameMode == 1) { | |
correctAnswer = GetMutliplicationCard(); | |
do { | |
if(returnCode == -1) { | |
printf("Incorrect Input. Try again. \n"); | |
} | |
returnCode = GetUserAnswer(&userAnswer); | |
} | |
while(returnCode == -1); | |
if(userAnswer == correctAnswer) { | |
score++; | |
printf("Correct!\n"); | |
} | |
else { | |
printf("Incorrect\n"); | |
} | |
} | |
} | |
} | |
return(0); | |
} | |
void PrintMenu() { | |
printf("Please select an option from the following menu: \n"); | |
printf("(m)ultiplication\n"); | |
printf("(a)ddition\n"); | |
printf("(q)uit\n"); | |
} | |
int GetUserAnswer(int *userAnswer) { | |
char inputString[4]; | |
char *endPointer; | |
fgets(inputString, sizeof(inputString), stdin); | |
*userAnswer = strtol(inputString, &endPointer, 10); | |
if(strlen(inputString)>3) { | |
FlushStdin(); | |
} | |
if(*inputString != '\0' && *endPointer == '\0') { | |
return 1; | |
} | |
return 0; | |
} | |
int DetermineGameMode(int *mode) { | |
char inputString; | |
int error = 0; | |
inputString = getchar(); | |
if(inputString == 'm') { | |
*mode = 1; | |
} | |
else if(inputString == 'a') { | |
*mode = 2; | |
} | |
else if(inputString == 'q') { | |
*mode = 3; | |
} | |
else { | |
error = 1; | |
*mode = 0; | |
} | |
FlushStdin(); | |
return error; | |
} | |
int GetAdditionCard() { | |
int numberOne, numberTwo, sum; | |
numberOne = rand() % 9; | |
numberTwo = rand() % 9; | |
sum = numberOne + numberTwo; | |
printf("What is %d + %d?\n", numberOne, numberTwo); | |
return(sum); | |
} | |
int GetMutliplicationCard() { | |
int numberOne, numberTwo, product; | |
numberOne = rand() % 9; | |
numberTwo = rand() % 9; | |
product = numberOne * numberTwo; | |
printf("What is %d X %d?\n", numberOne, numberTwo); | |
return(product); | |
} | |
void FlushStdin() /* function to clear the input buffer from "standard in" */ | |
{ | |
char nextChar = 'a'; /* set the nextchar equal to anything at the beginning */ | |
nextChar = getchar(); | |
/* loop through the standard in buffer and "get" each character to remove | |
them from the buffer */ | |
while (nextChar != '\n') | |
{ | |
nextChar = getchar(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment