Thread: isalpha

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    71

    Question isalpha

    why does this function crash my program
    Code:
    int checkPrice (int *num)
    {
    	scanf ("%d", num);
    	while (isalpha (*num))
    	{
    		fflush (stdin);
    		printf("Only numerals allowed\n");
    		printf("Please re-input the price: ");
    		scanf ("%d", num);
    		return 0;
    	}		
    	fflush (stdin);
    	return 1;
    }
    num is being passed by reference..
    basically, it works if i input a number but if i input a character it crashes and thats the exact thing i want to test in the program..
    any other ways around it

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    26
    It works fine to me! can we see the full code?!
    why did u include return 0 in the while loop?!

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    71

    Re: isalpha

    my friend tested a number with isalpha and it worked and i did almost same thing and it didnt... point being that isalpha can test integers
    Last edited by mackol; 11-29-2002 at 03:12 AM.

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    71
    Originally posted by Salem
    isalpha tests characters, not numbers

    Code:
    while ( scanf("%d", num ) != 1 ) {
        while ( getchar() != '\n' );  // better than fflush(stdin)
        printf("Only numerals allowed\n");
        printf("Please re-input the price: ");
    }
    i dont quite get ur first line of code...

    what happens if a user inputs 22442a

    will that line of code work for that

  5. #5
    Black Mage Extraordinaire VegasSte's Avatar
    Join Date
    Oct 2002
    Posts
    167
    Salem, in your first line of code:
    Code:
    while ( scanf("%d", num ) != 1 ) {
        while ( getchar() != '\n' );  // better than fflush(stdin)
        printf("Only numerals allowed\n");
        printf("Please re-input the price: ");
    }
    should it not read:
    Code:
    while (scanf ("%d", &num) !=1 ) {
    /* rest of code here */
    I always thought the & was necessary when using scanf?!?!
    Last edited by VegasSte; 11-29-2002 at 03:40 AM.

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    26
    num is a pointer which contain an address therefore there is no need for &!

  7. #7
    Black Mage Extraordinaire VegasSte's Avatar
    Join Date
    Oct 2002
    Posts
    167
    Thankyou for clearing that up Lazystudent!

  8. #8
    Registered User
    Join Date
    May 2002
    Posts
    71
    whoa
    salem dude, u r a genius
    but u know what even though it works, i still dont get why should it keep reading till != 1 ???

  9. #9
    Registered User
    Join Date
    May 2002
    Posts
    71
    i read the C reference manual but it didnt make sense till u gave ur explanation
    thanks mate

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Of course, it's quite possible that the whole reason your code was crashing was this line:

    fflush (stdin);

    After all, it's undefined. Theoreticly, it could crash your program.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Palindrome, problem with isalpha
    By Kyeong in forum C Programming
    Replies: 2
    Last Post: 11-07-2008, 01:28 PM
  2. isalpha case
    By mufanz in forum C++ Programming
    Replies: 5
    Last Post: 03-19-2006, 01:30 PM
  3. Problem with isalpha
    By kron_19792000 in forum C Programming
    Replies: 3
    Last Post: 09-26-2005, 12:13 AM
  4. isalpha || ispunct
    By linuxdude in forum C Programming
    Replies: 3
    Last Post: 05-21-2004, 09:43 PM
  5. Floating Point isalpha();
    By UnclePunker in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 08:00 PM