/* It is not entering data into the third scanf() statement .*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main(void)
{
struct book
{
char name;
int pages;
float price;
};
struct book a1,a2,a3,a4;
printf("Enter data into 3 books\n");
scanf("%c %d %f",&a1.name,&a1.pages,&a1.price);
scanf("%c %d %f",&a2.name,&a2.pages,&a2.price);
scanf("%c %d %f",&a3.name,&a3.pages,&a3.price);
printf(" you entered:\n");
printf("\n%c %d %f",a1.name,a1.pages,a1.price);
printf("\n%c %d %f",a2.name,a2.pages,a2.price);
printf("\n%c %d %f",a3.name,a3.pages,a3.price);
getch();
}
views:
55answers:
2
+4
A:
You want to use strings, not single characters:
int main(void)
{
struct book
{
char name[100];
int pages;
float price;
};
struct book a1,a2,a3,a4;
printf("Enter data into 3 books\n");
scanf("%s %d %f",&a1.name,&a1.pages,&a1.price);
scanf("%s %d %f",&a2.name,&a2.pages,&a2.price);
scanf("%s %d %f",&a3.name,&a3.pages,&a3.price);
printf(" you entered:\n");
printf("%s %d %f\n",a1.name,a1.pages,a1.price);
printf("%s %d %f\n",a2.name,a2.pages,a2.price);
printf("%s %d %f\n",a3.name,a3.pages,a3.price);
return 0;
}
But note this is prone to buffer overflows, and won't deal correctly with book names that contain spaces.
anon
2010-07-06 13:37:41
+1
A:
you are wanting a string as a name, while you are giving a %c specifier for the input which expects a character.
so either use %s for a string input.
or better use some string function like gets()
gets (a1.name);
scanf ( %d %f",&a1.pages,&a1.price);
And again to remind that you must be careful with size of string(char array) to avoid stack overflows.
Thanks
Alok.Kr.
Kumar Alok
2010-07-06 14:03:51