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:

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;
}

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?

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.

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.

>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?

That's working but if the idea is a bad one , let me know :-)

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?

>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.

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]

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;

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.