A7 - conditionals and loops
#include <stdio.h>
int main() {
int selection;
int exit = 0;
while (exit == 0) {
printf("hello world!\n");
printf("1. start\n");
printf("2. settings\n");
printf("3. most guesses\n");
printf("4. exit\n\n");
printf("please select an option: ");
scanf("%d", &selection);
printf("you selected option %d\n", selection);
if (selection == 1) {
printf("starting...\n");
} else if (selection == 2) {
printf("settings...\n");
} else if (selection == 3) {
printf("most guesses...\n");
} else if (selection == 4) {
printf("exiting...\n");
exit = 1;
} else if (selection < 1 || selection > 4) {
printf("invalid option, please select again: ");
scanf("%d", &selection);
printf("you selected option %d\n", selection);
}
}
return exit;
}
- the if block above can be replaced by a switch case block:
switch (selection)
{
case 1:
printf("starting...\n");
break; // break is necessary to prevent fall-through
case 2:
printf("settings...\n");
break;
case 3:
printf("most guesses...\n");
break;
case 4:
printf("exiting...\n");
exit = 1;
break;
default:
printf("invalid option, please try again.\n");
break; // for consistency
- to implement checking in the input,
scanf line should be replaced with:
do {
inputStatus = scanf("%d", &selection);
} while (
inputStatus != 1 &&
(tempValue = getchar()) != EOF &&
(tempValue = getchar()) != '\n'
);
- this can be abstracted away into another function:
int obtainNumericUserInput() {
int inputStatus, tempValue, selection;
do {
inputStatus = scanf("%d", &selection);
} while (
inputStatus != 1 &&
(tempValue = getchar()) != EOF &&
(tempValue = getchar()) != '\n'
);
return selection;
}
- this function can be called as:
selection = obtainNumericUserInput();
- the function must also be declared at the top
#include <stdio.h>
// function declaration
int obtainNumericUserInput();
int main() {
int selection;
int exit = 0;
while (exit == 0) {
...
selection = obtainNumericUserInput();
printf("you selected option %d\n", selection);
switch (selection)
{
...
}
return exit;
}
int obtainNumericUserInput() {
int inputStatus, tempValue, selection;
do {
inputStatus = scanf("%d", &selection);
} while (
inputStatus != 1 &&
(tempValue = getchar()) != EOF &&
(tempValue = getchar()) != '\n'
);
return selection;
}
- now adding functionality for the other options:
#include <stdio.h> // for printf and scanf
#include <stdlib.h> // for malloc and rand
#include <conio.h> // for getch
// function declaration
int obtainNumericUserInput();
int* getMostGuessesList(int numberofGuesses);
void displayMostGuesses();
void displaySettings(int *minimumValue, int *maximumValue);
void playGame(int, int); // parameter names can be added later
int getRandomNumber(int min, int max);
// constant definition
#define NUM_MOST_GUESSES 10
int main() {
int selection;
int exit = 0;
int minimumValue = 1, maximumValue = 35; // default values
while (exit == 0) {
printf("hello world!\n");
printf("1. start\n");
printf("2. settings\n");
printf("3. most guesses\n");
printf("4. exit\n\n");
printf("please select an option: ");
selection = obtainNumericUserInput();
printf("you selected option %d\n", selection);
switch (selection)
{
case 1:
printf("starting...\n");
break; // break is necessary to prevent fall-through
case 2:
displaySettings(&minimumValue, &maximumValue); // pass by reference to modify the original values
break;
case 3:
displayMostGuesses();
break;
case 4:
exit = 1;
break;
default:
printf("invalid option, please try again.\n");
break; // for consistency
}
}
return exit;
}
int obtainNumericUserInput() {
int inputStatus, tempValue, selection;
do {
inputStatus = scanf("%d", &selection);
} while (
inputStatus != 1 &&
(tempValue = getchar()) != EOF &&
(tempValue = getchar()) != '\n'
);
return selection;
}
int* getMostGuessesList(int numberofGuesses) {
int* guesses = (int*) malloc(numberofGuesses * sizeof(int)); // allocate memory for the array
int miniumNumber = 2, maximumNumber = 35;
for (int i = 0; i < numberofGuesses; i++) { // (initialization; condition; increment)
// generate random number between miniumNumber and maximumNumber
guesses[i] = getRandomNumber(miniumNumber, maximumNumber);
}
return guesses;
}
void displayMostGuesses() {
printf("displaying most guesses...\n");
int* mostGuesses = getMostGuessesList(NUM_MOST_GUESSES);
for(int i = NUM_MOST_GUESSES - 1; i >= 0; i--) { // display in reverse order
printf("%d. %d guesses\n", i + 1, mostGuesses[i]);
}
printf("press a key to return to main menu...");
getch(); // wait for user input
}
void displaySettings(int *minimumValue, int *maximumValue) { // pass by reference to modify the original values
int selection, backtoMainMenu = 0;
printf("displaying settings...\n");
printf("1. set minimum value, current value: %d\n", *minimumValue);
printf("2. set maximum value, current value: %d\n", *maximumValue);
printf("3. return to main menu\n");
printf("please select an option: ");
selection = obtainNumericUserInput();
switch (selection)
{
case 1:
printf("enter new minimum value: ");
int newMinimumValue = obtainNumericUserInput();
if (newMinimumValue < maximumValue && newMinimumValue > 0) {
minimumValue = newMinimumValue;
} else {
printf("invalid minimum value. the minimum value must be greater than 0 and less than the maximum value.\n");
}
break;
case 2:
printf("enter new maximum value: ");
int newMaximumValue = obtainNumericUserInput();
if (newMaximumValue > minimumValue) {
maximumValue = newMaximumValue;
} else {
printf("invalid maximum value. the maximum value must be greater than the minimum value.\n");
}
break;
case 3:
backtoMainMenu = 1;
break;
default:
printf("invalid option, please try again.\n");
break;
}
printf("press a key to return to main menu...");
getch(); // wait for user input
}
void playGame(int minimumValue, int maximumValue) {
printf("playing game with range %d to %d...\n", minimumValue, maximumValue);
int cheatModeOn = 1;
int maximumNumberofGuesses = 3, playerNumberofGuesses = 0;
int currentPlayerAnswer = 0, closeGuessRange = 5, gameWon = 0;
int correctAnswer = getRandomNumber(minimumValue, maximumValue);
for(int i = 0; i < maximumNumberofGuesses; i++) {
if(cheatModeOn) {
printf("cheat mode: the correct answer is %d\n", correctAnswer);
}
printf("you have %d guesses remaining. please enter your guess: ", maximumNumberofGuesses - i);
currentPlayerAnswer = obtainNumericUserInput();
playerNumberofGuesses++;
if (currentPlayerAnswer == correctAnswer) {
printf("congratulations! you guessed the correct answer %d in %d guesses!\n", correctAnswer, playerNumberofGuesses);
gameWon = 1;
break; // exit the loop if the game is won
} else if (currentPlayerAnswer < correctAnswer && currentPlayerAnswer >= correctAnswer - closeGuessRange) {
printf("your guess is too low, but you're very close!\n");
} else if (currentPlayerAnswer < correctAnswer) {
printf("your guess is too low.\n");
} else if (currentPlayerAnswer > correctAnswer && currentPlayerAnswer <= correctAnswer + closeGuessRange) {
printf("your guess is too high, but you're very close!\n");
} else if (currentPlayerAnswer > correctAnswer) {
printf("your guess is too high.\n");
}
}
}
int getRandomNumber(int min, int max) {
return (rand() % (max - min + 1)) + min; // generate random number between min and max
}