help with c program

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
maubybark maubybark is offline Offline 31 Days Ago, 8:07 pm |
-1
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.
Quick reply to this message  
C Syntax
  1. #include<stdio.h>
  2. #include<conio.h>
  3.  
  4. int main ()
  5. {
  6.  
  7. int idnumber, retirement_years, age=0;
  8.  
  9. //int current_age (int age1);
  10. do
  11. {
  12.  
  13. printf ("\nPlease enter 3 digit id number\n");
  14. scanf("%d", &idnumber);
  15. printf("\n Please enter your age\n");
  16. scanf("%d", &age);
  17.  
  18. if (age >45)
  19. {
  20. retirement_years = 65-age;
  21. printf("\n You have %.2d years to retirement\n", &retirement_years);
  22. }
  23. else
  24. {
  25. retirement_years = 70-age;
  26. printf("\n You have %.2d years to retirement\n", &retirement_years);
  27. }
  28. }
  29. while ((idnumber ==000) || (age== -1));
  30.  
  31.  
  32. getch ();
  33. return 0;
  34. }
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41. //int current_age (int age1)
0
Iam3R Iam3R is offline Offline | 31 Days Ago
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
  1. 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

  1. while ((idnumber ==000) || (age== -1));
to
  1. 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.



  1. int get_retirement_age(int age, int idnumber);
  2.  
  3. int main()
  4. {
  5. int age, idnumber, valid;
  6. do {
  7. printf("enter age and idnumber\n");
  8. scanf("%d\n",&age);
  9. printf("Enter idnumber\n");
  10. scanf("%d\n",&idnumber);
  11. valid = get_retirement_age(age,idnumber) ;
  12. if(valid == 0 )
  13. printf( "Invalid details entered: terminating program\n");
  14.  
  15. } while(valid);
  16. return 0;
  17. }
  18.  
  19. int get_retirement_age(int age, int idnumber)
  20. {
  21. int retirement_years;
  22.  
  23. if(age == -1 || idnumber == 0)
  24. return 0;
  25.  
  26. if (age >45) {
  27. retirement_years = 65-age;
  28. printf(" you have %d years to retirement\n", retirement_years);
  29. }
  30. else {
  31. retirement_years = 70-age;
  32. printf("You have %d years to retirement\n",retirement_years);
  33. }
  34. return 1;
  35. }
Note : its not compiled or tested just on my head..
Last edited by Iam3R; 31 Days Ago at 12:11 am. Reason: In tags
 
 

Message:


Similar Threads
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC