| | |
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:
Solved Threads: 0
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:
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)
struct Date { int d; int m; int y; } Date getDate() { string str_date; Date date; do { cout << "\nEnter DOB (DDMMYYYY): "; getline(cin, str_date); cout << str_date.length(); // sanity check if (str_date.length()==0) break; if (atoi(str_date.c_str()) && (str_date.length() == 8)) break; } while (!atoi(str_date.c_str()) || (str_date.length() != 8)); // Date OK so save 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()); return date; }
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.
>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.
•
•
Join Date: Nov 2007
Posts: 26
Reputation:
Solved Threads: 0
•
•
•
•
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.
•
•
Join Date: Nov 2007
Posts: 26
Reputation:
Solved Threads: 0
•
•
•
•
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.
is the recursive function the if inside the do...while?
>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.
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.
•
•
Join Date: Nov 2007
Posts: 26
Reputation:
Solved Threads: 0
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;
mohanrobin ignored.
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;
c Syntax (Toggle Plain Text)
if (str_date.length()==0) { date.d = 0; date.m = 0; date.y = 0; return date; }
mohanrobin ignored.
>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 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.
![]() |
Similar Threads
- Julian date to Calender Date (Java)
- convert date(string) to date (date time) (JavaScript / DHTML / AJAX)
- Validate the input date by comparing it with current year using Date function Date(Y) (PHP)
- Microsoft Access date validation Need a solution (Database Design)
- How to get auto calculated expiry date on lost focus event of registration date (ASP.NET)
- problem again (C)
- Can Someone please check my project & comment/tips (C)
- parsing date strings mm/dd/yy?? (C++)
Other Threads in the C++ Forum
- Previous Thread: Network programing using ACE framework and C++
- Next Thread: C++ function to check validity of sudoku
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






