Thread: preventing user entering characters

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    6

    preventing user entering characters

    hello,

    i have made a program where it asks the user to enter an interger, but i want a error message to come up when the user enters a letter, how can i do this??

    thanks

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Post your code.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    88
    you cant prevent them from inputing a character without telling us your operating system and compiler.

    you can use fgets() to input and atoi() to convert to int

    Code:
    char string[100]
    int number;
    do
    {
     fgets(string, 100, stdin)
    }while(!(number = atoi(string)));
    this works by letting them input everything but continuing input until a number is provided.

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    6
    The scanf() function returns the number of values assigned to the the given addresses (the ones that comesafter the control string.).For example, if you have
    scanf ("%d %d",&x,&y) where x and y are integers,
    and the input is say: 20 c
    then no value will be assigned to y and 20 will be assigned to x.
    and the return value of scanf() is 1.
    Note:the character c will be available for the next scanf() function.

    Thus,just check the return value of scanf().
    ex:


    Code:
    printf ("Enter an integer:");
    temp=scanf("%d",&x);
    if(temp!=1)
       printf("Wish u knew what an integer is\n");

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    6
    here is the code where i want to prevent users from entering a letter:
    Code:
    		printf("Please enter the temperture:\n");
    		scanf("%d", &temp);
    
    			if ( temp > 45 || temp < -25)
    			{
    				do
    				{
    					printf("The temperture is out of range.\n");
    					printf("Please re-enter temperture:\n");
    					scanf("%d", &temp);
    				} while ( temp > 45 || temp < -25);
    
    else
    //rest of my program
    thanks for any help

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Better still, read the FAQ .
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Jun 2003
    Posts
    6
    Try this...
    Code:
    temp=100;
    do{
     
             printf("Please enter the temperture:\n");
             int_var = scanf("%d", &temp);
    
             if (int_var==0) 
                  printf ("Enter an integer....")
             else 
                  if ( temp > 45 || temp < -25)
    	  printf("The temperture is out of range.\n");
    
    }while (temp>45 || temp<-25);
    Hope this serves the purpose...

  8. #8
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by Mahesh Herle B
    Try this...
    Code:
    temp=100;
    do{
     
             printf("Please enter the temperture:\n");
             int_var = scanf("%d", &temp);
    
             if (int_var==0) 
                  printf ("Enter an integer....")
             else 
                  if ( temp > 45 || temp < -25)
    	  printf("The temperture is out of range.\n");
    
    }while (temp>45 || temp<-25);
    Hope this serves the purpose...
    Try this FAQ then:
    http://www.eskimo.com/~scs/C-faq/q12.20.html
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Try this FAQ then:
    They didn't read the last FAQ I posted, what makes you think they'll read that one
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by Hammer
    >>Try this FAQ then:
    They didn't read the last FAQ I posted, what makes you think they'll read that one
    Good point...

    As I posted elsewhere, when I click on the FAQ links in Salem's messages, I don't get a FAQ, I get the index, which is not the link posted. I'm wondering if there is a link problem within the forum's server.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  11. #11
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020

  12. #12
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    am I the only one who read the FAQ!?
    Do you people know what FAQ means!?

    and How come people avoid the FAQ like the plague. The link Hammer posted HAS your solution if you read through it. AND you will LEARN something. I really don't get it but whatever.

    NOW to right the injustice:

    FAQ

    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Do you people know what FAQ means!?
    FAQ is a way of saying "........ off", as can be found by the proper pronunciation of the acronym. The commonly accepted pronunciation is a compromise: two letters are sounded
    as if they were forming a syllable and the third letter is named: it is 'fuh queue'. That's not only how it's said when it stands for "frequently asked question." When it means a compilation of frequently asked questions and their answers, that's 'fuh queue' too.

    >and How come people avoid the FAQ like the plague.
    Probably because they think we've been telling them to. See above.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do you check how many characters a user has entered?
    By engstudent363 in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 06:05 AM
  2. Instant messenger app help
    By AusTex in forum C Programming
    Replies: 2
    Last Post: 05-01-2005, 12:41 AM
  3. limit the characters using gets function
    By yukon in forum C Programming
    Replies: 6
    Last Post: 10-08-2001, 02:15 PM
  4. Stopping a user from typeing.
    By knave in forum C++ Programming
    Replies: 4
    Last Post: 09-10-2001, 12:21 PM