input checking problem..

This is a discussion on input checking problem.. within the C Programming forums, part of the General Programming Boards category; ggngn...

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    input checking problem..

    ggngn
    Last edited by transgalactic2; 01-22-2009 at 04:14 PM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    One suggestion is to read a whole lines worth of input into a char array variable and then use the strtod function on that buffer. If the "end" pointer argument to that function points to a null then the information was parsed properly. If it points to something else, i.e. the conversion of the text into an integer stopped short for some reason (such as encountering the first 'g' in "1gghjk"), then you know something other than a simple integer value was entered and can reprompt for more data.

    Incidentally, I would not use recursion for the clean and main_menu functions. The reason you are using it in this instance is not something recursion was really meant to solve.

    [edit]Oh, and main returns an int. [/edit]
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This is a little smoother, imo:

    Code:
    input1 = 0;
    
    while(input1 == 0)
       {
       printf(" Enter Your Choice: ");
       input1 = scanf("%d", &opt);
       }
    The program keeps looping back to query the user and get the input value,
    rather than just ask once, and then run on. Very easy to add all kinds of test
    for good input, as well.

  4. #4
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i solved it
    Last edited by transgalactic2; 01-22-2009 at 02:31 PM.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Quote Originally Posted by Adak View Post
    This is a little smoother, imo:

    Code:
    input1 = 0;
    
    while(input1 == 0)
       {
       printf(" Enter Your Choice: ");
       input1 = scanf("%d", &opt);
       }
    The program keeps looping back to query the user and get the input value,
    rather than just ask once, and then run on. Very easy to add all kinds of test
    for good input, as well.
    Uh, no...

    That still has the problem with "1gghjk", the scanf call will return 1 and it will also return 1 to the opt variable. The rest of the string "gghjk" (garbage) will still be in the input stream/buffer. And god forbid you enter something that doesn't start with a numerical char like "gghjk1", the scanf will return 0 since there was nothing to convert into an int and the stream will still have the "gghjk" in it and you'll loop endlessly.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Nov 2008
    Location
    Bay Area, CA
    Posts
    60
    Quote Originally Posted by hk_mp5kpdw View Post
    Uh, no...

    That still has the problem with "1gghjk", the scanf call will return 1 and it will also return 1 to the opt variable. The rest of the string "gghjk" (garbage) will still be in the input stream/buffer. And god forbid you enter something that doesn't start with a numerical char like "gghjk1", the scanf will return 0 since there was nothing to convert into an int and the stream will still have the "gghjk" in it and you'll loop endlessly.
    Surprised there isn't a flush in there. Adak's solution looks almost exactly like what I came up with on my own in my programs, except I added a flush after the scanf line.

    Code:
    #define FLUSH while (getchar() != '\n')
    Code:
    input1 = 0;
    
    while(input1 == 0)
       {
       printf(" Enter Your Choice: ");
       input1 = scanf("%d", &opt);
       FLUSH;
       }
    If this is his only input line in the program, he may not need to do the define, just put the while loop in there directly.

    All of this presupposes that he can even use loops, though. His teacher, for whatever reason that god only knows, seems hell bent on banning loops from the class. He's posted like 10 different things, all of them "how do you do this without a loop?" In which case, he'll have to figure out some other way of doing this.

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    what will happen with your code if scanf or getchar returns EOF?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,013
    Do not edit out your posts once you get your answer! How is anyone else having the same trouble supposed to know how you solved it?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem collecting data from input file
    By gkoenig in forum C Programming
    Replies: 12
    Last Post: 03-30-2008, 08:40 AM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 04:42 PM
  3. Problem with Printing message
    By robert_sun in forum C Programming
    Replies: 2
    Last Post: 05-16-2004, 03:09 PM
  4. Checking ascii values of char input
    By yank in forum C Programming
    Replies: 2
    Last Post: 04-29-2003, 08:49 AM
  5. Problem with text input
    By newbie in forum C++ Programming
    Replies: 2
    Last Post: 03-10-2002, 04:44 PM

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