Thread: Having "if statement" check input for integer.

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    70

    Having "if statement" check input for integer.

    Ok, I have been looking online, but I can't find any help for my limited knowledge so far in my course.

    I am making a basic program that you input an integer for month and one for day. I have it set up already to give an error if it is beyond the limit of how many months their are and days in a month (exluding Feb special rules). The tricky part though is making the "if statements" verify that the input from the user isn't a decimal number.

    While looking online I could find solutions pertaining while loops and all these other commands I am not supposed to have learned yet, so I shouldn't use them. All I have at my disposale are basic arguments within if statements.

    My code so far:

    Code:
    #include <stdio.h>
    
    int main()
    {
    
    int month, day;
    
    printf("Enter a month (use a 1 for Jan, etc.): ");
    scanf("%d", &month);
    printf("Enter a day of the month: ");
    scanf("%d", &day);
    
    if (month < 1 || month > 12 || month%1 != 0)
        printf("Interger for month entered was %d and is an invalid entry", month);
    else if (day < 1 || day > 31 || day%1 != 0)
        printf("Interger for day entered was %d and is an invalid entry", day);
    else {
        printf("The month entered was %d and the day of the month %d", month, day);
    }
    return 0;
    }
    I was trying to use the modular command to check for a remainder, but when I run the program I am getting:

    [cameron@localhost Program4]$ make Exercise2
    cc Exercise2.c -o Exercise2 //No Errors from Compiler
    [cameron@localhost Program4]$ ./Exercise2
    Enter a month (use a 1 for Jan, etc.): 1
    Enter a day of the month: 2
    The month entered was 1 and the day of the month 2 //Normal run that works
    [cameron@localhost Program4]$ ./Exercise2
    Enter a month (use a 1 for Jan, etc.): 1.2 //Trying to input decimal number
    Enter a day of the month: Interger for day entered was 0 and is an invalid entry[cameron@localhost Program4]$ //Says Integer for day was 0 and shuts down.

  2. #2
    Registered User
    Join Date
    Jun 2013
    Posts
    70
    Guess you guys don't know either. Oh well. Assignment is due in 30 min. Spent 1 1/2 hours on this part alone lol. Can't find anything on google search that doesn't involve while loops or something else that my book hasn't gone over yet.
    Last edited by Cameron Taylor; 06-12-2013 at 10:30 PM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Guess you guys don't know either. Oh well. Assignment is due in 30 min.
    Well you only waited about 40 minutes before complaining. This is a forum with users from all around the planet, not some helpdesk with always available help.

    Enter a month (use a 1 for Jan, etc.): 1.2 //Trying to input decimal number
    Enter a day of the month: Interger for day entered was 0 and is an invalid entry[cameron@localhost Program4]$ //Says Integer for day was 0 and shuts down.
    scanf("%d", &month);
    printf("Enter a day of the month: ");
    scanf("%d", &day);

    Here's how it works.
    You type in "1.2\n"
    the first scanf removes 1, stopping at the . (it's not part of an integer).
    1 is assigned to month, and the return result is also 1 (which you DON'T check).

    The second scanf comes along, sees the . in ".2\n" left over from last time and immediately bails out.
    Nothing is assigned to day, and the return result of scanf is zero.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [HELP] Check if "Just do it" is in users input
    By Arbyn Acosta in forum C Programming
    Replies: 15
    Last Post: 09-14-2012, 07:10 AM
  2. nbin=fopen("input.txt","a"); doesn't work?
    By Adam Rinkleff in forum C Programming
    Replies: 2
    Last Post: 06-23-2011, 02:57 PM
  3. "Expected declaration or statement at end of input"
    By levitylek in forum C Programming
    Replies: 1
    Last Post: 11-16-2010, 10:02 AM
  4. Replies: 14
    Last Post: 11-08-2010, 01:47 AM
  5. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM