A quick question about validation checks

This is a discussion on A quick question about validation checks within the C Programming forums, part of the General Programming Boards category; If I were to use validation checks (both int and char) in a program, am I obliged to use structures ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    15

    Question A quick question about validation checks

    If I were to use validation checks (both int and char) in a program, am I obliged to use structures to do so?

    As an example, suppose I have a variable dept_no declared as int dept_no = 3. How is it done?

    (Note: If it can only be done with structure, just say so without posting the code because I already know how to do it that way).

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >If I were to use validation checks (both int and char) in a program, am I obliged to use structures to do so?
    Not if you don't want to. There are a number of ways to check data for validity, ranging from very simple to very complex. It really depends on your program. Can you be more specific as to what you're doing?

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    15
    To use the example above, let's say the variable is dept_no and it is declared as an integer of 3 digits.

    I'm trying to code it so that the program doesn't accept values that exceed the 3 digits and/or that contain letters. If I were to do any of those, it would say something like 'Wrong value' and prompt me to enter a valid value before moving on.

    Would such a thing be possible?

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Well, this depends on where you are getting the data from but try something like:

    Code:
    char input[5];
    fgets(input, 4, stdin);
    if(strlen(input) > 3)
    	do loop stuff
    I'm not sure I fully understand your question, but that is how I'd do that (basically)

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >'m trying to code it so that the program doesn't accept values that exceed the 3 digits and/or that contain letters.
    That's easy, do a board search for posts about input validation made by me. I've given others almost this exact code several times before.

    -Prelude
    My best code is written with the delete key.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by orbitz
    Well, this depends on where you are getting the data from but try something like:

    Code:
    char input[5];
    fgets(input, 4, stdin);
    if(strlen(input) > 3)
    	do loop stuff
    I'm not sure I fully understand your question, but that is how I'd do that (basically)
    This code won't work properly. For a start, the second parm of fgets() should represent the buffer size, not buffer size minus 1. Even if you did code it as buf-1, the test on the next line will never be true, because there is only room for 3 bytes of data in the buffer, the 4th will be the \0.

    And short buffer lengths are never really a good idea for getting user input, imho.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quick question (adding characters)
    By Cactus in forum C Programming
    Replies: 2
    Last Post: 09-24-2005, 04:54 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 04:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 05:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 12:04 PM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 06:46 PM

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