1

Just basically doing a project for myself. Basically writing a simple program to show your age and your horoscope sign. I am displaying the output I want except the age is wrong. I'm trying to figure out why. I'm thinking because I'm subtracting the year.

My Birthday is 7 14 1991

It outputs my age is 29. I am 28

Just wondering how I can put July instead of 7. Pretty sure i will have to do alot of if and then statements for the horoscope.

Here is the code

#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;

}
Scubba91
  • 11
  • 2
  • 4
    Several ways to skin that cat. The simplest is to just make an array of months, _e.g._ `std::string month_names[12] = { "January", "February, ... , "December" };` and then look that up later: `month_names[cust.Customer_Month - 1]`. – paddy May 11 '20 at 07:12
  • 1
    Please be more specific than "the age is wrong". How exactly is it wrong? – molbdnilo May 11 '20 at 07:14
  • try looking into https://stackoverflow.com/questions/3184121/get-month-name-from-month-number – Muhammad Hasan May 11 '20 at 07:15
  • 1
    @MuhammadHasan This is a C++ question. That C# question you link to won't help this person in the slightest. – paddy May 11 '20 at 07:18
  • For the horoscope, you can use a switch with range of values. https://gcc.gnu.org/onlinedocs/gcc/Case-Ranges.html#Case-Ranges Also, I cannot seem to reproduce your problem with the age. It shows the correct age. Although, I'd advice you to separate such operations with brackets. – Stf Kolev May 11 '20 at 08:22
  • well I am 28 years old but the output is 29. It's not checking my birthday didn't happen this year. – Scubba91 May 11 '20 at 20:03
  • That's right - all you're doing is subtracting years. So it's not a surprise that the result is wrong, is it? – Asteroids With Wings May 11 '20 at 20:05

2 Answers2

0

For displaying the month You need to store all the month names somewhere (preferably in an array) and then just refer to it (e.g. January would be array[0], August would be array[7] etc. , in general it's array[month - 1] if you have them all in the correct order)

The age problem... I didn't really get what You mean by "wrong", but I guess You think they're sometimes incorrect because You're not checking whether given person already had birthday this year or not (You need to compare months and days from current time and from the user's input and based on that subtract 1 from the age or not).

Pat Pac
  • 27
  • 3
  • Yeah thats my issue. Trying to figure out an argument I could put in it that could show the correct age. – Scubba91 May 11 '20 at 20:09
  • @Scubba91 No need to do any magic there. Just compare the current month with the user's month of birth. If current is lower You're all good with Your calculations. If current is higher, just subtract 1 from the age. If they're equal You need to also compare the days. – Pat Pac May 12 '20 at 11:45
0

Calculating age through the subtraction of years will cause errors in age calculation. It is necessary to judge the month and date before the age can be accurately calculated.

You could create a map to correspond to the month and the number, and then you can enter the month in English instead of the number, and use the map to get the number of the corresponding month when needed.

Here is the code:

#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;
}

enter image description here

Jeaninez - MSFT
  • 3,210
  • 1
  • 5
  • 20