0

This code runs almost as I like it to but the only problem is when I use a char as and input, then it goes in an infinite loop. How can I fix this?

#include <stdio.h>
#include <stdlib.h>

int main(){

    int year;
    int age;

    printf("This program will calculate how old you are \n\n");
    //sleep(2); 
    do
    {
        printf("what year are you born in?\n");
        scanf("%d", &year);
        age = 2020 - year;
      
        if(year >= 1910 && year <= 2020){
            printf("You will or have turned %d year this year \n\n", age);
            //sleep(1);
        }else if (year == 0){
            printf("Program exits \n");
        }else{
            printf("write a valid input between 1910 and 2020\n\n");
        }
    }while (year != 0);

    //system("PAUSE");
    return 0;
}
dbush
  • 162,826
  • 18
  • 167
  • 209

0 Answers0