getting exact input

This is a discussion on getting exact input within the C Programming forums, part of the General Programming Boards category; I am bored and decided to take on a task. To get a number from the user without any flaw. ...

  1. #1
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501

    getting exact input

    I am bored and decided to take on a task. To get a number from the user without any flaw. It works for numbers, but if I enter 234a then it converts to 234 any ideas on how to change that
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void){
            char buffer[BUFSIZ];
            int x;
            printf("Enter a number:  ");
            fflush(stdout);
            if(!(fgets(buffer,sizeof buffer,stdin)))
                    exit(0);
            if(!(sscanf(buffer,"%d",&x)))
                    exit(0);
            printf("%d is your number\n",x);
            return 0;
    }
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  2. #2
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Sorry my brother must have logged on, on my computer. I posted this code above. I could isdigit ever element in the array but is there another way?
    Last edited by linuxdude; 05-05-2004 at 09:31 PM.

  3. #3
    I like code Rouss's Avatar
    Join Date
    Apr 2004
    Posts
    131
    Just keep it as a string, if you want it to print the 'a' too.
    Code:
    printf("%s is your number\n", buffer);
    Unless you mean you want it to reject the input...
    Code:
    int i = 0;
    int l = strlen(buffer);
    while(i != l)
    {
       printf("Enter a number:  ");
       if(!(fgets(buffer,sizeof buffer,stdin)))
                    exit(0);
       for(i = 0; i < l; i++)
          if(!isdigit(buffer[i]))
              i = l + 1;
    }
    if(!(sscanf(buffer,"%d",&x)))
                    exit(0);
    I think that will do what you want... If you want it to reject invalid input.

    // Sorry, was typing during your second post... Don't know of another way..

  4. #4
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    That is what I figured. Nice example excep the use of 1(num) and l(char) have a syntax highlighter that tells you if it is a num or not

  5. #5
    I like code Rouss's Avatar
    Join Date
    Apr 2004
    Posts
    131
    lol... I didn't even think about that... I just used l for length... Yeah... 1 != l... Just like O != 0.
    D'0h.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by linuxdude
    but is there another way?
    FAQ
    (the strtol version may be of particular interest)



    Code:
    if(!(sscanf(buffer,"%d",&x)))
    What about a return value of EOF?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. input redirection
    By sashaKap in forum C Programming
    Replies: 6
    Last Post: 06-25-2009, 01:59 AM
  2. Input statement problem
    By une in forum C Programming
    Replies: 3
    Last Post: 05-29-2007, 11:16 PM
  3. For loop problems, input please.
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-22-2007, 03:54 AM
  4. I would love some input on my BST tree.
    By StevenGarcia in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2007, 12:22 AM
  5. Help with Input Checking
    By Derek in forum C Programming
    Replies: 7
    Last Post: 06-17-2003, 03:07 AM

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