944,117 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 551
  • C RSS
Nov 2nd, 2009
-1

help with c program

Expand Post »
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.
  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)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
maubybark is offline Offline
1 posts
since Oct 2009
Nov 3rd, 2009
0
Re: help with c program
Here we go,
Quote ...
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; Nov 3rd, 2009 at 12:11 am. Reason: In tags
Reputation Points: 34
Solved Threads: 4
Junior Poster
Iam3R is offline Offline
110 posts
since Oct 2009
Jan 2nd, 2011
0
Re: help with c program
Hi,

change this while condition to

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

to

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

Your program will work
Reputation Points: 11
Solved Threads: 0
Newbie Poster
uttamclasses is offline Offline
3 posts
since Jan 2011

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Beginners Program: Leap Year
Next Thread in C Forum Timeline: Help me please





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC