c 2b 2b birthday program

Solutions on MaxInterview for c 2b 2b birthday program by the best coders in the world

showing results for - "c 2b 2b birthday program"
Elisa
30 May 2020
1#include <iostream>
2#include <string>
3
4int main() {
5
6    const char dash = '-' ; // expected separator
7    const int current_year = 2019 ;
8
9    int month ;
10    int day ;
11    int year ;
12    char separator ; // to capture the separator between input flds
13
14    std::cout << "Please enter your birth date (mm-dd-yyyy): " ;
15
16    if( std::cin >> month && month > 0 && month < 13 && // valid month [1,12]
17        std::cin >> separator && separator == dash && // valid separator
18        std::cin >> day && day > 0 && day < 32 && // valid day [1,31]
19        std::cin >> separator && separator == dash && // valid separator
20        std::cin >> year && year > 1800 && year <= current_year ) // valid year [1800,current_year]
21    {
22        // valid input: print name of month (Jan == 1)
23        const std::string month_names[] = // look up table containing names
24        {
25            "", // 0 is not used
26            "January", // 1
27            "February", // 2
28            "March",
29            "April",
30            "May",
31            "June",
32            "July",
33            "August",
34            "September",
35            "October",
36            "November",
37            "December" // 12
38        };
39
40        std::cout << "your birth day is in the month of " << month_names[month] << '\n' ;
41    }
42
43    else std::cout << "Invalid Date!\n" ;
44}