Since I had a little time, and I never used isdigit myself
I made a very basic example of using it.

int main ()
{
  char CHAR[]="h"; // then change this to a digit

  if (isdigit(CHAR[0]))
  {
    cout << CHAR[0] << " is a digit" << endl;
    cin.get();
  }
  else
  {
    cout << CHAR[0] << " is not a digit" << endl;
    cin.get();
  }
  return 0;
}

WaltP

I would appreciate it if you would help me to fix the mistakes I have made. But in fact, you are not helping.

commented: When you're hostile to help, help won't be given. -2

I really don't know wheather I will reach on that extend of writing a program of 410 lines. Good work

I would appreciate it if you would help me to fix the mistakes I have made. But in fact, you are not helping.

Really? Than take the fricking advices!
Look at Suzie999 example and learn how to use the isdigit function, it would help you validate the input and see if it's formed only from digits, or if there are strings in it. Also, use this to find more about validating things.

Lucaci Andrew

You aren't helping me either. Only Suzie999 and NathanOliver have helped me.

To Suzie999 and NathanOliver: thanks for the help. :-D

commented: You sir are a lost cause. +0

Hi markwiering,

Some hints to point you in the right direction: look up (here or wherever, e.g. Google or your C++ reference) cin.fail(), cin.clear() and cin.ignore().

Good luck with your coding.

Suzie999

Thanks for the help, but char can only hold one character. In my program I have to enter more than one character. I tried your method, but when I enter 1997, the program simply will cut of the 199 and only sees the 7.

Which code do I have to use so the program is able to see at least 4 characters and won't crash if I enter a letter instead of a value?

strlen() can be used to find the length of the entered string.
and a simple loop can check it.

Which code do I have to use so the program is able to see at least 4 characters and won't crash if I enter a letter instead of a value?

Your problem is that cin goes into a failed state when it gets the unexpected input (e.g. letters when it's expecting an integer value). The advice I posted earlier will enable you to detect when cin goes into a failed state (cin.fail), clear the fail state (cin.clear), and get rid of whatever characters are left in the stream (cin.ignore).

Technically you can, as an alternative, store and parse the input, and there are times when parsing input can be worthwhile, but you don't need to for simple input like this.

If you don't understand the explanation about cin going into a failed state, that's fine. Just look up cin.fail(), cin.clear(), and cin.ignore(). It'll take you 5 minutes and you will have your solution.

commented: Thanks! With your advise, I was able to update my program. Now the program won't allow you to enter characters or symbols when values are required. +1

Here is the updated version of my Age Calculator with a few improvements:

  • The program won't allow you to enter characters or symbols when values are required.
  • Two bugs have been fixed.
  1. The program asks you the wrong question when you enter an invalid current day. FIXED!
  2. In a few situations, the program shows you the wrong days. FIXED!

http://www.daniweb.com/software-development/cpp/threads/434926/updates-version-age-calculator#post1867334

Bob
Thanks! Your advise helped me to update my program. Now my program won't allow the user to enter characters or symbols when values are required.

OK OK OK, here is an example of what Suzie said you need to do, you do it yourself...

cout << "In which year where you born? (Example: 1995) ";
cin >> bjaar;
while <check if bjaar is not an integer>{

    cout << "Your entry was not a number, please correct it";
    cin >> bjaar;
}

now what you really need to do is drop the attitude, accept the input of people who YOU acknowledge know more than you (else you would not post in this forum) and start working to better yourself rather than attempt to convince others your then best

No one knows everything and most of what we do have some kind of flaw, but learning and developping our skills is what we call "GROWING UP"

you are considered a troll when you steal time of those who do not have it and are actually out of decency attempting to assist those who need it...

Eagletalon
I already solved that problem. Check my last message on page 2 (this is page 3).

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.