Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

isdigit()

Status
Not open for further replies.

ecannizzo

Programmer
Sep 19, 2000
213
US
I am using this code:

printf("Please enter the month as a number> ");
scanf("%d", &entered_month);

entered_month is declared as an integer. If the user puts a letter or string when asked this question, the program goes wacko on me. I tried validating using isdigit(), but then I get and error message from the system. Can anyone help?

Thanks!
Erica
 
You could also use an ascii comparison

int myIsDigit(char* dig)
{
if(strlen(dig) == 0)
return 1;

if(*dig >= '0' && *dig<='9')
return myIsDigit(dig+1);

return 0;
}

if you want to include decimal points in the future just add a comparison to the if

Matt
 
Pretty elegant approach on that Matt!
Looks really nice, but a while cycle should do the trick as well...
To make it even more nicer, i would say:
Code:
 if (*dig == NULL)
instead a call to strlen... [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
I am confused. I'm not sure what you're telling me to do. Sorry I'm new to C. This is my code for that piece.

do {
printf(&quot;Please enter the month as a number> &quot;);
scanf(&quot;%d&quot;, &entered_month);
} while ((!isdigit(entered_month) || (entered_month > 12) || (entered_month) < 1));

But the isdigit() doesn't work. Is what your saying something I can do in this situation?

Erica
 
Ok...
You actually try to use isdigit function on a multiple digits number in the first place.
Second, if you enter a string and expecting an integer, i think the entered_month param will have a random value (or 0, i'm not sure about that), so the best approach would be to initialize your data ( and you should do that ALWAYS, as a rule) so, saying
Code:
entered_month = 0
at the beginning of the loop might be helpfull.
Second of all, drop the isdigit thing, that only works for characters, even if the function takes an INT as input parameter.

you should change the code like this:
Code:
entered_month = 0;
do {
   printf(&quot;Please enter the month as a number> &quot;);
   scanf(&quot;%d&quot;, &entered_month);
} while ((entered_month > 12) || (entered_month) < 1));

HTH... [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
Thanks, but my problem is when you input a letter or string to the question asked, my program bombs on me so I need a way of testing to make sure they entered and integer and not a character or string. And if they entered a character/string, I need to tell them and have them reanswer the question.

Please help.

Erica
 
Like i said, the scanf function does not assign any value to the variable if something wrong was typed (like in your case). So, initialize the value of the variable and maybe change the loop like:

Code:
bool continue = true;
int em = 0;
while (continue)
{ scanf(&quot;%d&quot;,&em);
  if (em < 1 || em > 12)  // this includes em=0...
    { printf(&quot;(T)error!!! Wrong month...\n Try again&quot;)
    }
  else 
    continue = false;
}
Like i said: INITIALIZE YOUR VARIABLES ALWAYS!
[red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
Sorry, I should have cleared that up. I do initialize my variables, I just didn't paste my whole program in here. I will try this new loop and see if it works.

Thanks!
Erica
 
I too am new to C, I've had a few assignments where I've had to validate data and I always enter these situations as &quot;char&quot; or &quot;*char&quot;. You can perform any validation checks you want then and if the end result has to be an int you can always use the &quot;atoi&quot; function. Hope this helps you a bit.


Dennis
 
scanf() returns the number of conversions made. You should be able to test the value returned to see if it successfully made a decimal conversion.

Brudnakm
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top