闰年基本的C ++布尔总是返回true [英] Leap Year Basic C++ Boolean Always returns true

查看:127
本文介绍了闰年基本的C ++布尔总是返回true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写这篇code,以确定所输入的一年是否是闰年。这意味着,那些由4整除的400是闰年,并通过100或别的东西都没有。

不过,我的程序总是返回布尔值true,这样输出才会相信每年为闰年。

下面是我的code迄今:

 的#include<&iostream的GT;
#包括LT&;&CMATH GT;
#包括LT&;串GT;
#包括LT&;&了iomanip GT;使用命名空间std;
 布尔leap_year(INT年);
诠释的main()
{
    INT年;
     布尔leap_year(INT年);
    COUT<< 有问题请输入年份:;
    CIN>>年;
    如果(leap_year == FALSE)
    {
        COUT<< 这一年是不是闰年。
    }
    其他
    {
        COUT<< 今年是闰年。
    }
    返回0;
}
  布尔leap_year(INT年)
{    如果(年%4 == 0)
    {
        布尔leap_year = TRUE;
    }
    否则,如果(每年400%== 0)
    {
        布尔leap_year = TRUE;
    }
    否则,如果(每年100%== 0)
    {
        布尔leap_year = FALSE;
    }
    其他
    {
        布尔leap_year = FALSE;
    }    如果(布尔leap_year = FALSE)
    {
        返回false;
    }
    其他
    {
        返回true;
    }
}


解决方案

您在声明一堆局部变量

 否则如果(每年100%== 0)
{
    布尔leap_year = FALSE;
}

一旦}括号,这个变量超出作用域也是如此你已经存储的数据。

 如果(布尔leap_year = FALSE)
{
    返回false;
}

这是定义一个变量leap_year并赋予其假。如果将反过来将评估虚假的,因此它总是会去别的条件。

我已经冒昧地重写程序的一部分。现在,您可以看到呼叫正在对该函数。另外,在函数的局部变量is_leap_year用于存储返回值,并最终被退回。我也纠正了逻辑,如前面第一个%4检查将是真实的,并没有其他的if语句将被执​​行,这是不是你想要的。

 的#include<&iostream的GT;
#包括LT&;&CMATH GT;
#包括LT&;串GT;
#包括LT&;&了iomanip GT;使用命名空间std;
布尔leap_year(INT年);
诠释的main()
{
    INT年;
    COUT<< 有问题请输入年份:;
    CIN>>年;
    如果(leap_year(年)== FALSE)//调用函数和检查,如果回报是假的
    {
        COUT<< 这一年是不是闰年。
    }
    其他
    {
        COUT<< 今年是闰年。
    }
    返回0;
}
  布尔leap_year(INT年)
{
    布尔is_leap_year = FALSE;
    如果(年%4 == 0)
    {
       is_leap_year = TRUE;
    }
    如果(每年100%== 0)
    {
        is_leap_year = FALSE;
    }
    如果(年%400 == 0)
    {
        is_leap_year = TRUE;
    }
    返回is_leap_year;
}

I wrote this code to determine whether or not an inputted year is a leap year. Meaning, those divisible by 4 and 400 are leap years, and by 100 or something else are not.

But, my program always returns true for the boolean value, so that output will believe every year is a leap year.

Here's my code so far:

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

using namespace std;
 bool leap_year(int year);
int main()
{
    int year; 
     bool leap_year(int year);
    cout << "Please input the year in question: ";
    cin >> year;
    if (leap_year == false)
    {
        cout << "The year is not a leap year.  ";
    }
    else
    {
        cout << "The year is a leap year. ";
    }
    return 0;
}
  bool leap_year(int year)
{

    if (year % 4 == 0)
    {
        bool leap_year = true;
    }
    else if (year % 400 == 0)
    {
        bool leap_year = true;
    }
    else if (year % 100 == 0)
    {
        bool leap_year = false;
    }
    else
    {
        bool leap_year = false;
    }

    if (bool leap_year = false)
    {
        return false;
    }
    else
    {
        return true;
    }
}

解决方案

You are declaring a bunch of local variables

else if (year % 100 == 0)
{
    bool leap_year = false;
}

As soon as the } parentheses, this variable goes out of scope and so does the value you have stored.

if (bool leap_year = false)
{
    return false;
}

This is defining a variable leap_year and assigning it false. The if in turn will evaluate false, hence it will always go to else condition.

I have taken the liberty to rewrite a portion of the program. You can now see how the call is being made to the function. Also in the function a local variable is_leap_year is used to store the return value, and finally being returned. I have also corrected the logic, as earlier the first %4 check would be true and none of the other if statements would be executed, which is not what you want.

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

using namespace std;
bool leap_year(int year);
int main()
{
    int year; 
    cout << "Please input the year in question: ";
    cin >> year;
    if (leap_year(year) == false)  //Call the function and check if return is false
    {
        cout << "The year is not a leap year.  ";
    }
    else
    {
        cout << "The year is a leap year. ";
    }
    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;
}

这篇关于闰年基本的C ++布尔总是返回true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆