Leap Year Basic C++Boolean始终返回true
Leap Year Basic C++ Boolean Always returns true
我写这段代码是为了确定输入的年份是否是闰年。也就是说,那些可以被4和400整除的年份是闰年,而被100或其他东西整除的则不是闰年。
但是,我的程序总是为布尔值返回true,这样输出就会认为每年都是闰年。
到目前为止,这是我的代码:
#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;
}
}
您正在声明一堆局部变量
else if (year % 100 == 0)
{
bool leap_year = false;
}
一旦出现}括号,这个变量就会超出范围,您存储的值也会超出范围。
if (bool leap_year = false)
{
return false;
}
这是定义一个变量leap_year并将其赋值为false。if反过来将求值为false,因此它将始终转到else条件。
我有权改写程序的一部分。现在,您可以看到对函数的调用是如何进行的。此外,在函数中,局部变量is_leap_year用于存储返回值,并最终返回。我还纠正了逻辑,因为前面的第一个%4检查将为true,而不会执行其他if语句,这不是您想要的。
#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;
}
它应该是IF((年份%4==0&&年份%100!=0)||(年份%400==0))return ISLEAPYEAR。
//You can simplify it like this:
bool leap_year(int year)
{
bool leap_year = false;
if ((year % 4 == 0 && year%100 !=0) || year % 400 == 0)
{
bool leap_year = true;
}
return leap_year;
}
为了避免这种情况:
if (leap_year = false)
{
return false;
}
您可以检查类似的布尔值
if (leap_year)
{
// code to execute when leap_year is true
}
您的问题是您是否返回true
或false
的条件是:
if (bool leap_year = false)
{
return false;
}
else
{
return true;
}
您正在将值false
分配给在条件语句中初始化的变量leap_year
。赋值运算符被定义为:T& T::operator =(const T2& b)
,这意味着你总是在你的条件下计算false
。
解决这个问题的一种方法是在bool leap_year(int year)
的顶部声明leap_year
,而不是每次使用它(这是毫无意义的行为)
bool leap_year(int year) {
bool leap_year = false;
if (year % 4 == 0) {
leap_year = true;
} else if (year % 400 == 0) {
leap_year = true;
} else if (year % 100 == 0) {
leap_year = false;
}
if (leap_year == false) {
return false;
} else {
return true;
}
}
但更好的解决方案是使用C++已经提供的功能:
bool leap_year(int year) {
tm bar = { 0, 0, 0, 29, 1, year - 1900 };
return static_cast<time_t>(-1) != mktime(&bar) && bar.tm_mday == 29 && bar.tm_mon == 1 && bar.tm_year == year - 1900;
}
[实际示例]
相关文章:
- flutter:即使shouldRepaint()返回true,自定义画家也不会重新绘制
- C++如果使用 lambda 表达式的语句返回 true,但输出来自 false,为什么
- MFC 的 OnInit() 函数中的返回 true 和返回 false 有什么区别
- 我正在尝试创建一个布尔函数,该函数计算字符并在字符为"()*-+"时返回 true
- 创建一个函数,如果元素在unordered_set中,则返回 true,如何处理模板
- QTimer isActive 返回 true,但 remainingTime 返回 -1
- 为什么在 IsValid(Object) 返回 true 后不能安全地使用 Object?
- 计算 <Classtype*> 向量中所有项的布尔值的最有效方法,如果全部为真则返回 true
- QDir mkpath 返回 true,但未创建目录
- std::is_array 当它应该返回 false 时返回 true
- 尝试创建一个评估字符的bool函数,如果字母数字为字母,则返回true
- 我想返回 True/False 关于值是否在一组值中
- 为什么我的布尔函数返回 true 会导致读取访问冲突?
- ifstream::is_open 返回 true,即使该位置不存在文件也是如此
- 为什么包含指针的条件总是返回 true?
- 对于 ((无符号整数)0-1)返回 true>0
- 为什么#ifndef __func__返回true
- 我的bool函数一直返回true,我不知道为什么
- 对表达式求值,直到表达式返回true
- 在{8,4,6,2}中搜索4时,std::binary_search是否有任何实现将返回true