~fkfd/fcc

9763af02995c0fae47b8a3a75d68e74b1ad66450 — Frederick Yin 1 year, 4 days ago 2269b94
Handle invalid input
2 files changed, 7 insertions(+), 13 deletions(-)

M quiz.c
M quiz.h
M quiz.c => quiz.c +7 -12
@@ 39,11 39,6 @@ void quiz(const char *pool_fp, const char *log_fp, const bool random_mode,
		}
		const int answer = ask_question(question);
		if (answer == QUIT) break;
		if (answer == INVALID) {
			// ask this question again
			i--;
			continue;
		}
		history.attempts++;
		if (answer == CORRECT) {
			history.hits++;


@@ 62,13 57,9 @@ int ask_question(const question_t question) {
	const char correct = 'A' + question.correct;
	char choice = getchar();
	while (getchar() != '\n');
	// convert to upper case
	if (choice >= 'a' && choice <= 'z') {
	if ((choice >= 'a' && choice <= 'd') || choice == 'q') {
		// convert to upper case
		choice -= 32;
	} else if (choice < 'A' || choice > 'Z') {
		// invalid
		printf(RED "Invalid\n" RESET);
		return INVALID;
	}
	if (choice == 'Q') {
		printf(YELLOW "Quit\n" RESET);


@@ 77,13 68,17 @@ int ask_question(const question_t question) {
	if (choice == correct) {
		printf(GREEN "Correct!\n\n" RESET);
		return CORRECT;
	} else {
	} else if (choice >= 'A' && choice <= 'D') {
		printf(RED "Incorrect\n" RESET);
		printf(GREEN "The correct answer is: %c. %s\n\n" RESET, correct,
		       question.answers[question.correct]);
		// allow user to read correct answer
		while (getchar() != '\n');
		return INCORRECT;
	} else {
		// invalid answer; ask again
		printf(RED "Invalid answer, try again\n" RESET);
		return ask_question(question);
	}
}


M quiz.h => quiz.h +0 -1
@@ 10,7 10,6 @@
enum {
	CORRECT,
	INCORRECT,
	INVALID,
	QUIT,
};