Birthdate program using the MM-DD-YYYY

Hi! how do I remove the "-" sign when I run my output? We were told to create a program using the switch selector that will ask the user to enter an integer format of his/her birthdate in the following condition:
• The month should not exceed to 12.
• The number of day should not exceed to 31.
• The year must range from 180 to present.
Otherwise, the program should return “Invalid Date!”
After evaluating the program should give the exact equivalent word of the integer month.


#include <iostream>
using namespace std;

int main () {

int month, day, year;

cout<<"Please enter your Birthdate (mm-dd-yyyy):";
cin>>month>>day>>year;

switch (month){
case 1:
cout<<"\nYour birthday is January"<<day<<"," <<year<<" "<<endl;
break;
case 2:
cout<<"\nYour birthday is February"<<day<<"," <<year<<" "<<endl;
break;
case 3:
cout<<"\nYour birthday is March"<<day<<"," <<year<<" "<<endl;
break;
case 4:
cout<<"\nYour birthday is April"<<day<<"," <<year<<" "<<endl;
break;
case 5:
cout<<"\nYour birthday is May"<<day<<"," <<year<<" "<<endl;
break;
case 6:
cout<<"\nYour birthday is June"<<day<<"," <<year<<" "<<endl;
break;
case 7:
cout<<"\nYour birthday is July"<<day<<"," <<year<<" "<<endl;
break;
case 8:
cout<<"\nYour birthday is August"<<day<<"," <<year<<" "<<endl;
break;
case 9:
cout<<"\nYour birthday is September"<<day<<"," <<year<<" "<<endl;
break;
case 10:
cout<<"\nYour birthday is October"<<day<<"," <<year<<" "<<endl;
break;
case 11:
cout<<"\nYour birthday is November"<<day<<"," <<year<<" "<<endl;
break;
case 12:
cout<<"\nYour birthday is December"<<day<<"," <<year<<" "<<endl;
break;
}


if (month>=12)
cout<<"\nInvalid Month!";
else if (day>=31)
cout<<"\nInvalid Day!";
else if (year>=1980)
cout<<"\nInvalid Year!";

return 0;
}

Ignoring the other issues with your code (also, why didn't you post your code in the code blocks this forum's new post template provided for you?)...

You'll probably want to use something like std::cin.ignore
http://www.cplusplus.com/reference/istream/istream/ignore/#example

You could also use std::getline to read in strings (with '-' as the delimiter) and then use std::stoi to parse them, though that's a bit more involved.

Finally, if you want a really dirty solution (which I wouldn't recommend), just multiply your day and year by -1.

-Albatross

EDIT: Forgot an std::.
Last edited on
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
#include <iostream>
#include <string>

int main() {

    const char dash = '-' ; // expected separator
    const int current_year = 2019 ;

    int month ;
    int day ;
    int year ;
    char separator ; // to capture the separator between input flds

    std::cout << "Please enter your birth date (mm-dd-yyyy): " ;

    if( std::cin >> month && month > 0 && month < 13 && // valid month [1,12]
        std::cin >> separator && separator == dash && // valid separator
        std::cin >> day && day > 0 && day < 32 && // valid day [1,31]
        std::cin >> separator && separator == dash && // valid separator
        std::cin >> year && year > 1800 && year <= current_year ) // valid year [1800,current_year]
    {
        // valid input: print name of month (Jan == 1)
        const std::string month_names[] = // look up table containing names
        {
            "", // 0 is not used
            "January", // 1
            "February", // 2
            "March",
            "April",
            "May",
            "June",
            "July",
            "August",
            "September",
            "October",
            "November",
            "December" // 12
        };

        std::cout << "your birth day is in the month of " << month_names[month] << '\n' ;
    }

    else std::cout << "Invalid Date!\n" ;
}
We were told to create a program using the switch selector

Study the solution from JLBorges, then modify it to use a switch statement to print the month names.

Note that in the real world, his solution is better than a switch statement. It also demonstrates something very useful: sometimes it's easier to solve a problem with data than with code.
@albataross oh sorry I don't know how to post my code block on this forum and I was in a hurry. Sorry bout that anyways, I'm gonna try your suggestion. Thank you!
Last edited on
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
#include <iostream>
#include <string>
using namespace std;

bool isValidRange( int n, int mn, int mx ) { return n >= mn && n <= mx; }

int main()
{
   const string months[13] = { "", "January", "February", "March", "April", "May", "June",
                                   "July", "August", "September", "October", "November", "December" };
   unsigned int month, day, year;
   char dash;

   cout << "Please enter your birthdate in the format mm-dd-yyyy: ";     // grrr, American order
   cin >> month >> dash >> day >> dash >> year;

   if ( isValidRange( month,    1, 12   ) &&
        isValidRange( day  ,    1, 31   ) &&
        isValidRange( year , 1800, 2019 ) )
   {
       cout << "Your birthday is " << months[month] << " " << day << ", " << year << "\n";
   }
   else
   {
       cout << "Invalid Date!";
   }
}
does this work? abs is in <cmath> and is std:: namespace.

cout<<"\nYour birthday is June"<<abs(day)<<"," <<abs(year)<<" "<<endl;
Last edited on
It will work if there are no spaces between the minus sign and the number.
Input of 11-23-1999 is fine; but 11 - 23 - 1999 would fail.
Topic archived. No new replies allowed.