HI
the prgram below request a user to enter 3 character id number and interger age. a function must be used
that tells the user how many years to retirement. i must pass the age and print
a message in the function. retirement age is 65 for persons over 45 and 70 for all
other persons. the id number is a 3 element character array. the program
should continue to accept user input until the id number entered is 000 or age is
-1.

please help. i am fairly new to c and i am unsure of how to do the function part.

my program is compiling and running but a long number is printing and not the age.

#include<stdio.h>
#include<conio.h>

int main ()
{

	int idnumber, retirement_years, age=0;

	//int current_age (int age1);
		do
		{

			printf ("\nPlease enter 3 digit id number\n");
			scanf("%d", &idnumber);
			printf("\n Please enter your age\n");
			scanf("%d", &age);

			if (age >45) 
			{
				retirement_years = 65-age;
				printf("\n You have %.2d years to retirement\n", &retirement_years);
			}
			else 
			{
				retirement_years = 70-age;
				printf("\n You have %.2d years to retirement\n", &retirement_years);
			}
		}
			while ((idnumber ==000) || (age== -1));
		

		getch ();
			return 0;
}

		




		//int current_age (int age1)

Here we go,

my program is compiling and running but a long number is printing and not the age.

you are printing the address of variable

printf("\n You have %.2d years to retirement\n", &retirement_years);

is wrong.
remove '&' before retirement_years in both if and else parts.

change this condition to

while ((idnumber ==000) || (age== -1));

to

while ( (idnumber !=0)  ||  (age != -1) );

the program will terminate when idnumber is equal to 0 or age is -1.

1. no need of using getch().
2. remove conio.h
if you require functions you can use the below.

int get_retirement_age(int age, int idnumber);

int main()
{
  int age, idnumber, valid;
    do {
        printf("enter age and idnumber\n");
        scanf("%d\n",&age);
        printf("Enter idnumber\n");
        scanf("%d\n",&idnumber);
       valid = get_retirement_age(age,idnumber) ;   
      if(valid == 0 )
          printf( "Invalid details entered: terminating program\n");
           
   } while(valid);
return 0;
}

int get_retirement_age(int age, int idnumber)
{   
      int retirement_years;

      if(age == -1 || idnumber == 0)
             return 0;

      if (age >45) {	
             retirement_years = 65-age;			
   printf(" you have %d years to retirement\n",  retirement_years); 
}
     else {	
         retirement_years = 70-age;			
        printf("You have %d years to retirement\n",retirement_years);
}
return 1;
}

Note : its not compiled or tested just on my head..

Edited 7 Years Ago by Iam3R: In tags

Hi,

change this while condition to

while ((idnumber ==000) || (age== -1));

to

while ( (idnumber !=0) || (age != -1) );

Your program will work

This article has been dead for over six months. Start a new discussion instead.