944,056 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 9877
  • C++ RSS
Nov 21st, 2007
0

Check for valid date, fine if no date entered

Expand Post »
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:
c++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Light Poster
alcoheca is offline Offline
26 posts
since Nov 2007
Nov 21st, 2007
0

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

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?
Reputation Points: 10
Solved Threads: 0
Light Poster
alcoheca is offline Offline
26 posts
since Nov 2007
Nov 21st, 2007
0

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

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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 21st, 2007
0

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

Click to Expand / Collapse  Quote originally posted by Narue ...
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.

Click to Expand / Collapse  Quote originally posted by Narue ...
>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?
Reputation Points: 10
Solved Threads: 0
Light Poster
alcoheca is offline Offline
26 posts
since Nov 2007
Nov 21st, 2007
0

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

That's working but if the idea is a bad one , let me know :-)
Reputation Points: 10
Solved Threads: 0
Light Poster
alcoheca is offline Offline
26 posts
since Nov 2007
Nov 21st, 2007
0

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

Click to Expand / Collapse  Quote originally posted by mohanrobin ...
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?
Reputation Points: 10
Solved Threads: 0
Light Poster
alcoheca is offline Offline
26 posts
since Nov 2007
Nov 21st, 2007
0

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

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 21st, 2007
0

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

Click to Expand / Collapse  Quote originally posted by alcoheca ...
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 Nick Evan; Nov 21st, 2007 at 10:27 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Nov 21st, 2007
0

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

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
alcoheca is offline Offline
26 posts
since Nov 2007
Nov 21st, 2007
0

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

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Network programing using ACE framework and C++
Next Thread in C++ Forum Timeline: C++ function to check validity of sudoku





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC