简体   繁体   中英

Why does my code does not work even though it is correct?

This is the part of my code. It has 0 errors and 0 warnings but it does not work. It is completely correct. But it does not work.

#include<stdio.h>

struct details{
    char empName;
    int age;
    float salary;
}det1;

void main(){

    printf("Please enter a name : ");
    scanf("%s",&det1.empName);
    printf("Please enter the age : ");
    scanf("%d",&det1.age);
    printf("Please enter the salary : ");
    scanf("%f",&det1.salary);

    FILE *p;
    p = fopen("employee.txt","w");
    fprintf(p,"%s %d %0.2f",det1.empName,det1.age,det1.salary);
    fclose(p);

}

regarding the OPs posted code:

Running it through a compiler with most useful warnings enabled results in:

gcc    -ggdb -Wall -Wextra -Wconversion -pedantic -std=gnu11  -c "untitled2.c"  
untitled2.c:9:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
void main(){
^~~~
untitled2.c: In function ‘main’:
untitled2.c:20:17: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat=]
 fprintf(p,"%s %d %0.2f",det1.empName,det1.age,det1.salary);
            ~^           ~~~~~~~~~~~~
            %d

so it does not cleanly compile.

Then this statement:

scanf("%s",&det1.empName);

is trying to insert a unlimited length of characters into a single character.

suggest modifying:

struct details{
    char empName;
    int age;
    float salary;
}det1;

into:

struct details           <-- struct definition
{
    char empName[30];    <-- room for 29 characters + NUL terminating character
    int age;
    float salary;
}

struct details det1;     <-- struct instance

then, this statement:

scanf("%s",&det1.empName);

needs to be changed to:

scanf("%29s",det1.empName);

Note that '%s' will stop at the first 'white space' so the employee name must be a single word

Note that the '%s' always appends a NUL byte to the input so the MAX CHARACTERS modifier has to be 1 less than the length of the input buffer.

You might try:

scanf( "%29[^\n], det1.empName );  

as that will read input until a '\\n' is encountered or 29 characters are read.

Of course, for all calls to scanf() the code should be checking the returned value (not the parameter values) to assure the operation was successful. IE

if( scanf("%29s",det1.empName) != 1 )
{
    // tell user about problem
    fprintf( stderr, "scanf to read employee name failed\n" );
    // cannot continue so exit program
    // note: 'exit()' and EXIT_FAILURE
    // are exposed via the statement:
    // #include <stdlib.h>
    exit( EXIT_FAILURE );
}

// implied else, scanf for employee name successful

because the scanf() family of functions returns the number of successful 'input format conversion specifiers' (which for all three calls to scanf() in the posted code is expecting a returned value of 1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM