Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Lesson 7 of 8
Mini Project Part 2 - Number Guessing Game
Build a complete number guessing game that combines all control flow constructs: `do-while` for the game loop guaranteeing at least one guess, `if/else if/else` for hint feedback, `break` to exit on win, input validation with `while` for bad scanf, and a replay loop. This mini-project demonstrates how control flow interacts with I/O in a real program.
Number Guessing Game - Complete Implementation
/* guess.c - number guessing game using all control flow */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MIN_VAL 1
#define MAX_VAL 100
#define MAX_TRIES 7
/* Clear stdin after bad input */
static void clear_input(void) {
int c;
while ((c = getchar()) != '
' && c != EOF) { /* drain */ }
}
int main(void) {
srand((unsigned int)time(NULL)); /* seed once at startup */
char play_again;
do { /* outer do-while - replay loop */
int secret = MIN_VAL + rand() % (MAX_VAL - MIN_VAL + 1);
int tries = 0;
int won = 0;
printf("
=== Guess the number (%d-%d, %d tries) ===
",
MIN_VAL, MAX_VAL, MAX_TRIES);
while (tries < MAX_TRIES) { /* inner while - guessing loop */
int guess;
printf("Guess #%d: ", tries + 1);
if (scanf("%d", &guess) != 1) {
printf("Invalid input - enter a number
");
clear_input();
continue; /* skip the rest, retry this iteration */
}
tries++;
if (guess < MIN_VAL || guess > MAX_VAL) {
printf("Out of range! Enter %d-%d
", MIN_VAL, MAX_VAL);
tries--; /* don't count invalid range as a try */
continue;
}
if (guess == secret) {
printf("Correct! You got it in %d tries!
", tries);
won = 1;
break; /* exit guessing loop */
} else if (guess < secret) {
printf("Too low! (%d tries left)
", MAX_TRIES - tries);
} else {
printf("Too high! (%d tries left)
", MAX_TRIES - tries);
}
}
if (!won) {
printf("Out of tries! The number was %d
", secret);
}
printf("Play again? (y/n): ");
clear_input(); /* flush any leftover newline */
if (scanf(" %c", &play_again) != 1) play_again = 'n';
} while (play_again == 'y' || play_again == 'Y');
printf("Thanks for playing!
");
return 0;
}
/* Build: gcc -Wall -Wextra -std=c99 guess.c -o guess
Run: ./guess */Common Mistakes
- Not clearing the input buffer after failed `scanf` - when `scanf("%d", &guess)` fails (user types 'abc'), the bad characters stay in the buffer. The next `scanf` immediately reads the same bad input and fails again - infinite loop. Always `clear_input()` after a failed read.
- Not seeding `rand` - `rand()` without `srand(time(NULL))` always produces the same sequence. Seed once at program startup, not inside a loop (seeding in a loop resets the sequence).
- Off-by-one in the range - `rand() % MAX_VAL` gives 0 to MAX_VAL-1. For a range [MIN, MAX] inclusive: `MIN + rand() % (MAX - MIN + 1)`.
Tip
Tip
Practice Mini Project Part 2 Number Guessing Game in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Game loop combines random number seeding, conditional branch hints, and guess counters
Practice Task
Note
Practice Task - (1) Write a working example of Mini Project Part 2 Number Guessing Game from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Mini Project Part 2 Number Guessing Game is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready c code.
Key Takeaways
- Build a complete number guessing game that combines all control flow constructs: `do-while` for the game loop guaranteeing at least one guess, `if/else if/else` for hint feedback, `break` to exit on win, input validation with `while` for bad scanf, and a replay loop.
- Not clearing the input buffer after failed `scanf` - when `scanf("%d", &guess)` fails (user types 'abc'), the bad characters stay in the buffer. The next `scanf` immediately reads the same bad input and fails again - infinite loop. Always `clear_input()` after a failed read.
- Not seeding `rand` - `rand()` without `srand(time(NULL))` always produces the same sequence. Seed once at program startup, not inside a loop (seeding in a loop resets the sequence).
- Off-by-one in the range - `rand() % MAX_VAL` gives 0 to MAX_VAL-1. For a range [MIN, MAX] inclusive: `MIN + rand() % (MAX - MIN + 1)`.
Finished this lesson?