Changing from "Year to Age"

Hello there, good day.
Is there a way for me to change when a user enter a year (eg: 1959), it will print out (age = 60)? Assuming that the current year is 2019.

1
2
3
 	printf("\nEnter your birth year : ");
	scanf("%d", &birth_year);
	printf("Age : ?? ");


Ps, given the condition that we can only use "stdio.h"
Last edited on
Seeing as there's no way for the program to be accurate to the month/day (most people born in 1959 are still 59 today), a simple 2019-birth_year would suffice. Perhaps add in a check to make sure it isn't negative.

printf("Age : %d", 2019 - birth_year);
Last edited on
if you need to get the current year instead of hard code 2019, you could do that but its rather clunky with ONLY stdio.
you're right, that makes sense. i guess i'll just assume everyone born in 1959 is 60.

thanks everyone!

yes @jonnin, unfortunately we are only limited to stdio.h
Last edited on
but do you want the current year or just 2019?
Nick said "assuming the current year is 2019", so to me that means "just hardcode it".

But I'm curious, jonnin, what would your method be of determining the current year without #including <time.h> or <stdlib.h>? (Assuming that <stdio.h> doesn't implicitly include them).

Perhaps there's some sort of file-based solution, though that's probably OS-dependent.
Last edited on
I was thinking you could get the create timestamp off a file. I can't recall what C offers, but it seemed possible. Hopefully he does not need it, it would be an example of how not to do stuff ...
Would it be reasonable to ask the current date?
@jonnin , i dont it to be the current year, just hardcode it as 2019 is the current year. And yes, i can only include stdio and math.io as im still just starting up on coding c.

Thanks for the extra info tho. Appreciate it.

Actually one more thing (side question), why is it when i run the code on my laptop (2017 visual studio), it compiles and runs without any errors but when i try it on my college’s computer (2010 visual studio), it gives a lot of errors when i try running it also prompts something like “project out of date” when i try to run it there. Let me know if u need screenshots

Is it the same project/solution you're trying to run on on both 2017 and 2010 VS? They might not always be compatible. Copy the source text, not the project file/structure.
Topic archived. No new replies allowed.