Professional Documents
Culture Documents
C Program To Store Details of An Employee in A Structure Stack Overflow PDF
C Program To Store Details of An Employee in A Structure Stack Overflow PDF
This is a program to store details of an employee in a structure. Although the program runs it shows many
errors, it does not give me a chance to enter address. Why is the program not running properly . Where am I
going wrong ?
#include <stdio.h>
#include <conio.h>
struct details
{
char name[30];
int age;
char address[500];
float salary;
};
int main()
{
struct details detail;
clrscr();
printf("\nEnter name:\n");
gets(detail.name);
printf("\nEnter age:\n");
scanf("%d",&detail.age);
printf("\nEnter Address:\n");
gets(detail.address);
printf("\nEnter Salary:\n");
scanf("%f",&detail.salary);
printf("\n\n\n");
printf("Name of the Employee : %s \n",detail.name);
printf("Age of the Employee : %d \n",detail.age);
printf("Address of the Employee : %s \n",detail.address);
printf("Salary of the Employee : %f \n",detail.salary);
getch();
}
1 of 3 12/24/2012 5:28 PM
c - program to store details of an employee in a structure - Stack Overflow http://stackoverflow.com/questions/7630890/program-to-store-details-o...
c homework
You are printing age and salary as strings, they should be %d and %f instead. Try change that and update
your answer. That is a problem, but not the one you are asking about. – K-ballo Oct 3 '11 at 3:56
feedback
2 Answers
Statement scanf("%d",&detail.age); will read 222 but not the newline you've entered. This newline
will remain in input buffer and pass it to next input gets().
You can use getchar() method to remove some chars from the input buffer to avoid such problems.
char ch;
....
printf("\nEnter age:\n");
scanf("%d",&detail.age);
printf("\nEnter Address:\n");
gets(detail.address);
Another problem is the incorrect use of format specifier with printf function.
1 That solves it, Thank you. This is the first time I have encountered this problem :) – Failed_Noob Oct 3 '11 at
4:15
feedback
Here's one of the problems. Your printf format codes don't match the datatypes you're passing it.
should be:
The other problem is probably in your scanf() . The first call leaves a newline in the stream which messes
up the later calls. Try this instead:
That solves the printf() problem but the program is still not allowing me to input address – Failed_Noob Oct 3 '11
at 4:03
feedback
2 of 3 12/24/2012 5:28 PM
c - program to store details of an employee in a structure - Stack Overflow http://stackoverflow.com/questions/7630890/program-to-store-details-o...
Not the answer you're looking for? Browse other questions tagged c homework or ask
your own question.
3 of 3 12/24/2012 5:28 PM