This is what the code looks like. It's a simple program to determine which years are leap years, and which aren't. I'm having trouble ending the program if the user types ctrl-z. Any help would be appreciated. I know it's probably something simple, but I can't figure it out. :(

Here's the code:

#include <iostream>
using namespace std;

bool isLeap(int year);

int main(){
int year;
bool leapYear;

do{
cout << "Enter a year: ";
cin >> year;

leapYear = isLeap(year);

if (leapYear == true)
cout << year << " is a Leap Year. \n\n";
else
cout << year << " is not a Leap Year. \n\n";
}while(year != EOF);

return 0;
}

bool isLeap(int year){
	bool trueOrFalse;
	double condition1;
	double condition2;
	double condition3;

	condition1 = (year % 4);
	condition2 = (year % 100);
	condition3 = (year % 400);
	
	if(condition1 == 0 && condition2 != 0 || condition3 == 0)
		trueOrFalse = true;
	else 
		trueOrFalse = false;
		
return(trueOrFalse);

you have to initialize year to EOF before line 12 then add an if statement after eof to see if it still the same. The problem with this is that year will not be changed if you type anything other than numeric digits.

do{
cout << "Enter a year: ";
year = EOF;
cin >> year;
if( year == EOF)
    break;
// rest of program here

Or, to avoid having to reset year to EOF each time,

do{
      cout << "Enter a year: ";
      cin >> year;
      if( cin.eof( ) )
           break;
// rest of program here

And similarly, the loop test can be written as

}while ( !cin.eof( ) );
Comments
much better solution
This article has been dead for over six months. Start a new discussion instead.