Thread: There must be a way...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    119

    There must be a way...

    I posted a question previously about what happens in a program when scanf is requesting an integer, if you just keep hitting enter, you can "do it all day". I got some very helpful replies, with a link that I checked out which showed me other great ways to validate input, which I am grateful for. However, I still have the same problem:

    Below isn't very fancy code, but I just wanted to conduct a simple test to see if what I learned would work. The good news is it works great if the user enters a character instead of a number, however, if the user hits enter, they can keep hitting it over and over again and the cursor will happily continue to go on down the screen. I know there must be a way to prevent this from happening, because I've run professional programs before (as I'm sure we all have) that were compiled in C, and I can guarantee you that in any of these programs, you can't pull off this enter fiasco. I really want to know how to fix this, because no matter if one day I become able to code an incredible program, it will never look professional anytime someone accidently just hits enter when I'm asking for an integer, and the cursor scrolls on down the screen.

    Code:
    #include <stdio.h>
    int main() {
              int number;
              printf("Enter an integer:");
    
              if (scanf("%d", &number) != 1) {
                    printf("You didn't type in a number");
                    getchar();
              }
        return 0;
        }
    Please help!!
    Last edited by Ash1981; 01-02-2006 at 02:25 AM. Reason: spelling error

  2. #2
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    Don't know if its the ideal solution...but it works
    Code:
    #include <stdio.h>
    int main() {
              int number;
              printf("Enter an integer:");
    
              if (scanf("%[0-9]d", &number)!=1) {
                    printf("You didn't type in a number");
                    getchar();
              }
        return 0;
     }

  3. #3
    Registered User freespace's Avatar
    Join Date
    Nov 2005
    Posts
    7
    A combination of fgets and sscanf will do what you want, like so:

    Code:
    #include <stdio.h>
    int 
    main()
    {
            int             number;
            char buf[512];
            printf("Enter an integer:");
    
            fgets(buf, sizeof(buf)-1,stdin);
            if (sscanf(buf,"%d", &number) != 1) {
                    printf("You didn't type in a number");
                    getchar();
            }
            return 0;
    }
    An advantage is that fgets' behaviour is a lot more predictable and intutitive then scanf. Hope this helps.

    Cheers,
    Steve

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    thanks so much to both of you

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well you'll never be able to manage that kind of control if you stick to just ANSI-C. ANSI-C just has an input stream and an output stream, and really doesn't care whether that's a keyboard and monitor on your desktop machine, or a keypad and LCD on a vending machine.

    Most useful programs which have user interaction end up using some kind of screen oriented API, whether it be say ncurses for a console type application, or one of the many GUI libraries like Qt for windowed applications.

    In your example of getting an integer, rather than calling scanf(), you would call a function to read an integer from some position on the screen. The routine itself would know how to keep the cursor in place, main() only gets the answer.

    As it says here, if you want to write portable code then you need to think about how you arrange the code into separate functions and files.

    Most of the code would depend only on ANSI-C and the internal interfaces of your own code.
    Some of the code would depend on ANSI-C and say a widely supported OS interface like POSIX.
    A small part of the code would depend on a specific screen handling package like say ncurses.

    Say you had a text editor with a total of 30 files in a 20, 7, 3 split using the above functional partitions. If, for example, you want to use the code on a system which doesn't have ncurses, and you've made a good job of isolating that dependency, then you only have 3 files to fix, rather than 30 if you'd mixed everything into one big mess.

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    Salem : Thanks for your reply as well, I will definatly take that into account.

Popular pages Recent additions subscribe to a feed