Thread: Help! C beginner needs help with dealing with inputs to a scanf() function!

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    5

    Help! C beginner needs help with dealing with inputs to a scanf() function!

    Hi

    I have written a very basic program that asks for people to input a number and it works out the factorial of that number and displays it to the screen. However, when people enter a letter instead of a number, the program goes on an unstoppable loop. How can I detect whether a user has entered a letter and deal with it before this happens?

    Any help greatly apprecitated!

    Thanks

    Jamie

  2. #2
    Registered User *pointer's Avatar
    Join Date
    Oct 2001
    Posts
    74
    Code:
    if((isalpha(input)) == TRUE){
        /*code to handle a letter*/
    }else{
        /*code for proper input*/
    }
    pointer = NULL

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    24

    But....

    But I think the problem still remains there...
    If the user inputs .,/= etc. Then???
    RaHaTk

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    5
    which library is isalpha() in? Because I can't find it!

    Thanks

    Jamie

  5. #5
    Unregistered
    Guest
    check ctype.h i think

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > (isalpha(input)) == TRUE
    In addition, isalpha doesn't return TRUE - it returns non-zero if the character passes the test

    So
    Code:
    if ( isalpha(input) ) {
        // its a letter
    } else {
        // no it isn't
    }

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Hmm.. maybe this will be helpful.

    Program with problems...
    Code:
    #include <stdio.h>
    int main ()
    {
     int i;
     printf ("Type in numbers, foo!\nType in 0 to quit!\n");
     while ()
     {
      scanf ("%d", &i);
      if (i == 0) break;
      printf ("You typed in %d\n", i);
     }
     return 0;
    }
    And the program without the same problems...
    Code:
    #include <stdio.h>
    int main ()
    {
     int i, j;
     printf ("Type in numbers, foo!\nType in 0 to quit!\n");
     while ()
     {
      j = scanf ("%d", &i);
    
      if (j != 1) 
      {
       printf ("That ain't a number, get outta my program!\n");
       break;
      }
    
      if (i == 0) break;
      printf ("You typed in %d\n", i);
     }
     return 0;
    }
    Just trying to take advantage of the fact that scanf returns the number of arguments scanned in, or EOF if EOF is encountered. So basically, in that call of scanf, it is going to either return 1, which means it succesfully scanned something in, a 0 which means it couldn't read any arguments (because the first one wasn't an int), or EOF, which shouldn't matter unless you use a file as stdin.

    As usuall, I'm not -sure- that the programs 'll work, but I like to inagine they do.
    Callou collei we'll code the way
    Of prime numbers and pings!

  8. #8
    quah
    Guest
    > int main ()
    > {
    > int i;
    > printf ("Type in numbers, foo!\nType in 0 to quit!\n");
    > while ()

    You seem to have acquired some bad programming habbits there.
    Both your 'main' and your 'while' should not be empty. This is just sloppy programming.

    Quzah.

  9. #9
    Registered User
    Join Date
    Nov 2001
    Posts
    5

    Smile

    Thank you all very much!

    I now have a working program.

    Drinks on me.... =)

  10. #10
    Registered User
    Join Date
    Aug 2001
    Posts
    207

    code

    Hi, I'm curious about how your code looks now that is working. Would you post it?

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM