-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
46 lines (36 loc) · 1.14 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
char getInputYN(char * Message)
{
//While loop in case we have to get input multiple times
while(1) {
//Print the message given to the function
printf("%s", Message);
//Get a character (hopefully y or n)
char Input = getchar();
//Print a new line after the message, otherwise it looks odd
printf("\n");
//Clear the input buffer, otherwise things get printed twice
while ((getchar()) != '\n');
//Check if the Input char is y, Y, n, or N, and return it if it is
if((Input == 'y') || (Input == 'Y')) {
return Input;
}
else if((Input == 'n') || (Input == 'N')) {
return Input;
}
//If the Input char is not what it should be, print a string and retry
else {
printf("Please enter Y or N.\n");
}
}
}
int main()
{
//Find out if the user wants to play a game
char playGame = getInputYN("Would you like to play a game? (Y/N) ");
//Print if the user chose y, Y, n, or N
printf("You chose %c.", playGame);
return 0;
}