ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
hi,
this is the code for a structure:
#include <stdio.h>
main()
{
char name[3];
float price[3];
int pages[3], i;
printf("\nEnter names,prices and no. of pages of 3 books\n");
for(i=0;i<=2;i++)
scanf("%c%f%d",&name[i],&price[i],&pages[i]);
printf("\nAnd this is what you entered\n");
for(i=0;i<=2;i++)
printf("%c%f%d\n",name[i],price[i],pages[i]);
}
but the output of this program after entering the name price and pages of
3 books is garbage.plz point me where is the mistake.
You should create a 2-d char array as sibtay wrote and use scanf like these
Code:
scanf("%s %f %d", name[i], &price[i], &pages[i]);
Notice the %s placeholder which is for char array and lack of & in front of the name[i] because when you create a 2-d char array name[i] will be an address already. Also you should use %s when printing name[i] in the second loop.
Originally posted by sibtay i have done this with c++'s cin function
I was not able to do it scanf...the problem is that in the second iteration of the first for loop, scanf("%c",&name[i]) is not working properly
Because you are using a character placeholder %c instead of a character array placeholder %s. And declaration of name[i] should also be changed. Look at the posts before. These will fix i think.
Originally posted by itsme86 scanf() should never ever be used to input a string for the same reason gets() should never ever be used. Use fgets() to input a string.
gets() is very dangerous; i agree with that, but when we use scanf() we have a chance to specify character count to be read:
You're right. I'm sorry. What I meant to say was scanf() should never use the %s modifier to input a string unless you specifiy the width like you did with the %19s. Without the width scanf("%s", str); is just as bad as gets(str);
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.