forked from portfoliocourses/c-example-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput_validation.c
More file actions
35 lines (29 loc) · 963 Bytes
/
Copy pathinput_validation.c
File metadata and controls
35 lines (29 loc) · 963 Bytes
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
/*******************************************************************************
*
* Program: Input Validation Example
*
* Description: Example of performing input validation with a do-while loop in C.
*
* YouTube Lesson: https://www.youtube.com/watch?v=IDIJXsZRqP4
*
* Author: Kevin Browne @ https://portfoliocourses.com
*
*******************************************************************************/
#include <stdio.h>
int main()
{
int day = 0;
do
{
// ask the user to enter a day between 1-31
printf("Enter day of the month (1-31): ");
scanf("%d", &day);
// if the day is out of range, inform the user
if (day < 1 || day > 31)
printf("Error day must be between 1-31\n");
// keep asking the user to enter a day until they enter one that is in range
} while (day < 1 || day > 31);
// output the day of the month once a valid day has been entered
printf("Day of the month: %d\n", day);
return 0;
}