-2

I am Tony and I am new to c++ programming. I would like to ask a question related to creating a program to check for leap year.

In the following codes, I try to create a bool function to check whether the input is a leap year. If the input is negative, I will cout "Bye!" and stop the program immediately. If the input is positive, then I will check whether it is a leap year using the bool function I built until the input is a negative number then I will exit the program.

However, I am not able to find what mistakes I have made and the current situation is, when I input a positive value, there is no result generated. Please help if you are available. Much thanks to you. : )

#include <iostream>
#include <cmath>
#include <string>
#include <iomanip>

using namespace std;
bool leap_year(int year);
int main()
{
    int year; 
    while (cout << "Enter a year (or negative number to quit): ")
    {
        cin >> year;
        if (leap_year(year) == false && year <0 ) 
        {
            cout << "Bye!" << endl;
        }
        break;
        if (leap_year(year) == false && year >0 )
        {
            cout << "The year is not a leap year." << endl;
        }
        if (leap_year(year) == true && year >0 ) 
        {
            cout << "The year is a leap year." << endl;
        }
        return 0;
    }    
}
  bool leap_year(int year)
{
    bool is_leap_year = false;
    if (year % 4 == 0)
    {
       is_leap_year = true;
    }
    if (year % 100 == 0)
    {
        is_leap_year = false;
    }
    if (year % 400 == 0)
    {
        is_leap_year = true;
    }
    return is_leap_year;
}
  • it's because of your unconditional break inside the while loop that makes your program exit directly? – tunglt Dec 05 '18 at 11:23

1 Answers1

1

First of all, you (should) want a while(true) loop and not a while(std::ostream) loop.

So replace

while (cout << "Enter a year (or negative number to quit): ")
{

with

while (true)
{
    cout << "Enter a year (or negative number to quit): ";

As @paddy pointed out, you can check std::ostream`s return type to look for errors when printing out. But in this simple program I doubt it's necessary.

Then you have the break outside your if statement, which will always break out of the program (no matter the Input). Replace

if (leap_year(year) == false && year <0 ) 
{
    cout << "Bye!" << endl;
}
break;

with

if (year < 0)
{
    cout << "Bye!" << endl;
    break;
}

(there's no need to check if the negative input is a leap year. You can achieve entering only 1 if-statement with if-else statments, therefore you can also replace if(leap_year(year) == false && year < 0) with just if (year < 0); as I did.)

When you apply this to all statements (not changing their internal logic) and remove the return 0; at the end of the loop, you get your desired program flow. Also removing using namespace std; is just better (read here why). You also don't Need to include <iomanip>, <cmath> nor <string>. Full Code:

#include <iostream>

bool leap_year(int year);
int main() {
    int year;
    while (true) {
        std::cout << "Enter a year (or negative number to quit): ";
        std::cin >> year;
        if (year < 0) {
            std::cout << "Bye!" << std::endl;
            break;
        }
        else if (leap_year(year)) {
            std::cout << "The year is a leap year." << std::endl;
        }
        else {
            std::cout << "The year is not a leap year." << std::endl;
        }
    }
}
bool leap_year(int year){
    bool is_leap_year = false;
    if (year % 4 == 0){
        is_leap_year = true;
    }
    if (year % 100 == 0){
        is_leap_year = false;
    }
    if (year % 400 == 0){
        is_leap_year = true;
    }
    return is_leap_year;
}
Stack Danny
  • 7,754
  • 2
  • 26
  • 55
  • 1
    The input stream should be checked for error after trying to read the integer, and appropriate action taken. Not a bad answer otherwise. – paddy Dec 05 '18 at 11:38
  • Thank you very much, Danny. May I ask why I need to delete the return 0? Also why you will add std:: before cout? (p.s. This really clears all my doubts. Very logical and easy to understand.) – Tsz Chun Leung Dec 05 '18 at 11:40
  • @TszChunLeung `return 0;` Exits the program immediately. It should only be put at the very last line of the `main()` function. (unless you want to return earlier because there's a serious error). – Stack Danny Dec 05 '18 at 11:46
  • @TszChunLeung your `return 0;` Statement was at the end of the *while* loop but not of the main function. (notice how there's an extra `}` after `return 0;`) – Stack Danny Dec 05 '18 at 11:48
  • @StackDanny Thank you very much. I really do. – Tsz Chun Leung Dec 05 '18 at 11:53
  • @TszChunLeung also the `std::` makes it more clear *what* exactly you're using. There could be some serious issues with the code when it gets complicated if you always use all namespaces. It's good practise to just always write the namespaces. again, [here's](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) more on that :) – Stack Danny Dec 05 '18 at 11:55
  • Simplest leap year test: `return ((year & 3) == 0 && ((year % 25) != 0 || (year & 15) == 0)); // returns true if leap year` – Kevin P. Rice Dec 10 '18 at 11:26