为什么我会从我的职能部门收到一份会议声明
Why am I getting a meesed up cout statement from my function?
嗨,我正在开发一个程序,该程序应该显示两个日期中的后一个,它需要接受两个Date结构并返回后一个。后者为2014年4月31日,而后者为2014年04月30日。这是我的代码,我不明白为什么我会得到一个奇怪的长数字。。。谢谢你的帮助。
#include <iostream>
#include <iomanip>
using namespace std;
struct Date
{
int day;
int month;
int year;
};
int main()
{
void laterDate(Date, Date);
Date one;
Date two;
Date later;
cout << "Enter a Date starting with the day, then month, then year:n";
cin >> one.day;
cin >> one.month;
cin >> one.year;
cout << "nnEnter another date in the same fashion as above:n";
cin >> two.day;
cin >> two.month;
cin >> two.year;
cout << "nnThank you, I will now tell you which date is later then the other!" << endl;
laterDate(one, two);
system("pause");
return 0;
}
void laterDate(Date o,Date t)
{
Date later;
if (o.year >= t.year)
if (o.month >= t.month)
if (o.day > t.day)
{
later.day= o.day;
later.month = o.month;
later.year = o.year;
}
else
{
later.day = t.day;
later.month = t.month;
later.year = t.year;
}
cout << later.day << "/" << later.month << "/" << later.year << endl;
}
输出输入一个日期,从天开始,然后是月,然后是年:04302014年
以与上面相同的方式输入另一个日期:04312014年
谢谢你,我现在告诉你哪一天比另一天晚!-858993460/-858993460按任意键继续。
结束输出
您应该打破从显示结果的代码中比较日期的逻辑。比较可以完全排序的对象(如日期(的标准习惯用法是重载operator<
。
// the intent of this function is to return true if lhs precedes rhs,
// and return false otherwise (rhs precedes lhs, or they are equal)
bool operator<(Date const& lhs, Date const& rhs)
{
// first test the year.
// if the years differ, there is no need to test the month or the day
if (lhs.year < rhs.year) return true;
if (lhs.year > rhs.year) return false;
// years are equal, test the month
if (lhs.month < rhs.month) return true;
if (lhs.month > rhs.month) return false;
// months are equal, test the day
return lhs.day < rhs.day;
}
然后你的函数可以很容易地写成这样:
void laterDate(Date const& lhs, Date const& rhs)
{
Date const& later = (lhs < rhs) ? rhs : lhs;
cout << later.day << "/" << later.month << "/" << later.year << endl;
}
if (o.year > t.year ||
(o.year >= t.year && o.month > t.month) ||
(o.year >= t.year && o.month >= t.month &&
o.day > t.day))
{
later.day= o.day;
later.month = o.month;
later.year = o.year;
}
else
{
later.day = t.day;
later.month = t.month;
later.year = t.year;
}
通过这种方式,else将始终被调用,在您的实现中,它只在两个第一个条件为true时被调用,导致显示随机位
我想我的结构会有所不同。首先,我将输出Date
作为Date
类的友元函数,然后,我将重载>
运算符来比较Date
类实例。
struct Date
{
int day;
int month;
int year;
friend std::ostream &operator<<(std::ostream& out, const Date &d) {
return out << d.day << "/" << d.month << "/" << d.year;
}
bool operator>(const Date &t) const;
};
>
运算符的实现如下所示:
bool Date::operator>(const Date &t) const
{
if (year > t.year) return true;
if (year < t.year) return false;
if (month > t.month) return true;
if (month < t.month) return false;
return day > t.day;
}
然后你的main
程序是这样的:
int main()
{
Date one;
Date two;
std::cout << "Enter a Date starting with the day, then month, then year:n";
std::cin >> one.day >> one.month >> one.year;
std::cout << "nnEnter another date in the same fashion as above:n";
std::cin >> two.day >> two.month >> two.year;
std::cout << "nnThank you: " << (one > two ? one : two)
<< " is the later daten";
}
同样要注意的是,可以将输入操作符部分链接起来,如图所示。事实上,我倾向于声明一个输入运算符operator>>
,并将Date
结构提升为一个完整的类。
相关文章:
- 为什么我必须在C++中添加一个赋值符号来声明一个数组
- 有没有办法一次声明相同类型的多个对象,并通过一个表达式立即使用相同的右值初始化它们?
- 如果 C 函数仍然可以间接执行(通过回调函数),那么将它声明为静态函数是否是一种不好的做法?
- 在类的私有部分中声明一个数组,并在构造函数中分配数组值
- 如何声明一个数组,然后在另一行中赋值
- 如何插入一组已传递给已经声明的函数的值集
- 如何将对象定义为一种类型,然后再将其声明为子类型
- 有没有一种方法可以使用__declSpec(Align)来声明具有不同类型的对象
- 一条线和两行声明之间的差异
- 在 typedef 结构中声明一个数组
- 在循环中声明的对象是否只构造一次
- 有没有一种"safe"方法来声明 lippincott 函数,这样它就不会在捕获之外被调用?
- 不为模板化类分离函数声明和定义是一种好的做法吗
- 标记C 声明的一种便携式方式,为C 11所接受的弃用
- 我可以退回一份推荐信吗
- 打印一份动物清单
- 非静态成员函数,为每个对象创建生成一份机器码副本
- 为什么我会从我的职能部门收到一份会议声明
- 想在Hudson C/C++上找到一份工作
- C++ : 如何检测矢量中的重复项<string>并打印一份副本?