Hello everyone,
I am new to learning C++ and have no prior experiences with programing. I just started with learning C++ and it is a little intimidating and mindboggling. However, I have so far fun with the whole C++. My teacher wants us to extend a leap year algorithm to tell when a year is a millennium.
Now I have been spending hours working on this solution and I came up with this modification. The program runs it tells me about the leap Year and even Millennium year, however, (and that’s where the problem is) all the years I put in are a Millennium year. Can anyone tell me where I am going wrong with this code? Thank you.
//**********************************************
//Leap Year and Millennium Program
//This program inputs a year and prints wheter
//the year is a leap year and a Millennium
//**********************************************
#include <iostream> // Access output stream
using namespace std;
bool IsLeapYear ( int year );
bool millenniumYear (int year );
int main()
{
int year; // year to be tested
cout << "Enate a year AD, for example 1997."
<< endl; //promp for input
cin >> year; //read year
if (IsLeapYear(year)) //test for leap year
cout << year << "is a leap year." << endl;
else
cout << year << "is not a leap year." << endl;
if (millenniumYear(year)) //test for mellennium year
cout << year << "is a millennium year." << endl;
else
cout << year << "is not a millennium year." << endl;
// IsLeapYera returs true if year is a leap year and
// false otherwise.
{
if (year % 4 != 0) //yera is not divisible by 4
return false; // if so, can't be leap year
// must be divisible by 4 at this point
if (year % 100 != 0) // is not multiple of 100
return true; //if so, is a leap year
//must be divisble by 100 at this point
if (year % 400 != 0) //is not a multiple of 400
return false; //if so, is not a leap year
//must be divisble by 400 at this point
}
bool millenniumYear ( int year)
//millenniumYear returns true if divided by 1000
//false otherwise
{
if (year % 1000 !=0) //year is devidable by 1000
return true; //if so, it is a millennium
bool millenniumYear ( int year)
//millenniumYear returns true if divided by 1000
//false otherwise
{
if (year % 1000 !=0) //year is devidable by 1000
returntrue; //if so, it is a millennium
system ("PAUSE");
return 0;
}
This function is a bit of a mess. Why is there a system("PAUSE") in it? Did you just fumble the cut n' paste?
Anyway, you want to return true when a year is dividable by 1000?
Try if (year % 1000 ==0) // if year IS dividable by 1000
rather than if (year % 1000 !=0)// if year IS NOT dividable by zero
#include <iostream>
int main()
{
usingnamespace std;
cout << "Please enter a year: " << endl;
int year;
cin >> year;
if (year % 1000 == 0)
{
cout << "This is a millenium and leap year." << endl; //As far as I know, every millennium year is a leap year.
}
elseif (year % 4 == 0)
{
cout << "This year is a leap year" << endl;
}
else
cout << "This is not a leap year nor is a millenium year.";
return 0;
}
Thank you for your quick answer. Like I said, I am very new and been watching videos on C++ and reading all kinds of stuff. The” system Pause” seemed logical to me at that point. Anyway, we learn from our mistakes. I did take your advice and changed a few thinks and it is working now. Thank you for your help. I most likely will be back with more questions.