用于检查输入是否为leap年的C ++程序 [英] C++ program for checking whether the input is a leap year
问题描述
我是Tony,是C ++编程的新手.我想问一个有关创建程序来检查leap年的问题.
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.
在以下代码中,我尝试创建一个布尔函数来检查输入是否为a年.如果输入为负,我将提示再见!"并立即停止该程序.如果输入为正,那么我将使用自己构建的布尔函数检查是否为a年,直到输入为负数,然后退出程序.
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;
}
推荐答案
首先,您(应该)需要一个while(true)
循环而不是一个while(std::ostream)
循环.
First of all, you (should) want a while(true)
loop and not a while(std::ostream)
loop.
所以替换
while (cout << "Enter a year (or negative number to quit): ")
{
使用
while (true)
{
cout << "Enter a year (or negative number to quit): ";
正如@paddy指出的那样,您可以检查std :: ostream的返回类型以在打印时查找错误.但是在这个简单的程序中,我怀疑是否有必要.
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.
然后,在if语句之外有break
,它将始终脱离程序(无论输入如何).替换
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;
使用
if (year < 0)
{
cout << "Bye!" << endl;
break;
}
(无需检查负输入是否为a年.您可以使用 if-else
语句,因此您也可以像我一样将if(leap_year(year) == false && year < 0)
替换为if (year < 0)
.)
(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.)
将其应用于所有语句(不更改其内部逻辑)并在循环末尾删除return 0;
时,您将获得所需的程序流.同样,删除using namespace std;
更好(请在此处 ).您也不需要包含<iomanip>
,<cmath>
或<string>
.完整代码:
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;
}
这篇关于用于检查输入是否为leap年的C ++程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!