943,587 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 8252
  • C RSS
Jul 2nd, 2009
0

Using isdigit

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
tyserman5674 is offline Offline
25 posts
since Sep 2008
Jul 2nd, 2009
0

Re: Using isdigit

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.
Reputation Points: 485
Solved Threads: 88
Posting Pro
csurfer is offline Offline
564 posts
since Jan 2009
Jul 2nd, 2009
0

Re: Using isdigit

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.
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
Jul 3rd, 2009
0

Re: Using isdigit

I want to thank you all for your input, I did get it solved.
Reputation Points: 10
Solved Threads: 0
Light Poster
tyserman5674 is offline Offline
25 posts
since Sep 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: File problem in link list
Next Thread in C Forum Timeline: please hlep ()





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC