Reputation: 3692
I'm new to C, and am studying structures. I created a simple structure and tried to enter input values through scanf()
in the structure variables, but I'm only getting a partial/garbage output. Is there something I'm missing out here?
Here's my original code-
#include<stdio.h>
void main()
{
struct book
{
char name;
float price;
int pages;
};
struct book b1,b2,b3;
int i;
printf("\n Enter the names, prices and no. of pages of the 3 books: \n");
scanf("%c %f %d", &b1.name,&b1.price, &b1.pages);
scanf("%c %f %d", &b2.name,&b2.price, &b2.pages);
scanf("%c %f %d", &b3.name,&b3.price, &b3.pages);
printf("\n You have entered: \n");
printf("%c %.2f %d", b1.name,b1.price, b1.pages);
printf("%c %.2f %d", b2.name,b2.price, b2.pages);
printf("%c %.2f %d", b3.name,b3.price, b3.pages);
}
And this is the set of output-
Enter names, prices & no. of pages of 3 books
A 230.5 13
B 345.9 33
And this is what you entered
A 230.50 13
0.00 3
B 345.89 33
As you can see:
structure variable(b2)
, the printf()
stops working and doesn't accept info. about the third structure variable(b3)
scanf()
, I get some junk value in the second line- 0.00 3
I thought maybe the structure definition should be outside main()
always, so I even modified it to define the struct book
outside main()
and declare the variables struct book b1,b2,b3;
inside main(), but it didn't have any effect.
What did I do wrong here?
UPDATE-
I know there exists a similar answer on SO here, but the problem here was that the addressof(&)
operator wasn't included with scanf()
Upvotes: 0
Views: 2004
Reputation:
#include<stdio.h>
#include<string.h>
void main()
{
struct book
{
char name[99];
float price;
int pages;
};
struct book b1,b2,b3;
int i,ch;
printf("\n Enter the names \n");
gets(b1.name);
printf("\n enter the price\n");
scanf("%f",&b1.price);
printf("\n enter the pages\n");
scanf("%d",&b1.pages);
while ((ch = fgetc(stdin)) != EOF && ch != '\n')
{
}
printf("\n Enter the names \n");
gets(b2.name);
printf("\n enter the price\n");
scanf("%f",&b2.price);
printf("\n enter the pages\n");
scanf("%d",&b2.pages);
while ((ch = fgetc(stdin)) != EOF && ch != '\n')
{
}
printf("\n Enter the names \n");
gets(b3.name);
printf("\n enter the price\n");
scanf("%f",&b3.price);
printf("\n enter the pages\n");
scanf("%d",&b3.pages);
printf("\n You have entered: \n");
printf("%s %.2f %d\n", b1.name,b1.price, b1.pages);
printf("%s %.2f %d\n", b2.name,b2.price, b2.pages);
printf("%s %.2f %d\n", b3.name,b3.price, b3.pages);
}
Upvotes: 0
Reputation: 753645
Most book titles are longer than a single character, but the %c
format limits the title to one character. You structure also only stores a single character, so you'd need to change the definition to allow for a longer title. More problematic, most book titles are longer than one word, but the %s
format, which is the 'obvious' alternative to %c
, only reads up to the first white space, which means it only reads the first word.
You should also check the return value from scanf()
each time you use it. If it is not 3, something went wrong. The reason you have a problem is that the first line of input ends with a newline, which is not read by the %d
format, so it is read by the %c
, and then the numeric conversion fails on the letter. You can fix that by using " %c %.2f %d"
with a space before the %c
to skip optional leading white space (which would skip the newline; it counts as white space).
There isn't a simple fix to this. You need to know how to distinguish the end of the title from the price and the pages. I'd probably use fgets()
to read a whole line of input. Then I'd scan backwards to find the number of pages and price; the residue of the line is presumably the book title.
Upvotes: 4
Reputation: 122383
After you entered the first line of values, and press ENTER, the new line character is still in the input buffer and will be processed by "%c"
in the next scanf
.
To fix the problem, don't use scanf
. Instead, use fgets
to get each line and then use sscanf
to get the values from each line.
Upvotes: 5