Check for valid date, fine if no date entered

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Nov 2007
Posts: 26
Reputation: alcoheca is an unknown quantity at this point 
Solved Threads: 0
alcoheca alcoheca is offline Offline
Light Poster

Check for valid date, fine if no date entered

 
0
  #1
Nov 21st, 2007
hi,
I'm checking a date is valid as it's typed in, however I always want to allow the user to skip with a CR.

The while condition is causing my program to crash when str_date.length()==0 (I think)

here's my code:
  1. struct Date {
  2. int d;
  3. int m;
  4. int y;
  5. }
  6.  
  7. Date getDate() {
  8. string str_date;
  9. Date date;
  10. do {
  11. cout << "\nEnter DOB (DDMMYYYY): ";
  12. getline(cin, str_date);
  13. cout << str_date.length(); // sanity check
  14. if (str_date.length()==0) break;
  15. if (atoi(str_date.c_str()) && (str_date.length() == 8)) break;
  16. } while (!atoi(str_date.c_str()) || (str_date.length() != 8));
  17.  
  18. // Date OK so save
  19. date.d = atoi(str_date.substr(0,2).c_str());
  20. date.m = atoi(str_date.substr(2,3).c_str());
  21. date.y = atoi(str_date.substr(4,7).c_str());
  22. return date;
  23. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 26
Reputation: alcoheca is an unknown quantity at this point 
Solved Threads: 0
alcoheca alcoheca is offline Offline
Light Poster

Re: Check for valid date, fine if no date entered

 
0
  #2
Nov 21st, 2007
Thinking the problem is maybe trying to return a Date which hasn't any values. So maybe I should replace the first break, with a return - but how would I return a blank date?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 714
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Check for valid date, fine if no date entered

 
0
  #3
Nov 21st, 2007
Your code shouldn't compile at all because you haven't terminated the structure definition with a semicolon.

>date.d = atoi(str_date.substr(0,2).c_str());
>date.m = atoi(str_date.substr(2,3).c_str());
>date.y = atoi(str_date.substr(4,7).c_str());
substr doesn't use a begin/end pattern, it uses a begin/count pattern. The second argument should be 2, 2, and 4 for those three lines, respectively.

>The while condition is causing my program to crash when str_date.length()==0 (I think)
You think correctly. You're breaking from the loop when the string is empty and then you proceed to access the nonexistent elements of the string with your calls to substr.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 26
Reputation: alcoheca is an unknown quantity at this point 
Solved Threads: 0
alcoheca alcoheca is offline Offline
Light Poster

Re: Check for valid date, fine if no date entered

 
0
  #4
Nov 21st, 2007
Originally Posted by Narue View Post
Your code shouldn't compile at all because you haven't terminated the structure definition with a semicolon.
sorry my mistake when typing it in.

Originally Posted by Narue View Post
>date.d = atoi(str_date.substr(0,2).c_str());
>date.m = atoi(str_date.substr(2,3).c_str());
>date.y = atoi(str_date.substr(4,7).c_str());
substr doesn't use a begin/end pattern, it uses a begin/count pattern. The second argument should be 2, 2, and 4 for those three lines, respectively.

>The while condition is causing my program to crash when str_date.length()==0 (I think)
You think correctly. You're breaking from the loop when the string is empty and then you proceed to access the nonexistent elements of the string with your calls to substr.
OK so I will return a date of 00000000 instead of the first break, does that seem like the correct way out of this?
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 26
Reputation: alcoheca is an unknown quantity at this point 
Solved Threads: 0
alcoheca alcoheca is offline Offline
Light Poster

Re: Check for valid date, fine if no date entered

 
0
  #5
Nov 21st, 2007
That's working but if the idea is a bad one , let me know :-)
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 26
Reputation: alcoheca is an unknown quantity at this point 
Solved Threads: 0
alcoheca alcoheca is offline Offline
Light Poster

Re: Check for valid date, fine if no date entered

 
0
  #6
Nov 21st, 2007
Originally Posted by mohanrobin View Post
hi .i am mohan .i ama newbie to this website.any way thank you for entering into my site.
recursive functions are functions with in functions.in your source code the format is not correct ,there is no values for entering the dat.so pls study and try it .good luck.
What site are you talking about?

is the recursive function the if inside the do...while?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 714
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Check for valid date, fine if no date entered

 
0
  #7
Nov 21st, 2007
>That's working but if the idea is a bad one , let me know :-)
It's not my job to hold your hand. If you're not confident enough in your code, then rewrite it until you are.

>What site are you talking about?
>is the recursive function the if inside the do...while?
Ignore mohanrobin. He's just spouting nonsense.
Last edited by Narue; Nov 21st, 2007 at 10:23 am.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,834
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 297
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Roasting Maven

Re: Check for valid date, fine if no date entered

 
0
  #8
Nov 21st, 2007
Originally Posted by alcoheca View Post
What site are you talking about?

is the recursive function the if inside the do...while?
Never mind him, most of his posts don't make sense..

[edit]too slow... [/edit]
Last edited by niek_e; Nov 21st, 2007 at 10:27 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 26
Reputation: alcoheca is an unknown quantity at this point 
Solved Threads: 0
alcoheca alcoheca is offline Offline
Light Poster

Re: Check for valid date, fine if no date entered

 
0
  #9
Nov 21st, 2007
HI Narue,

Sorry I didn't mean to ask you to hold my hand.
For now I'll leave it as it is and see how it bears under testing.

I just shoved this in place of the 1st if(...) break;
  1. if (str_date.length()==0) {
  2. date.d = 0;
  3. date.m = 0;
  4. date.y = 0;
  5. return date;
  6. }

mohanrobin ignored.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 714
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Check for valid date, fine if no date entered

 
0
  #10
Nov 21st, 2007
>Sorry I didn't mean to ask you to hold my hand.
I know you didn't, but confidence isn't something that I can give you. If you only rely on other people to tell you if your solutions are good or bad, you'll never step out of the beginner/intermediate stage. As a programmer you have complete control over how a problem is solved. Often there are multiple solutions that are equally good, and it's up to you to make a decision. Second guessing yourself will hurt you far more than learning from a poor decision because it stunts your growth.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC