shr3jn
shr3jn

Reputation: 615

Having Trouble in SImple C Program

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

Answers (3)

D. Cannone
D. Cannone

Reputation: 78

in scanf, the other parameters should be pointers
it should be

  scanf("%d", &year);

Upvotes: 2

Yuri
Yuri

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

Adam Zalcman
Adam Zalcman

Reputation: 27233

You need to pass an address to scanf, i.e.:

scanf("%d", &year);

Note the ampersand.

Upvotes: 7

Related Questions