用于检查输入是否为a年的C ++程序

时间:2018-12-05 11:10:15

标签: c++ leap-year

我是Tony,是C ++编程的新手。我想问一个有关创建程序来检查leap年的问题。

在以下代码中,我尝试创建一个布尔函数来检查输入是否为a年。如果输入为负,我将提示“再见!”并立即停止该程序。如果输入为正,那么我将使用构建的布尔函数检查是否为is年,直到输入为负数,然后退出程序。

但是,我无法找到自己所犯的错误以及当前的状况,当我输入一个正值时,不会产生任何结果。如果有空,请帮助。非常感谢您。 :)

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

2 个答案:

答案 0 :(得分:1)

首先,您(应该)需要一个while(true)循环而不是一个while(std::ostream)循环。

所以替换

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

使用

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

正如@paddy指出的那样,您可以检查std :: ostream的返回类型以在打印时查找错误。但是在这个简单的程序中,我怀疑是否有必要。

然后,在if语句之外有break,它将始终脱离程序(无论输入如何)。替换

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

使用

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

(无需检查负输入是否为a年。您可以使用if-else statments仅输入1个if陈述,因此也可以将if(leap_year(year) == false && year < 0)替换为if (year < 0);就像我一样。)

将其应用于所有语句(不更改其内部逻辑)并在循环结束时删除return 0;时,您将获得所需的程序流。同样,删除using namespace std;更好(请阅读here为什么)。您也不需要包括<iomanip><cmath><string>。完整代码:

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

答案 1 :(得分:-1)

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