You are on page 1of 3

c - program to store details of an employee in a structure - Stack Overflow http://stackoverflow.com/questions/7630890/program-to-store-details-o...

program to store details of an employee in a structure

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();
}

This is the output I get:

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

edited Oct 3 '11 at 4:04 asked Oct 3 '11 at 3:53


Failed_Noob
304 1 10 33
93% accept rate

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

@K-ballo I made the required changes – Failed_Noob Oct 3 '11 at 4:04

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);

while((ch = getchar()) != '\n' && ch != EOF) { }

printf("\nEnter Address:\n");
gets(detail.address);

Another problem is the incorrect use of format specifier with printf function.

answered Oct 3 '11 at 4:08


AVD
46.5k 7 26 58

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.

printf("Age of the Employee : %s \n",detail.age);


printf("Salary of the Employee : %s \n",detail.salary);

should be:

printf("Age of the Employee : %d \n",detail.age);


printf("Salary of the Employee : %f \n",detail.salary);

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:

scanf("%d ", &detail.age);

edited Oct 3 '11 at 4:01 answered Oct 3 '11 at 3:56


Mysticial
76.1k 19 113 147

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

You might also like