2

So I'm writing a program and I want to have some user input. Let's say we define an int; called age. If the user is an adult (let's say 30 and up), I want the program to swap all the informal "you"'s to "formal" you's (many languages, like French, have this distinction, think of vous vs tu) In Dutch, "u" is formal as opposed to "je".

What would be the most concise way of doing this? I have this right now (using the date of today, 18 Sept 2015):

string abc;

if (2015 - year of birth > 30) {
    abc = "u";
}
else {
    abc = "je";
}
if (2015 - year of birth  == 30) {
    if ( September - month of birth  > 0) {
        abc = "u";
    }
    else {
        abc = "je";
    }
 }
 if (2015 - year of birth == 30) {
     if (September - month of birth == 0) {
         if (18 - day of birth >= 0) {
            abc = "u";
         }
         else {
            abc = "je";
         }
     } 
 }

I'm pretty sure it works but is probably suboptimal. How can I improve this?

Ilya
  • 4,583
  • 4
  • 26
  • 51
user29855
  • 59
  • 5

6 Answers6

5

I'm assuming you may really need to compare against today's date, so get that using std::chrono and localtime - the latter unpacks the date into a std::tm for you: do note in the linked docs that the year and month are 1900- and 0-based respectively.

The std::tie is convenient to group the values into a tuple for easy comparison, such that if the left-most value is equal it considers the one to the right and so on.

auto now = std::chrono::system_clock::now();
auto my_time_t = std::chrono::system_clock::to_time_t(now);
std::tm* p = std::localtime(&my_time_t);

string abs = std::tie(p->tm_year + 1900 - 30, p->tm_mon + 1, p->tm_mday) >
             std::tie(year_of_birth, month_of_birth, day_of_birth)
             ? "je" : "u";

If you don't actually need today's date, just ditch the top 3 lines and hardcode the p->tm_xxx + ... values.

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
1

You could do something like

string abc="u";

if ((2015 - year of birth < 30) ||
    (2015 - year of birth ==30 && September- month of birth < 0) ||
    (2015 - year of birth ==30 && September- month of birth ==0 && 18 - day of birth < 0)) 

abc ="je";
Jeremy
  • 11
  • 1
1

The simplest way to do it is to implement/use function "number of days between two dates". You can use it this way if mistake in few days is acceptable for your task:

int difference = currentDate - birthday; // nb of days between today and birthday of user
abc = (difference >= 30 * 365) ? "u" : "je"; // 365 is approx. number of days in a year

To do it you can read these questions:

  1. Number of days between two dates C++,
  2. How to calculate how many days between two dates in c++ visual studio?,
  3. How to get the number of days between two dates using boost::date_time,
  4. Determining the difference between dates
Community
  • 1
  • 1
Ilya
  • 4,583
  • 4
  • 26
  • 51
  • 1
    *"number of days between two dates"* - as long you don't care about leap years, or factor that in rather than use the suggested `30 * 365`, yes. – Tony Delroy Sep 18 '15 at 07:59
  • @TonyD, thank you for pointing on this! Yes, I expect, that mistake in few days is acceptable in this task. I've updated my answer. – Ilya Sep 18 '15 at 08:21
0

Without the proper accolades its pretty difficult to read. In your code, you have to little accolades. But to make it a little shorted, makes it look better aswel.

string abc = "je";

if (2015 - year of birth > 30) abc = "u";
else if ( 2015 - year of birth  == 30){
    if ( September - month of birth  > 0) abc = "u";
    else if (September - month of birth == 0){ 
        if (18 - day of birth >= 0) abc = "u";
    } 
}

Now you have the string abc as "je". this way, you don't need all the else's. If one of the conditions is true, it makes it "u". Should work the same.

EDIT: It is actually better to do it like jeremy's answer.

wouter140
  • 255
  • 2
  • 12
0

What you are trying to achieve is if some one is a single day greater than 30 years, then the string will be, abc = 'u' else it will be abc = "je".

Now the approach you are using is static i.e. you are using a specific date in your program. You can solve the above very easily using ctime library in c++.

This link has the program you are searching for. I hope this helps :)

DarKnight
  • 83
  • 10
0

Firstly I would convert the birth date to milliseconds since epoch and compare to the current time since epoch. This would greatly simply your code as you would have 1 test for address type.

This thread explains the process

Math to convert seconds since 1970 into date and vice versa

Then you can simple

if (ageInMilliseconds >= formalAgeInMilliseconds)

Community
  • 1
  • 1
kaifong
  • 342
  • 2
  • 6