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

1. ## 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. 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.

3. > 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.

Popular pages Recent additions