isdigit() Function!

This is a discussion on isdigit() Function! within the C Programming forums, part of the General Programming Boards category; Hi guys, here is the code: Code: #include <stdio.h> #include <stdlib.h> #include <ctype.h> int main() { int iYearBorn = 0; ...

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230

    isdigit() Function!

    Hi guys, here is the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    int main()
    {
    	int iYearBorn = 0;
    	printf("\nEnter year born, either 2000, 2001, 2002: ");
    	scanf("%d", &iYearBorn);
    		if (isdigit(iYearBorn) == 0)
    			{
    				printf("\nInvalid Entry. Not a number! Program Terminating.\n");
    				return 0;
    			}
    	switch(iYearBorn)
    		{
    			case 2000:
    			printf("\n2000 is the year of luck!\n");
    			break;
    			case 2001:
    			printf("\nPeace be with you 2001\n");
    			break;
    			case 2002:
    			printf("\n2002 is uber leet pwnage dudez\n");
    			break;
    			default:
    			printf("\nNo data on that year!\n");
    		}
    	return 0;
    }
    What I want to do is check the user's input to make sure it's a number, if it's not a number I want it to display that message (Invalid Entry. Not a number! Program Terminating) and kill the program.

    If it is a number I want it to continue on down to the switch statement etc, but the problem is, whenever I run it, no matter what the user input is, it ALWAYS says "Invalid Entry. Not a number! Program Terminating" and kills the program, even if I enter a number... so I'm assuming i'm using the isdigit function wrong somehow, if anyone could help me out that'd be great. Thanks!

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,470
    isdigit checks the character, not a number

    so '1' is digit, and 'a' is not...

    to make verification as you want - you need to read the user input as string for example using fgets

    then - try to convert it using strtol for example and see where the conversion stops, if all characters of the string except \n are successfully parsed - it was a valid number, if something left in the buffer - it is not
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 06:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21