New to C program code to calculate age

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2008
Posts: 10
Reputation: newbiecoderguy is an unknown quantity at this point 
Solved Threads: 0
newbiecoderguy's Avatar
newbiecoderguy newbiecoderguy is offline Offline
Newbie Poster

New to C program code to calculate age

 
0
  #1
Jul 2nd, 2008
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

  1. #include <stdio.h>
  2. int main(void)
  3. {
  4. char: actors_name[40];
  5. scanf("%s", actors_name);
  6. char: film_name[40];
  7. scanf("%s", film_name);
  8. int bday;
  9. int releaseyear;
  10. int age
  11. int filmage;
  12.  
  13. printf("What is the actors name?\n");
  14. scanf("%s", actorname);
  15. printf("What is the film name?\n");
  16. scanf("%s", film_name);
  17. printf("What is the actors birthday?\n);
  18. scanf("%d", bday);
  19. printf("What is the films release year?\n");
  20. scanf("%d", &releaseyear);
  21.  
  22. 2008- bday = age
  23. 2008 - filmrelease = filmage
  24.  
  25.  
  26. printf("When %s, actorname filmed %s, film_name
  27. he was %sage, years old. %s, actorname is currently
  28. %age, and he is a Zodiac sIGN, %sfilmname was made %s, filmage)
  29.  
  30. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,831
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: New to C program code to calculate age

 
0
  #2
Jul 2nd, 2008
Originally Posted by newbiecoderguy View Post
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

  1. #include <stdio.h>
  2. int main(void)
  3. {
  4. char: actors_name[40];
  5. scanf("%s", actors_name);
  6. char: film_name[40];
  7. scanf("%s", film_name);
  8. int bday;
  9. int releaseyear;
  10. int age
  11. int filmage;
  12.  
  13. printf("What is the actors name?\n");
  14. scanf("%s", actorname);
  15. printf("What is the film name?\n");
  16. scanf("%s", film_name);
  17. printf("What is the actors birthday?\n);
  18. scanf("%d", bday);
  19. printf("What is the films release year?\n");
  20. scanf("%d", &releaseyear);
  21.  
  22. 2008- bday = age
  23. 2008 - filmrelease = filmage
  24.  
  25.  
  26. printf("When %s, actorname filmed %s, film_name
  27. he was %sage, years old. %s, actorname is currently
  28. %age, and he is a Zodiac sIGN, %sfilmname was made %s, filmage)
  29.  
  30. }

You have some syntax errors.

  1. #include <stdio.h>
  2. int main(void)
  3. {
  4. char: actors_name[40];
  5. scanf("%s", actors_name);
  6. char: film_name[40];
  7. scanf("%s", film_name);
  8. int bday;
  9. int releaseyear;
  10. int age
  11. int filmage;
  12.  
  13. printf("What is the actors name?\n");
  14. scanf("%s", actorname);
  15. printf("What is the film name?\n");
  16. scanf("%s", film_name);
  17. printf("What is the actors birthday?\n);
  18. scanf("%d", bday);
  19. printf("What is the films release year?\n");
  20. scanf("%d", &releaseyear);
  21.  
  22. 2008- bday = age
  23. 2008 - filmrelease = filmage
  24.  
  25.  
  26. printf("When %s, actorname filmed %s, film_name
  27. he was %sage, years old. %s, actorname is currently
  28. %age, and he is a Zodiac sIGN, %sfilmname was made %s, filmage)
  29.  
  30. }

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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 10
Reputation: newbiecoderguy is an unknown quantity at this point 
Solved Threads: 0
newbiecoderguy's Avatar
newbiecoderguy newbiecoderguy is offline Offline
Newbie Poster

Re: New to C program code to calculate age

 
0
  #3
Jul 2nd, 2008
Besides the syntax errors, would this program execute what I need it to do?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,831
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: New to C program code to calculate age

 
0
  #4
Jul 2nd, 2008
Originally Posted by newbiecoderguy View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 10
Reputation: newbiecoderguy is an unknown quantity at this point 
Solved Threads: 0
newbiecoderguy's Avatar
newbiecoderguy newbiecoderguy is offline Offline
Newbie Poster

Re: New to C program code to calculate age

 
0
  #5
Jul 2nd, 2008
Yes im new to C, just got a book so I'm pretty lossed. but thanks for the struct link
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,500
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1479
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: New to C program code to calculate age

 
0
  #6
Jul 3rd, 2008
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
  1. days from year 0 to 1942 = 709,192 (ignoring leap years)
  2. Number of leap years = 1942/4 = 485
  3. Number of days from 1 Jan 1943 to 2 February 1943 = 31 (Jan) + 2 (Feb) = 33
  4. Now just add all those together yields 709,710
Now do the same thing for today's date (2 July 2008), then subtract 709,710 from that value.
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,661
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 123
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: New to C program code to calculate age

 
0
  #7
Jul 3rd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 10
Reputation: newbiecoderguy is an unknown quantity at this point 
Solved Threads: 0
newbiecoderguy's Avatar
newbiecoderguy newbiecoderguy is offline Offline
Newbie Poster

Re: New to C program code to calculate age

 
0
  #8
Jul 3rd, 2008
Originally Posted by jephthah View Post
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.
So whats a good way to go about solving this issue?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,831
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: New to C program code to calculate age

 
0
  #9
Jul 3rd, 2008
Originally Posted by newbiecoderguy View Post
So whats a good way to go about solving this issue?

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"
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,661
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 123
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: New to C program code to calculate age

 
0
  #10
Jul 3rd, 2008
^ 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.
Last edited by jephthah; Jul 3rd, 2008 at 1:19 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC