It's a simple C program. I've declared a structure named EmployeeDetails Now I'm trying to take four different inputs for every Employee and at last print them according to the input provided. But the program is not working as expected. Like sometimes the scanf() works sometime it gets skipped which results in some garbage value at the time of printing in the end. Check the output at the end for more clarity.
#include <stdio.h>
#define MAX_SIZE 40
int main() {
struct EmployeeDetails
{
char name[MAX_SIZE];
int dlnumber;
char route[MAX_SIZE];
float kmsdrove;
} e1, e2 ;
printf("1st Employee. Enter Your Name :\n");
scanf("%[^\n]%*c", e1.name);
printf("Kindly Provide us Your Driving License(DL) No. :\n");
scanf("%d", &e1.dlnumber);
printf("Route on Which You're Going to Drive :\n");
scanf("%[^\n]%*c", e1.route);
printf("How many Kms You already drove? :\n");
scanf("%f", &e1.kmsdrove);
printf("2nd Employee. Enter Your Name :\n");
scanf("%[^\n]%*c", e2.name);
printf("Kindly Provide us Your Driving License(DL) No. :\n");
scanf("%d", &e2.dlnumber);
printf("Route on Which You're Going to Drive :\n");
scanf("%[^\n]%*c", e2.route);
printf("How many Kms You already drove? :\n");
scanf("%f", &e2.kmsdrove);
printf("1st Employee Details: \n");
printf("Name: %s\n", e1.name);
printf("DL No.: %d\n", e1.dlnumber);
printf("Route: %s\n", e1.route);
printf("Kms already covered: %0.02f\n", e1.kmsdrove);
printf("2nd Employee Details: \n");
printf("Name: %s\n", e2.name);
printf("DL No.: %d\n", e2.dlnumber);
printf("Route: %s\n", e2.route);
printf("Kms already covered: %0.02f\n", e2.kmsdrove);
return 0;
}
Output of the program:
1st Employee. Enter Your Name :
Ravi
Kindly Provide us Your Driving License(DL) No. :
454
Route on Which You're Going to Drive :
How many Kms You already drove? :
4
2nd Employee. Enter Your Name :
Kindly Provide us Your Driving License(DL) No. :
32
Route on Which You're Going to Drive :
How many Kms You already drove? :
54
1st Employee Details:
Name: Ravi
DL No.: 454
Route:
Kms already covered: 4.00
2nd Employee Details:
Name:
DL No.: 32
Route: �U
Kms already covered: 54.00
Kindly help.
the following proposed code snippet:
scanf()
%[...]
input format conversion specifierscanf()
And now, the proposed code snippet:
#include <stdio.h> // scanf(), fprintf(), printf()
#include <stdlib.h> // exit(), EXIT_FAILURE
#define MAX_SIZE 40
// note the proper signature for 'main'
int main( void )
{
// for flexibility,
// separate struct definition
// from
// instances of the struct
struct EmployeeDetails
{
char name[ MAX_SIZE ];
int dlnumber;
char route[ MAX_SIZE ];
float kmsdrove;
};
// following instances would be better written as:
// struct EmployeeDetails employees[2]
// then a loop could be used to enter the employees
// rather than the extended list of repeated code.
struct EmployeeDetails e1;
struct EmployeeDetails e2;
// note the leading space in the format string
// that leading space consumes all leading 'white space'
printf( "1st Employee. Enter Your Name :\n" );
if( scanf( " %39[^\n]", e1.name) != 1 )
{
fprintf( stderr, "scanf for first emp name failed\n" );
exit( EXIT_FAILURE );
}
printf( "Kindly Provide us Your Driving License(DL) No. :\n" );
if( scanf( "%d", &e1.dlnumber ) != 1 )
{
fprintf( stderr, "scanf for first emp drive license failed\n" );
exit( EXIT_FAILURE );
}
// note the MAX CHARACTERS modifier on the '%[...]'
// that is 1 less than the length of the input buffer
// because `%s` and `%[...]` always append a NUL to the input
// This avoids any possibility of a buffer overflow and the
// resulting undefined behavior
printf( "Route on Which You're Going to Drive :\n" );
if( scanf( " %39[^\n]", e1.route ) != 1 )
{
fprintf( stderr, "scanf for emp 1 route failed\n" );
exit( EXIT_FAILURE );
}