Ignore certain character

Hey guys, I just wanted to know how I can ignore certain input from users. For example, I am writing a program to calculate how many days the user has been alive for and I want the users to input their birthdays in this format (MM/DD/YYYY). How can I tell c++ to ignore the '/' character?

Thanks!
Take a string and then verify the date format. You need to strip out the / chars
so cin.ignore('/') ?
You can either ask the user to insert the Month first, then the day and finally the year (thus, separately... best option in my opinion if you're working without GUI)

Or, you can take an entire string in input and make some checks on it (like, correct month, correct day and correct year...)

I would usually take in the information separately, but I am taking it in all at once for practice purposes. It's also slightly more convenient for the user I believe.
Something like this should be adequate:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

int main()
{
    int month, day, year ;
    const char forward_slash = '/' ;
    char delimiter1, delimiter2 ;

    std::cout << "enter birth date in the form MM/DD/YYYY: " ;
    std::cin >> month >> delimiter1 >> day >> delimiter2 >> year ;

    if( delimiter1 == forward_slash && delimiter2 == forward_slash )
    {
        // validate values for year, month, day

        // calculate how many days the user has been alive

        // print result
    }

    else std::cerr << "invalid format\n" ;
}
The whole fact the user is using a console and not a Window with the proper GUI is already less convenient for both you and the user.

But since it's for practice purposes, then you'll want to work with string manipulation.

1
2
3
string date;
cout << "Please insert a date in this format MM/DD/YYYY";
cin >> date;


Nothing forbids the user to type something out of the rules, like 99-0343.12

Well, you have multiple situations to take into account.

The most obvious ones are:

1) The month is correct ( values from 1 to 12 )
2) The day is correct ( values from 1 to 31 )
3) The year is correct ( values less than or equal to the current year )

Let's say you come up with a solution to these 3 problems. Now the user can freely type

10-04/2016

You see the user entered both '-' and '/' to separate day,month and year.

You can choose to both give an error if this occurs or just to ignore them as long as the date itself is correct
gedamial wrote:
The whole fact the user is using a console and not a Window with the proper GUI is already less convenient for both you and the user.

You're wrong.

@NathanLong
Read all user input as a string. Then you can parse it for validity. A simple enough way would be to do something like JLBorges suggests; just be a little less strict on input. For example, compare that (delimiter1==delimiter2) and that delimiter1 is one of / or - or whatever else you'll accept.

Hope this helps.

@Duoas I'm not. Wonder why everything has been moved to GUI applications. All starts from Operating Systems themselves, that once were like MS-DOS command line interface based. The same applies for common software applications based on command line interface.

It's less convenient from various perspectives. Of course, to learn the language in-depth is super-effective and most recommended way to do it.
Believe what you want.
Topic archived. No new replies allowed.