| | |
New to C program code to calculate age
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
I want this program to calculate an actors age at the time a movie they made was released, the user inputs all the data... I dont know C just learning but this is the layout I thought of any help would be appreciated, this does not compile
C Syntax (Toggle Plain Text)
#include <stdio.h> int main(void) { char: actors_name[40]; scanf("%s", actors_name); char: film_name[40]; scanf("%s", film_name); int bday; int releaseyear; int age int filmage; printf("What is the actors name?\n"); scanf("%s", actorname); printf("What is the film name?\n"); scanf("%s", film_name); printf("What is the actors birthday?\n); scanf("%d", bday); printf("What is the films release year?\n"); scanf("%d", &releaseyear); 2008- bday = age 2008 - filmrelease = filmage printf("When %s, actorname filmed %s, film_name he was %sage, years old. %s, actorname is currently %age, and he is a Zodiac sIGN, %sfilmname was made %s, filmage) }
•
•
Join Date: Jan 2008
Posts: 3,831
Reputation:
Solved Threads: 501
•
•
•
•
I want this program to calculate an actors age at the time a movie they made was released, the user inputs all the data... I dont know C just learning but this is the layout I thought of any help would be appreciated, this does not compile
C Syntax (Toggle Plain Text)
#include <stdio.h> int main(void) { char: actors_name[40]; scanf("%s", actors_name); char: film_name[40]; scanf("%s", film_name); int bday; int releaseyear; int age int filmage; printf("What is the actors name?\n"); scanf("%s", actorname); printf("What is the film name?\n"); scanf("%s", film_name); printf("What is the actors birthday?\n); scanf("%d", bday); printf("What is the films release year?\n"); scanf("%d", &releaseyear); 2008- bday = age 2008 - filmrelease = filmage printf("When %s, actorname filmed %s, film_name he was %sage, years old. %s, actorname is currently %age, and he is a Zodiac sIGN, %sfilmname was made %s, filmage) }
You have some syntax errors.
c Syntax (Toggle Plain Text)
#include <stdio.h> int main(void) { char: actors_name[40]; scanf("%s", actors_name); char: film_name[40]; scanf("%s", film_name); int bday; int releaseyear; int age int filmage; printf("What is the actors name?\n"); scanf("%s", actorname); printf("What is the film name?\n"); scanf("%s", film_name); printf("What is the actors birthday?\n); scanf("%d", bday); printf("What is the films release year?\n"); scanf("%d", &releaseyear); 2008- bday = age 2008 - filmrelease = filmage printf("When %s, actorname filmed %s, film_name he was %sage, years old. %s, actorname is currently %age, and he is a Zodiac sIGN, %sfilmname was made %s, filmage) }
Lines 4 and 6. Get rid of the colons.
Lines 4 - 11: Put all of the variable declarations before any of the scanf statements.
Lines 18 and 20: You are reading in integers on both of these, yet you have the & in one, but not the other. See the documentation for scanf for the proper syntax:
http://www.cplusplus.com/reference/c...dio/scanf.html
Lines 22 and 23: Put the stuff on the left side of the equals sign on the right side and vice versa.
Line 26: See the documentation for printf and look at the examples in both this link and the scanf link above for the proper syntax:
http://www.cplusplus.com/reference/c...io/printf.html
Also watch your variable names and quotes. These need to match (i.e. there's no variable called filmrelease).
Last edited by VernonDozier; Jul 2nd, 2008 at 10:42 pm.
•
•
Join Date: Jan 2008
Posts: 3,831
Reputation:
Solved Threads: 501
•
•
•
•
Besides the syntax errors, would this program execute what I need it to do?
Well I don't see where the Zodiac sign comes in (Do you want to display "Cancer", "Leo", etc.?). Also, you have bday, standing for birthday, being represented by single integer. It sounds like you want the year to figure out the age and you need the month and day for the Zodiac sign, so can you fit that all in one integer? What do you want the user to enter for the birthday? And do you want birthdate instead? If you throw out the Zodiac sign you can just use an integer for the birth YEAR, which is what it appears you are calculating. You can also use the struct_tm data type:
http://www.cplusplus.com/reference/c.../ctime/tm.html
but if you are completely new to C, I wouldn't do that yet unless you are already familiar with the concept of a struct.
My first thought was to use the time functions in time.h standard header file, but the problem with that is that the earliest date that can be use by those functions is 1970, which clearly will not work for your program. So you will have to do the calculations yourself.
If you convert the date to days since year 0 then the calculation of the age will very simple -- just subtract two integers.
For example: I was born February 3, 1943. So converting that to days yields
Now do the same thing for today's date (2 July 2008), then subtract 709,710 from that value.
If you convert the date to days since year 0 then the calculation of the age will very simple -- just subtract two integers.
For example: I was born February 3, 1943. So converting that to days yields
C Syntax (Toggle Plain Text)
days from year 0 to 1942 = 709,192 (ignoring leap years) Number of leap years = 1942/4 = 485 Number of days from 1 Jan 1943 to 2 February 1943 = 31 (Jan) + 2 (Feb) = 33 Now just add all those together yields 709,710
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
there was no "year 0". what we now call 1 BC was followed directly by what we now call 1 AD. (or 1 BCE followed by 1 CE, if you prefer)
When the Gregorian Calendar was adopted, they "threw out" 10 days to realign the calendar with the sun.
and changed leap years so they do not cycle once every 4 years. they skip years divisible by 100, excepting those also divisible by 400. 1900 was not a leap year. neither was 1800 or 1700. but 2000 was.
there are other problems reconciling the Julian calander, which was lunisolar, with the current Gregorian.
in any event, you're not going to have an accurate count of the number of days over the past 2 millenia.
When the Gregorian Calendar was adopted, they "threw out" 10 days to realign the calendar with the sun.
and changed leap years so they do not cycle once every 4 years. they skip years divisible by 100, excepting those also divisible by 400. 1900 was not a leap year. neither was 1800 or 1700. but 2000 was.
there are other problems reconciling the Julian calander, which was lunisolar, with the current Gregorian.
in any event, you're not going to have an accurate count of the number of days over the past 2 millenia.
•
•
•
•
there was no "year 0". what we now call 1 BC was followed directly by what we now call 1 AD. (or 1 BCE followed by 1 CE, if you prefer)
When the Gregorian Calendar was adopted, they "threw out" 10 days to realign the calendar with the sun.
and changed leap years so they do not cycle once every 4 years. they skip years divisible by 100, excepting those also divisible by 400. 1900 was not a leap year. neither was 1800 or 1700. but 2000 was.
there are other problems reconciling the Julian calander, which was lunisolar, with the current Gregorian.
in any event, you're not going to have an accurate count of the number of days over the past 2 millenia.
•
•
Join Date: Jan 2008
Posts: 3,831
Reputation:
Solved Threads: 501
Well, do you care whether it's accurate for the past two millenia? struct_tm goes back to 1900. I'm thinking jephthah might have been writing this tongue-in-cheek. I wouldn't worry about it unless there is a reason to, and if this is just a book example, there isn't. Easiest solution is to have three integer variables, one for each of the following. Ask the user their birth-month, birth-day, and birth-year, and do the calculations from there. It'll get you more used to programming, if that's your goal. Later, when you get a little more experienced, it may be worth your while to reprogram it using struct_tm or some of the functions Ancient Dragon was referring to. If your whole goal is simply to learn, just enter in years after 1970 and maybe have it display an error message if someone types in something before 1970.
Last edited by VernonDozier; Jul 3rd, 2008 at 1:44 am. Reason: misspelled "jephthah"
^ wasnt tongue-in-cheek, although you might say it was tangential 
just pointing out there are some difficult issues reconciling the different calendar systems if you try and go back two millenia. ...
however you can do this exercise exactly as Ancient Dragon said, and you will get a correct answer even though the premise is flawed... the absolute # days past this arbitrary (and imaginary) "year 0" will be wrong, but the "delta" in number of days between a persons relative birth-day and relative current-day will be correct. (assuming they weren't born in 1900 or earlier...)
it just seems to make more sense to start from a baseline of the year 1900 instead of the imaginary year 0 because then you're not switching between entire calendar systems.
or, as Vernon noted, just learn how to use the struct_tm functions, since this is what they were designed to do.

just pointing out there are some difficult issues reconciling the different calendar systems if you try and go back two millenia. ...
however you can do this exercise exactly as Ancient Dragon said, and you will get a correct answer even though the premise is flawed... the absolute # days past this arbitrary (and imaginary) "year 0" will be wrong, but the "delta" in number of days between a persons relative birth-day and relative current-day will be correct. (assuming they weren't born in 1900 or earlier...)
it just seems to make more sense to start from a baseline of the year 1900 instead of the imaginary year 0 because then you're not switching between entire calendar systems.
or, as Vernon noted, just learn how to use the struct_tm functions, since this is what they were designed to do.
Last edited by jephthah; Jul 3rd, 2008 at 1:19 pm.
![]() |
Similar Threads
- AverageAge not displaying. (C++)
- Pascal simple interest code (Pascal and Delphi)
- Calculate Federal Tax (C++)
- Why do they make computers harder to use and program? (Geeks' Lounge)
- a survey program (C++)
- Pseudo code help please (Computer Science)
- age calculator (Computer Science)
- Creating a GUI that accepts user input help (Java)
- New to Java, please help with first Assignment (Java)
Other Threads in the C Forum
- Previous Thread: Program to compute max and min values from input - HELP!
- Next Thread: How do I clear the screen?
| Thread Tools | Search this Thread |
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax directory dynamic fflush file fork forloop frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest kernel km linked linkedlist linux linuxsegmentationfault list lists locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes radix read recursion recv repetition research scanf scheduling scripting segmentationfault send sequential shape socketprograming stack standard string strings structures systemcall testautomation turboc unix user variable voidmain() wab win32api windows.h






