All pastes #538339 Raw Edit

Stuff

public text v1 · immutable
·published 2007-06-05 00:26 UTC
rendered paste body
/*
 * Title: question4.c
 *  Final
 *  BY: John Stile
 *  Purpose: Write a program that computes the age of the user.
 *   An approximate answer, based on the year of the person's birth, is required.
 * The program should:
 * a) Ask user to input the year of his/her birth.
 * b) Compare the input year with the current year.
 *    If the input year is later than the current year, responly with ONLY an error message.
 *    If the input is valid, print out the user's age to the screen.
 * c) Use a preprocessor command to define the current year such that the program can easily modify from year to year.
 */

#include <stdio.h>      // include funciton headers from stdo library
#include <time.h>       //  this gets us time() and localtime() function and types.
#define  THIS_YEAR 2007  // preprocessor macro to set current year.

/////////////////////////////////////////////////////////
// prototypes

void prompt_input ( int * ); // takes the address of an int, and stores the users birth year there.
void get_year( int * );      // takes the address of an int, and stores current year there.

/////////////////////////////////////////////////////////
int main (void) // header for main. takes nothing, returns an int to calling shell
{ // begin main block

   int birth_year; // declare int birth_year
   int this_year;  // declare int this_year

   prompt_input (&birth_year); // calling prompt_input, takes address of int, returns nothing
   printf("You entered: %d\n", birth_year); // print what was recorded

   get_year(&this_year); // calling get_year, takes address of this_year, returns nothing
   printf("Current year: %d.\n", this_year);  // print current year

   if( birth_year > this_year ) // evaluate if birth_year is greater than this_year
                                // if true, do block
   {
     printf("Impossible!\nBorn:%d before Today:%d\n", birth_year, this_year); // print output
     return 1; // exit program, return 1 to calling funciton
   }else{
     printf("You are %d year(s) old\n", (this_year-birth_year) ); // print output, age calculated
   }

   return 0;
}
/////////////////////////////////////////////////////////
// functions

void prompt_input ( int * input )
{
  printf("Input your year of birth and press Enter.(i.e.1969): "); // prompt for input
  scanf( "%d", input ); // read from stdin, store in address pointed to by input pointer.
  getchar(); // clear input buffer
  return;
}
/////////////////////////////////////////////////////////
void get_year(int * year)
{
  *year=THIS_YEAR;
 /*
  // Better Method than Macro:
  // Have the computer calculate current year
  // so the program doesn't have to be recompiled
  time_t tim=time(NULL);     // declare variable type time_t named time.
                             // initialize with value returned by time()
                             // time() returns the time since the Epoch (00:00:00 UTC, January 1, 1970) in seconds
  struct tm *now=localtime(&tim); // declare variable type struct tm pointer named now
                                  // initialize with value returned by localtime ( &t_time )
                                  // localtime() takes address of varialbe of type t_time,
                                  // localtime() returns type struct tm pointer.
  *year=(now->tm_year + 1900);    // tm->tm_year shows the year .
 */
  return;
}
/////////////////////////////////////////////////////////
/*
//Output:

  ./question4
     Input your year of birth and press Enter.(i.e.1969): 2008

     You entered: 2008
     Current year: 2007.
     Impossible to be born later than today!
     Born:2008 before Today:2007

  ./question4
     Input your year of birth and press Enter.(i.e.1969): 1969

     You entered: 1969
     Current year: 2007.
     You are 38 year(s) old
*/