get in put from user as a date

hi all pros ;)
here is my code

1
2
3
4
5
6
7
8
int m,d,y;
while(!CheckDate(m,d,y)){
 cin>>m;
 cout<<"/";
 cin>>d;
 cout<<"/";
 cin>>y;
}

my problem is when user type something like 11/12/1986 instead of 11[pressenter]/12[pressenter]/1986 the loop will never stop! this is my first problem. I used the
scanf("%d/%d/%d",&m,&d,&y);
but this one also fail when the validation returns false.

second problem is let say user enter in right way like 11[pressenter]/12[pressenter]/1986 but the validation fail let say enter month 35, so because of loop he he/she forced to input date again but the previous entered date still shown on screen(cant use system("cls") because of situation of my code -more than 1300 lines ;)-)

that's all :D
Last edited on
Try taking the input as a string. Then split it around the forward slash using the find and substr functions.

Then use atoi() to convert to ints.
You can also use boost::date_time library
http://www.boost.org/doc/libs/1_39_0/doc/html/date_time.html

It's a really powerful library and can automatically parse dates like "11/12/1986".
Thanks guys
regarding to chrisname I did like this :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
while(!CheckDate(m,d,y)){
                        string date,mm,dd,yy;
                        size_t found;
                        cin>>date;
                        found=date.find("/");
                        if(int(found)>0){
                            mm=date.substr(0,found);
                            found=date.find("/",found+1);
                        if(int(found)>0){
                            dd=date.substr((mm.length()+1),found-mm.length()-1);
                            yy=date.substr(mm.length()+dd.length()+2);
                        }
                        }
                        m=atoi(mm.c_str());
                        d=atoi(dd.c_str());
                        y=atoi(yy.c_str());
                      cout<<m<<" "<<d<<" "<<y;
                    }


it works good enough for me but rally don't know how good it is ;)
and for bringing back the courser what can i do? is it a good idea to do like:
1
2
for(int i=0;i<date.size();i++)
    cout<<"\b";


Your code should work fine.

And yes, you could do "\b \b" but I don't know if it will work. \b is just back-space, and doesn't delete anything.
In the articles section is an article on clearing the terminal/console. Try one of the methods in there.

By the way, use getline(file, buffer) for std::strings, e.g. std::getline(std::cin, myStdString);.
Glad to have helped.

Btw, R0mai, I'd never heard of that lib. I think writing your own functions for that is far more fun and satisfying though. One time I was doing a program, I forget what for, and I wrote a function, very similar to his, to use find, substr, atof, etc, to return the decimal equiv of a fraction. Someone on here helped me, actually. Anyway I was all pleased and stuff. Ten minutes later I found a library on Google for fraction handling...
Last edited on
you could do "\b \b" but I don't know if it will work. \b is just back-space,


lol it took half an hour for me to find out this and works like charm .

thanks
chrisname : Yes writing this kind of stuff is pretty fun (I did this kind of date program myself), but when it comes to do more difficult problems, like comparing, subtracting dates (with all the leap years/seconds etc.) it can become frustrating to debug every little problem, and if you don't write your program just for this purpose, it's pretty unnecessary to spend time on it when there is a perfectly working robust easy-to-use library.
Ah, that's a good point.

Nevermind. Glad that worked, Peha.
Topic archived. No new replies allowed.