Thread: Error checking

  1. #1
    Registered User
    Join Date
    Nov 2012
    Location
    St.Vincent
    Posts
    4

    Error checking

    Hello, I would like some assistance on the following. I want the user to input an integer value for a year. However I want to check two conditions, first that it is greater than 1900, which I'm thinking of doing by a while. More importantly though, I don't want the program to crash if a character or some unexpected input is entered. Any thoughts???

    There is this piece of code that works to take an input and wont crash no matter what is entered, how do I do the same for strictly an integer?

    while ( ( year = scanf("%d", year) ) != 1 ) {

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    To enable complete validation, you need to read things as a string, then convert.
    Code:
    char buff[BUFSIZ];
    while ( fgets( buff, BUFSIZ, stdin ) != NULL ) {
        if ( sscanf(buff,"%d",&year) == 1 && year >= 1900 ) {
        } else {
        }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Location
    St.Vincent
    Posts
    4
    Thanks. Checked code and it does work, but can you kindly explain to me what the first three lines do?
    Last edited by Fehai Soleyn; 11-11-2012 at 05:55 AM.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Fehai Soleyn View Post
    Thanks. Checked code and it does work, but can you kindly explain to me what the first three lines do?
    Salem has already done so, if you look carefully.

    There is a minor issue that his example assumes the user does not enter more than BUFSIZ characters in a line, but that is easily corrected.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Location
    St.Vincent
    Posts
    4
    I meant, I put the code in with the appropriate conditions I need and it does work. @Grumpy that is the reason i'd like to know what the first 3 lines do, because I have no idea what this BUFSIZ thing is about, so would be grateful for some explanation of he code. I am new to programming in C, but we have an assignment and I don't just want to use code that I don't know what it really is doing.

  6. #6
    Dweeb dojha00's Avatar
    Join Date
    Feb 2012
    Location
    Global
    Posts
    23
    Code:
    char buff[BUFSIZ];  //BUFSIZ is a constant or size of buff array;
    while ( fgets( buff, BUFSIZ, stdin ) != NULL ) {//taking input in buff array using standard input (stdin) of BUFSIZ as a string.read about fgets() function
        if ( sscanf(buff,"%d",&year) == 1 && year >= 1900 ) {// convering that string in year integer variable from buff array. read about sscanf() function.
        } else {
        }
    }
    What is life??

  7. #7
    Registered User
    Join Date
    Nov 2012
    Location
    St.Vincent
    Posts
    4
    Okay, thank you. Will do some reading on them then.

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    BUFSIZ is defined by stdio.h : check out the man page for this header file

    Man Page for stdio.h (all Section 3head) - The UNIX and Linux Forums

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error checking help...
    By Taka in forum C++ Programming
    Replies: 6
    Last Post: 03-23-2009, 06:55 PM
  2. Error Checking
    By jlharrison in forum C Programming
    Replies: 10
    Last Post: 02-12-2006, 05:31 PM
  3. Error Checking
    By Dangerous_Dave in forum C Programming
    Replies: 2
    Last Post: 11-12-2003, 02:30 PM
  4. Checking new for an error?
    By Bajanine in forum C++ Programming
    Replies: 4
    Last Post: 01-28-2003, 08:11 AM
  5. NEED help with a Error checking
    By paulroseby in forum C Programming
    Replies: 7
    Last Post: 10-16-2002, 03:34 PM

Tags for this Thread