输出不显示日期是否有效
Output does not show if date is valid or not
我试图输出日期是否有效。Ie 5-6-2013有效,但62-34-2013无效。当我在main中输入一个合法的日期(5,5,2013)到我的对象时,它会通过开关运行并正常工作,但是当我输入一个奇怪的日期(例如,80,52,2013)时,它只输出日期但不会说"无效日期"我遗漏了什么?(请忽略我在整个代码中所有明显的注释-它们帮助我记住我在哪里,使它更可读)谢谢!
#include <iostream>
#include <cmath>
#include <string>
#include <stdio.h>
using namespace std;
class dateType
{
// defining the class
public:
void setDate(int month, int day, int year);
// Function to set the date
int getDay() const;
int getMonth() const;
int getYear() const;
void printDate() const;
dateType(int month, int day, int year);
dateType();
bool isLeapYear(int yNumber);
int dMonth;
int dDay;
int dYear;
private:
int yearCalc;
// integer to store the year, only numbers above 0 will be valid
int monthCalc;
// variable to store the month, January through December (1-12)
int dayCalc;
// variable to store the day, this varies depending on the month
// aka 28 days in February unless it is a leap year, 31 days January, etc.
int outputDate;
//
};
// function definitions - setDate needs to check for validity
void dateType::setDate(int month, int day, int year)
{
bool notValid;
// we need to use a switch in order to run through each month.
switch (month)
{
// months with 28 days - February (2), unless Leap year - test for Leap year
case 2:
cout << "February" << endl;
if (isLeapYear(year))
cout << "Leap year - 29 days" << endl;
if (day > 29)
notValid = true;
break;
// months with 31 days - April (4), June (6), September (9) November(11)
case 4:
cout << "April - valid, 30 days" << endl;
break;
case 6:
cout << "June - valid, 30 days" << endl;
break;
case 9:
cout << "September - valid, 30 days" << endl;
break;
case 11:
cout << "November - valid, 30 days" << endl;
if (day > 30)
notValid = true;
break;
// months with 31 days - January (1), March (3), May (5) July(7), August (8), October (10), December (12)
// will not incude break statement as we do not want to break
// out of switch until the 31 day checks have been run through
case 1:
cout << "January - valid, 31 days" << endl;
break;
case 3:
cout << "March - valid, 31 days" << endl;
// printing out as a test to validate these months
break;
case 5:
cout << "May - valid, 31 days" << endl;
break;
case 7:
cout << "July - valid, 31 days" << endl;
break;
case 8:
cout << "August - valid, 31 days" << endl;
break;
case 10:
cout << "October - valid, 31 days" << endl;
break;
case 12:
cout << "December - valid, 31 days" << endl;
if (day >= 32)
notValid = true;
cout << "This is not a valid entry" << endl;
break;
// now break as we have checked all months ending in 31 days
}
dMonth = month;
dDay = day;
dYear = year;
}
int dateType::getDay() const
{
return dDay;
}
int dateType::getMonth() const
{
return dMonth;
}
int dateType::getYear() const
{
return dYear;
}
void dateType::printDate() const
{
cout << dMonth << "-" << dDay << "-" << dYear;
}
dateType::dateType(int month, int day, int year)
{
setDate(month, day, year);
}
bool dateType::isLeapYear(int yNumber)
{
if ((yNumber % 4 == 0 && yNumber % 100 != 0) || (yNumber % 400 == 0))
return true;
else
return false;
}
int main()
{
int day;
int month;
int year;
dateType year1(80, 52, 2013);
cout << "Date: ";
year1.printDate();
system("pause");
return 0;
}
试试下面的代码:
#include <iostream>
#include <cmath>
#include <string>
#include <stdio.h>
using namespace std;
class dateType {
// defining the class
public:
void setDate(int month, int day, int year);
// Function to set the date
int getDay() const;
int getMonth() const;
int getYear() const;
void printDate() const;
dateType(int month, int day, int year);
dateType();
bool isLeapYear(int yNumber);
int dMonth;
int dDay;
int dYear;
private:
int yearCalc;
// integer to store the year, only numbers above 0 will be valid
int monthCalc;
// variable to store the month, January through December (1-12)
int dayCalc;
// variable to store the day, this varies depending on the month
// aka 28 days in February unless it is a leap year, 31 days January, etc.
int outputDate;
//
};
// function definitions - setDate needs to check for validity
void dateType::setDate(int month, int day, int year) {
bool notValid;
// we need to use a switch in order to run through each month.
switch (month) {
// months with 28 days - February (2), unless Leap year - test for Leap year
case 2:
cout << "February" << endl;
if (isLeapYear(year))
cout << "Leap year - 29 days" << endl;
if (day > 29)
notValid = true;
break;
// months with 31 days - April (4), June (6), September (9) November(11)
case 4:
cout << "April - valid, 30 days" << endl;
break;
case 6:
cout << "June - valid, 30 days" << endl;
break;
case 9:
cout << "September - valid, 30 days" << endl;
break;
case 11:
cout << "November - valid, 30 days" << endl;
if (day > 30)
notValid = true;
break;
// months with 31 days - January (1), March (3), May (5) July(7), August (8), October (10), December (12)
// will not incude break statement as we do not want to break
// out of switch until the 31 day checks have been run through
case 1:
cout << "January - valid, 31 days" << endl;
break;
case 3:
cout << "March - valid, 31 days" << endl;
// printing out as a test to validate these months
break;
case 5:
cout << "May - valid, 31 days" << endl;
break;
case 7:
cout << "July - valid, 31 days" << endl;
break;
case 8:
cout << "August - valid, 31 days" << endl;
break;
case 10:
cout << "October - valid, 31 days" << endl;
break;
case 12:
cout << "December - valid, 31 days" << endl;
break;
// now break as we have checked all months ending in 31 days
default:
cout << "This is not a valid entry" << endl;
break;
}
if (day >= 32) {
notValid = true;
cout << "This is not a valid entry" << endl;
}
dMonth = month;
dDay = day;
dYear = year;
}
int dateType::getDay() const {
return dDay;
}
int dateType::getMonth() const {
return dMonth;
}
int dateType::getYear() const {
return dYear;
}
void dateType::printDate() const {
cout << dMonth << "-" << dDay << "-" << dYear;
}
dateType::dateType(int month, int day, int year) {
setDate(month, day, year);
}
bool dateType::isLeapYear(int yNumber)
{
if ((yNumber % 4 == 0 && yNumber % 100 != 0) || (yNumber % 400 == 0))
return true;
else
return false;
}
int main() {
int day;
int month;
int year;
dateType year1(80, 52, 2013);
cout << "Date: ";
year1.printDate();
system("pause");
return 0;
}
你在switch(month)
中漏掉了default
病例。你还需要移动这个:
if (day >= 32) {
notValid = true;
cout << "This is not a valid entry" << endl;
}
开关块后的。如果月份是有效的,但日期不是,这将处理这种情况。您还错过了if块中的{}
。
如果month不在指定的值中,则不执行任何操作,只设置月份。因此,对于52,由于没有case 52
或default
,它只是将月份设置为52,这就是全部。
相关文章:
- 如果变量名称不跟在 char* 后面,const char* 是否有效?
- 钳制迭代器是否有效
- 检查由括号、方括号和大括号组成的一组方括号是否有效?
- 在函数内创建的对象的范围 - 如果在函数外部存储和访问引用,它们是否有效?
- 模板签名解析为 void(void) 被 GCC 拒绝;这是否有效C++?
- 我如何知道作为参数的size_t在函数中是否有效?
- 我的运算符重载是否有效<<(流插入)左操作数不是 ostream
- C++ 返回指向函数内定义的静态数组的指针是否有效?
- 此递归模板类型定义是否有效C++?
- 将 C 函数转换为 C++ 以检查数字是否有效
- 函数参数的名称与调用函数时使用的变量相同是否有效?
- 如何检查输入是否有效?
- 如何检查用户的输入是否有效以及我正在寻找的数字?
- 在函数中按值传递 unordered_map/unordered_set 是否有效? C++
- 如何检查isupper(cstr)是否有效?
- 将 std::transform 与 std::back_inserter 一起使用是否有效?
- 只需要知道我在c ++中打印模式的方式是否有效,或者有另一种方法可以有效地做到这一点
- 如何检查字符串格式在读取C++文本文件时是否有效?
- 在 c++ 中将对象设置为等于同一类的构造函数是否有效?
- 创建加密安全密码.并验证它是否有效