| | |
help with c program
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
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.
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)
0
•
•
•
•
Here we go,
you are printing the address of variable
is wrong.
remove '&' before retirement_years in both if and else parts.
change this condition to
to
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.
Note : its not compiled or tested just on my head..
•
•
•
•
my program is compiling and running but a long number is printing and not the age.
C Syntax (Toggle Plain Text)
printf("\n You have %.2d years to retirement\n", &retirement_years);
remove '&' before retirement_years in both if and else parts.
change this condition to
C Syntax (Toggle Plain Text)
while ((idnumber ==000) || (age== -1));
C Syntax (Toggle Plain Text)
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.
C Syntax (Toggle Plain Text)
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; }
Last edited by Iam3R; 31 Days Ago at 12:11 am. Reason: In tags
Similar Threads
- AutoExecuting a program Without Explorer (Windows NT / 2000 / XP)
- Program help required (Assembly)
- Help with a program (Java)
- Add compression to my program (C++)
- Cool little Program to disable startup programs (Windows NT / 2000 / XP)
- 3d Program (Game Development)
| Thread Tools | Search this Thread |
#include * ansi append array arrays asterisks bash binarysearch centimeter changingto char character convert copyimagefile cprogramme creafecopyofanytypeoffileinc database dynamic execv feet fgets file floatingpointvalidation fork framework function getlogicaldrivestrin givemetehcodez grade gtkwinlinux hacking histogram ide inches include incrementoperators infiniteloop initialization input interest intmain() iso kernel keyboard kilometer license linked linkedlist linux list lists locate looping lowest matrix meter microsoft number oddnumber opendocumentformat openwebfoundation overwrite owf pdf pointer posix power probleminc process program programming radix recursion recv recvblocked research reversing segmentationfault sequential single socket socketprograming socketprogramming standard strchr string suggestions systemcall test testing threads turboc unix urboc user variable wab whythiscodecausesegmentationfault windowsapi



