1.11M Members

New to C program code to calculate age

 
0
 

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

#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)
	
}
 
0
 

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

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

#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/clibrary/cstdio/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/clibrary/cstdio/printf.html

Also watch your variable names and quotes. These need to match (i.e. there's no variable called filmrelease).

 
0
 

Besides the syntax errors, would this program execute what I need it to do?

 
0
 

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/clibrary/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.

 
0
 

Yes im new to C, just got a book so I'm pretty lossed. but thanks for the struct link

 
0
 

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

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

Now do the same thing for today's date (2 July 2008), then subtract 709,710 from that value.

 
0
 

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.

 
0
 

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?

 
0
 

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.

 
0
 

^ wasnt tongue-in-cheek, although you might say it was tangential :P

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.

 
0
 

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.
.

Using imagenary year 0 makes the calculations a little simpler -- If all he wants to do is calculate someone's age it doesn't really matter that the Gregorian calendar wasn't invented until the 16th century.

 
0
 

Ok, I been playing with the code and I have it calculating and outputting correctly but still has a couple bugs. Whenever I type in an actor like "Al Pacino" with a seperation in the name the program gives me the next 2 questions, however, if I just put "AL" it will ask me the next question correctly, but it messed up when I put a space between actor or film names such as "American History X" but "historyx" would work. What is causing this? here is the updated code

#include <stdio.h>
int main(void)
{
       char actorname[50];
       char filmname[50];
       int bday;
       int releaseyear;
       int age;
       int filmage;
       int film;

       printf("What is the actors name?\n");
       scanf("%s", &actorname);
       printf("What is the film name?\n");
       scanf("%s", &filmname);
       printf("What is the actors birth year (eg. 1985)\n");
       scanf("%d", &bday);
       printf("What is the films release year\n");
       scanf("%d", &releaseyear);

       age= 2008 -bday;
       filmage=releaseyear-bday;//for actor
       film=2008-releaseyear;//filmage

       printf("When %s filmed %s he was %d years old.\n %s is currently %d, %s was made %d years ago\n",actorname, 
filmname, filmage,actorname,age,filmname,film);

return 0;
}
 
0
 

If all he wants to do is calculate someone's age it doesn't really matter that the Gregorian calendar wasn't invented until the 16th century

true, and i said as much in my last post.

but it doesnt change the fact that, for several reasons, the premise is flawed

:)

 
0
 

Ok, I been playing with the code and I have it calculating and outputting correctly but still has a couple bugs. Whenever I type in an actor like "Al Pacino" with a seperation in the name the program gives me the next 2 questions, however, if I just put "AL" it will ask me the next question correctly, but it messed up when I put a space between actor or film names such as "American History X" but "historyx" would work. What is causing this? here is the updated code

scanf("%s" ...) stops at the first space, so if you want spaces in the text then use fgets() instead

fgets(actorname, sizeof(actorname), stdin);
// now strip the '\n' (Enter key) 
if( actorname[strlen(actorname)-1] == '\n')
    actorname[strlen(actorname)-1] = 0;
 
0
 

scanf("%s" ...) stops at the first space, so if you want spaces in the text then use fgets() instead

fgets(actorname, sizeof(actorname), stdin);
// now strip the '\n' (Enter key) 
if( actorname[strlen(actorname)-1] == '\n')
    actorname[strlen(actorname)-1] = 0;

I tried using this code but it would not compile...

 
0
 

Post your code. We have use that same code thousands of times.

 
0
 
#include <stdio.h>
int main(void)
{
       char actorname[50];
       char filmname[50];
       int bday;
       int releaseyear;
       int age;
       int filmage;
       int film;

       fgets(actorname, sizeof(actorname), stdin);
	   // now strip the '\n' (Enter key) 
	   if( actorname[strlen(actorname)-1] == '\n')
	    actorname[strlen(actorname)-1] = 0;
       printf("What is the film name?\n");
       scanf("%s", &filmname);
       printf("What is the actors birth year (eg. 1985)\n");
       scanf("%d", &bday);
       printf("What is the films release year\n");
       scanf("%d", &releaseyear);

       age= 2008 -bday;
       filmage=releaseyear-bday;//for actor
       film=2008-releaseyear;//filmage

       printf("When %s filmed %s he/she was %d years old.\n %s is currently %d, %s was made %d years ago\n",actorname, 
filmname, filmage,actorname,age,filmname,film);

return 0;
}
 
0
 

you need to include string.h header file. And if you are using Microsoft VC++ 2008 Express (or other edition) you may also need to disable 4996 warning number.

 
0
 

Ok ill try that and I am compiling in the terminal on a mac so I have no IDE

 
0
 

Holy crap it worked, thanks for the help Ancient Dragon

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article