Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
246 views
in Technique[技术] by (71.8m points)

c - Scanf for a two strings and a float number

I am creating a menu that needs to take in an three inputs from the users.

    char *fullname;
    char *date;
    float sal;
    printf("
Enter full name: ");

line92

scanf("%s", &fullname);
printf("
Enter hire date: ");

Line 94

scanf("%s", &date);
printf("
Enter salary: ");

Line 96

scanf("%d", &sal);

These are the errors I am recieving

Employee.c:92: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char **’
Employee.c:94: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char **’
Employee.c:96: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘float *’

Can I get an explanation of what is causing these issues?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are several problems:

First:

When you use scanf for strings you do not use the &. So just scanf("%s", fullname);.

Second:

Your pointers aren't initialized. Try this instead:

char fullname[256];
char date[256];

This will work as long as you input at most 255 characters.

Third:

Your typing for the last scanf doesn't match. You're passing in a float when you've specified an int in the format string. Try this:

scanf("%f", &sal);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...