C++ program for checking whether the input is a leap year

廉价感情. 提交于 2020-07-03 05:42:08

问题


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;
}

回答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;
}



回答2:


#include <iostream>
using namespace std;

bool leap_year(int year) {
  return year % 400 == 0 || (year % 100 != 0 && year % 4 == 0);
}

int main() {
  int year;
  while (cout << "Enter a year (or negative number to quit): ") {
    cin >> year;
    if (year < 0) { // the number is negative, our criteria to quit
      cout << "Bye!" << endl;
      // returns from main() and hence terminates the program  
      return 0; 
    }
    if (leap_year(year)) { // The check should be done only once
      cout << "The year is a leap year." << endl;
    } else {
      cout << "The year is not a leap year." << endl;
    }
  }
  return 0;
}


来源:https://stackoverflow.com/questions/53630926/c-program-for-checking-whether-the-input-is-a-leap-year

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!