【发布时间】:2020-08-26 15:50:48
【问题描述】:
基本上只是为自己做一个项目。 基本上是写一个简单的程序来显示你的年龄和星座。 我正在显示我想要的输出,但年龄是错误的。 我试图找出原因。我在想,因为我要减去年份。
我的生日是 1991 年 7 月 14 日
它输出我的年龄是 29。我是 28
只是想知道我怎么能把七月而不是七月。 很确定我将不得不为星座做很多 if 和 then 语句。
这里是代码
#include <iostream>
#include <ctime>
#include <string>
using namespace std;
struct Customer_info
{
string Customer_Name;
int Customer_Month;
int Customer_Day;
int Customer_Year;
};
int main()
{
Customer_info cust;
cout << "What is your name? And when exactly is your birthday?" << endl;
getline (cin, cust.Customer_Name);
cin >> cust.Customer_Month;
cin >> cust.Customer_Day;
cin >> cust.Customer_Year;
cout << "Your name is " << cust.Customer_Name << " " << "and you were born " << cust.Customer_Month << endl;
time_t now = time(0);
tm *date = localtime(&now);
cout << "The date is " << 1 + date->tm_mon << " / " << date->tm_mday << " / " << 1900 + date->tm_year << endl ;
int age;
age = 1900 + date-> tm_year - cust.Customer_Year;
cout << "Your age is " << age << endl;
return 0;
}
【问题讨论】:
-
剥那只猫的几种方法。最简单的方法是创建一个月份数组,例如
std::string month_names[12] = { "January", "February, ... , "December" };,然后再查看:month_names[cust.Customer_Month - 1]。 -
请比“年龄错误”更具体。到底错在哪里?
-
@MuhammadHasan 这是一个 C++ 问题。您链接到的那个 C# 问题对这个人毫无帮助。
-
对于星座,您可以使用具有值范围的开关。 gcc.gnu.org/onlinedocs/gcc/Case-Ranges.html#Case-Ranges 另外,我似乎无法重现您的年龄问题。它显示了正确的年龄。虽然,我建议你用括号分隔这些操作。
标签: c++ visual-studio datetime ctime