Reputation: 615
I'm having problem with a simple C program. Even if I enter a year between 1000 and 1999 it still displays invalid year. Please tell me what's happening??
#include <stdio.h>
main()
{
int year;
c:
printf("\n\nEnter a Year: ");
scanf("%d", year);
if ((year < 1000) || (year > 1999))
{
printf("\n\nInvalid Year");
goto c;
}
convert(year);
}
convert(int year)
{
printf("%d", year);
}
Upvotes: 3
Views: 206
Reputation: 78
in scanf, the other parameters should be pointers
it should be
scanf("%d", &year);
Upvotes: 2
Reputation: 2028
Adam Zalcman beat me to it, use
scanf("%d", &year)
, and try to avoid goto statements. Replace it with a while loop in the following fashion:
main() {
int year;
printf("\n\nEnter a Year: ");
scanf("%d", &year);
while((year < 1000) || (year > 1999)) {
printf("\n\nInvalid Year");
printf("\n\nEnter a Year: ");
scanf("%d", &year);
}
}
an even better construction would be to use the do{}while() construction, but I'll leave that to you as an exercise :)
Upvotes: 1
Reputation: 27233
You need to pass an address to scanf
, i.e.:
scanf("%d", &year);
Note the ampersand.
Upvotes: 7