hopeless little c program

This is a discussion on hopeless little c program within the C Programming forums, part of the General Programming Boards category; Hello, I'm working on a project where I'll calculate a persons age using birthdate and current date supplied by user. ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    6

    Question hopeless little c program

    Hello,

    I'm working on a project where I'll calculate a persons age using birthdate and current date supplied by user.
    I can't get the age to print out as an integer.

    as an additional comment I had already added if and else if statements further make the calculations depending on the date entered but had taken them out to revert back to beginning to get the print out right before moving on.

    Code:
    /* this program will calculate a person's age based on the current year and person's birthdate then print results.*/
    
    
    #include "stdafx.h"
    //function declarations
    
    void getBirthDate(int* birthDay, int* birthMonth, int* birthYear);
    void getCurrentDate(int* currentDay, int* currentMonth, int* currentYear);
    int calculateAge(int currentYear,int birthYear);
    void printResults(int birthYear, int birthMonth, int birthDay, int currentYear, int currentMonth, int currentDay);
    
    
    int main(void)
    
    {
    	//local declarations
    	int birthYear;
    	int birthMonth;
    	int birthDay;
    	int currentDay;
    	int currentMonth;
    	int currentYear;
    	int age;
    	
    //statements
    
    	getBirthDate(&birthDay, &birthMonth, &birthYear);
    	getCurrentDate(&currentDay, &currentMonth, &currentYear);
    	calculateAge(birthYear,currentYear);
    	printResults (birthYear, birthMonth, birthDay, currentYear, currentMonth, currentDay);
    	age = calculateAge(birthYear, currentMonth);
    	
    	return 0;
    } //main
    
    /* ===================================getBirthDate=============================
    This function reads the birthYear input by the user at the keyboard
    Pre parameter save the dates as integer values in birthYear, birthMonth and birthDay 
    Post Data read into parameter address
    */
    
    void getBirthDate(int* birthDay, int* birthMonth, int* birthYear)
    
    {
    
    		//statements
    	
    	printf("Please Enter your birth date in  dd, mm, yyyy format: \n");
    	scanf_s("%d %d %d",birthDay,birthMonth,birthYear);
    		
    	return ;
    
    } //getbirthYear
    
    /* ===================================getCurrentDate=============================
    This function reads the Current input by the user at the keyboard
    Pre parameter save the dates as integer values in birthYear, birthMonth and birthDay 
    Post Data read into parameter address
    */
    
    void getCurrentDate(int* currentDay, int* currentMonth, int* currentYear)
    
    { 
    
    		//statements
    	
    	printf("Please Enter the current date in  dd, mm, yyyy format: \n");
    	scanf_s("%d%d%d",currentDay,currentMonth,currentYear);
    	
    	
    	return ;
    
    } //getCurrentDate
    
    /* ==================================CalculateAge=============================================
    this function will calculate an age based on the two dates given
    pre
    Post returns age
    */
    int calculateAge(int currentYear, int birthYear)
    {
    //local declarations
    
    int age;
    
    //Statements
    
    age = currentYear - birthYear;
    
    						printf("you are %d\n");
    	
     return age;
    
    } //calculateAge
    
    /* =======================================printResult=====================================================
    prints the current date, birthdate and person's age
    */
    void printResults(int birthYear, int birthMonth, int birthDay, int currentYear, int currentMonth, int currentDay)
    {
    	//local declarations
    
    
    	//statements
    
    	printf("Your birthday is %d,%d,%d\n", birthDay, birthMonth, birthYear);
    	printf("The current date is %d,%d,%d\n", currentDay,currentMonth, currentYear);
    	//printf("you are %d\n", age);
     
    	return ;
    } // printResult
    Last edited by beone2; 06-17-2009 at 06:25 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You have to put "age" in the print statement somewhere.

  3. #3
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Code:
    printf("you are %d\n");
    *cough*
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    6

    Unhappy hopeless little c program 'er

    I had done that previously but the print statement printed the address of age not the value.
    updated code follows

    Code:
    // COPchapter5Assignment.cpp : Defines the entry point for the console application.
    //
    /* this program will calculate a person's age based on the current year and person's birthdate then print results.*/
    
    
    #include "stdafx.h"
    //function declarations
    
    void getBirthDate(int* birthDay, int* birthMonth, int* birthYear);
    void getCurrentDate(int* currentDay, int* currentMonth, int* currentYear);
    int calculateAge(int currentYear,int birthYear, int currentMonth, int birthMonth, int currentDay, int birthDay);
    void printResults(int birthYear, int birthMonth, int birthDay, int currentYear, int currentMonth, int currentDay);
    
    
    int main(void)
    
    {
    	//local declarations
    	int birthYear;
    	int birthMonth;
    	int birthDay;
    	int currentDay;
    	int currentMonth;
    	int currentYear;
    	int age;
    	
    //statements
    
    	getBirthDate(&birthDay, &birthMonth, &birthYear);
    	getCurrentDate(&currentDay, &currentMonth, &currentYear);
    	calculateAge(currentYear,birthYear, currentMonth, birthMonth, currentDay, birthDay);
    	printResults (birthYear, birthMonth, birthDay, currentYear, currentMonth, currentDay);
    	
    	
    	return 0;
    } //main
    
    /* ===================================getBirthDate=============================
    This function reads the birthYear input by the user at the keyboard
    Pre parameter save the dates as integer values in birthYear, birthMonth and birthDay 
    Post Data read into parameter address
    */
    
    void getBirthDate(int* birthDay, int* birthMonth, int* birthYear)
    
    {
    
    		//statements
    	
    	printf("Please Enter your birth date in  dd, mm, yyyy format: \n");
    	scanf_s("%d %d %d",birthDay,birthMonth,birthYear);
    		
    	return ;
    
    } //getbirthYear
    
    /* ===================================getCurrentDate=============================
    This function reads the Current input by the user at the keyboard
    Pre parameter save the dates as integer values in birthYear, birthMonth and birthDay 
    Post Data read into parameter address
    */
    
    void getCurrentDate(int* currentDay, int* currentMonth, int* currentYear)
    
    { 
    
    		//statements
    	
    	printf("Please Enter the current date in  dd, mm, yyyy format: \n");
    	scanf_s("%d%d%d",currentDay,currentMonth,currentYear);
    	
    	
    	return ;
    
    } //getCurrentDate
    
    /* ==================================CalculateAge=============================================
    this function will calculate an age based on the two dates given
    pre
    Post returns age
    */
    int calculateAge(int currentYear, int birthYear, int currentMonth, int birthMonth, int currentDay, int birthDay)
    {
    //local declarations
    
    int age;
    
    //Statements
    
    age = currentYear - birthYear;
    	if (currentMonth < birthMonth)
    		age--;
    		else if (currentMonth > birthMonth)
    			age = age;
    		else if (currentMonth == birthMonth & currentDay < birthDay)
    			age--;
    	else
    		age=age;
    						printf("you are %d\n");
    	
     return age;
    
    } //calculateAge
    
    /* =======================================printResult=====================================================
    prints the current date, birthdate and person's age
    */
    void printResults(int birthYear, int birthMonth, int birthDay, int currentYear, int currentMonth, int currentDay)
    {
    	//local declarations
    
    
    	//statements
    
    	printf("Your birthday is %d,%d,%d\n", birthDay, birthMonth, birthYear);
    	printf("The current date is %d,%d,%d\n", currentDay,currentMonth, currentYear);
    	printf("you are %d\n", age);
     
    	return ;
    } // printResult

  5. #5
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    printResults doesn't have access to age, it's out of scope

    and why the hell are you saying age=age?
    that's just stupid
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by beone2 View Post
    I had done that previously but the print statement printed the address of age not the value.
    Then you need to improve your typing skills. Note that there is no '&' in "age".

  7. #7
    The Registered User Aparavoid's Avatar
    Join Date
    May 2009
    Posts
    74
    scanf_s isn't portable. Something more of Microsoft's crap. What tutorial are you reading?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21