Using isdigit

Thread Solved

Join Date: Sep 2008
Posts: 25
Reputation: tyserman5674 is an unknown quantity at this point 
Solved Threads: 0
tyserman5674 tyserman5674 is offline Offline
Light Poster

Using isdigit

 
0
  #1
Jul 2nd, 2009
Hi Everyone,
I need help with using isdigit in C. What I am trying to do is verify that the user input is numeric. In my program, as is, I get the error:
line (21) : error C2061: syntax error : identifier 'isdigit'. If I remove the "if" from in front of the" isdigit" it will run but not give me the results that I need. It runs thru the program as if it where 0 and goes to my
3rd "if" statement, printing the you have 18 years to wait to vote, then it prints "You did not enter a digit".
Here is my code;
  1. /* CSS 561 Prograqmming Concepts
  2. Week 2 Assignment
  3. A program that prompts a user for his or her age,
  4. and computes whether they are old enough to vote – age 18.
  5. If they are too young, your program should inform them that
  6. they can vote in XX years. If they are old enough, you should
  7. inform them that they have been eligible to vote for YY years. */
  8. //by Ken Volkman
  9. #include <stdio.h>
  10. #include <ctype.h>
  11. int main()
  12.  
  13. {
  14. int iResponse = 0;
  15. char cResponse = '\0';
  16. {
  17. printf("\n\tThis program will determine if you are old enough to vote:\n");
  18. printf("\n\tPlease enter your age; ");
  19. scanf_s("%d, %c", &iResponse, &cResponse);
  20. }
  21. if isdigit(cResponse);
  22.  
  23. {
  24. if(iResponse >=18)
  25. {
  26. printf("\n\tYou have been elgibale to vote for\n\t%d years now.\n\t", iResponse - 18);
  27. //printf("\n\t");
  28. printf("\n\t'Thank You'");
  29. printf("\n\n\t");
  30. }
  31. if(iResponse < 18)
  32. {
  33. printf("\n\tYou are not old enough to vote yet, \n\tbut you will be eligable in %d years.\n", 18 - iResponse);
  34. //printf("\n\t");
  35. printf("\n\t'Thank You'");
  36. printf("\n\n\t");
  37. }
  38. else
  39. printf("\n\tYou did not enter a didgit\n\n\t");
  40. }
  41.  
  42. }
Can someone please help me out?
Tyserman5674
Last edited by Ancient Dragon; Jul 2nd, 2009 at 9:31 pm. Reason: correct code tags
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 476
Reputation: csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice 
Solved Threads: 76
csurfer's Avatar
csurfer csurfer is offline Offline
Posting Pro in Training

Re: Using isdigit

 
0
  #2
Jul 2nd, 2009
Errors :

1> You cannot code like this.isdigit() is a function:
  1. if isdigit(cResponse);
It should be
  1. if(isdigit(cResponse));

2>Be sure of the portability of scanf_s
3>Your program has a lot of mistakes.

*
  1. {
  2. printf("\n\tThis program will determine if you are old enough to vote:\n");
  3. printf("\n\tPlease enter your age; ");
  4. scanf_s("%d, %c", &iResponse, &cResponse);
  5. }
Useless Braces.

*The semicolon after your problematic if statement.With that its as good as not including that if statement.

*isdigit() checks for a digit 0 1 2 3 4 5 6 7 8 9 not for a number.

And many others so check.

And ya mainly use code tags.Is that so difficult???
Last edited by csurfer; Jul 2nd, 2009 at 1:28 pm.
I Surf in "C"....
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,602
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Using isdigit

 
0
  #3
Jul 2nd, 2009
the answer to your basic problem:
  1. if isdigit(cResponse);
is incorrect. it should be
  1. if (isdigit(cResponse))
  2. {
  3. // do stuff here
  4. }

(1) an if-statement must be entirely enclosed in parentheses.

(2) an if-statement -- if the condition is evaluated as true -- will execute either one line of code terminated with a semi-colon or a block of code contained between the curly braces, '{' and '}'

so even if you framed the conditional statement correctly, since you put the semicolon directly after the if statement, it still would never execute anything when the condition evaluated true.

.
Last edited by jephthah; Jul 2nd, 2009 at 1:54 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 25
Reputation: tyserman5674 is an unknown quantity at this point 
Solved Threads: 0
tyserman5674 tyserman5674 is offline Offline
Light Poster

Re: Using isdigit

 
0
  #4
Jul 3rd, 2009
I want to thank you all for your input, I did get it solved.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC