I am making a simple program where it takes a year from the user and determines if its a leap year or not. The problem I am having is I want it so if the user enters a string it will either end the program if the string is "N" and if its any other string of characters it will just print input is invalid. But as of right now any string input into scanf breaks the program and causes it to loop infinitely.
#include <stdio.h>
#include <stdbool.h>
#include "leapyear.h"
int main() {
int year = 0;
while (true) {
printf("Please enter a year to check:");
scanf("%i", &year);
if (year <= 0) {
printf("Sorry that input is invalid..\n");
}
else if(year == "N") {
printf("Quitting....");
return 0;
}
else {
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
printf("%i IS a leap year \n", year);
}
else {
printf("%i IS NOT a leap year \n", year);
}
}
}
return 0;
}
Ive went through about every post on here about scanf and how to take an int while also allowing for a string to be inputted and stored into the same variable but I cant wrap my head around it. Any help at all would be greatly appreciated.