【问题标题】:Why am I getting the wrong Age为什么我的年龄错误
【发布时间】: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


【解决方案1】:

为了显示月份您需要将所有月份名称存储在某个地方(最好是在一个数组中),然后只引用它(例如,一月将是数组 [0],八月将是数组 [7] 等,一般来说它是数组[月 - 1],如果你把它们都按正确的顺序排列)

年龄问题...我并没有真正理解你所说的“错误”是什么意思,但我猜你认为它们有时是不正确的,因为你没有检查给定的人 今年是否已经过生日 与否(您需要比较当前时间和用户输入的月份和日期,并基于是否从年龄中减去 1)。

【讨论】:

  • 是的,那是我的问题。试图找出一个我可以提出的论点,它可以显示正确的年龄。
  • @Scubba91 不需要在那里做任何魔法。只需将当前月份与用户的出生月份进行比较。如果电流较低,您的计算结果都很好。如果电流更高,只需从年龄中减去 1。如果它们相等,您还需要比较天数。
【解决方案2】:

通过减去岁数来计算年龄会导致年龄计算错误。在准确计算年龄之前,需要判断月份和日期。

您可以创建一个与月份和数字对应的地图,然后您可以用英文输入月份而不是数字,并在需要时使用地图获取对应月份的数字。

代码如下:

#include <iostream>
#include <ctime>
#include <string>
#include <map>
using namespace std;

map<string, int> monthMap;


struct Customer_info
{
    string Customer_Name;
    string Customer_Month;
    int Customer_Day;
    int Customer_Year;
};

int main()

{
    string monthString[12]{ "January" ,"February","March","April","May","June","July","August","September","October","November","December" };
    for (int i = 0; i < 12; i++)
    {
        monthMap.insert(pair<string, int>(monthString[i], i + 1));
    }

    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;
    if (date->tm_mon + 1 < monthMap[cust.Customer_Month] || (date->tm_mon + 1 == monthMap[cust.Customer_Month] && date->tm_mday < cust.Customer_Day))
        age--;
    cout << "Your age is " << age << endl;

    return 0;
}

【讨论】:

  • 你有任何更新吗?如果您的案例已经解决,请帮忙标记答案。如果没有,请随时与我们联系。
猜你喜欢
  • 2012-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多