Help on structures

I am trying to run a basic program of structures
code
Code:
#include<stdio.h>
int main()
{
	struct book
	{
		char name;
		float price;
		int pages;
	};
	struct book b1,b2,b3;
	printf("enter the value\n");
	scanf("%c%f%d", &b1.name, &b1.price, &b1.pages);
	fflush(stdin);
	scanf("%c%f%d", &b2.name, &b2.price, &b2.pages);
	fflush(stdin);
	scanf("%c%f%d", &b3.name, &b3.price, &b3.pages);
	fflush(stdin);
	printf("entered values are\n");
	printf("%c%f%d", b1.name, b1.price, b1.pages);
	printf("%c%f%d", b2.name, b2.price, b2.pages);
	printf("%c%f%d", b3.name, b3.price, b3.pages);
	return 0;
}
It is taking iput for only 2 elements(it should for 3) and displaying correctly only first element. Garbage value is displayed for second element

screenshot

ImageShack® - Online Photo and Video Hosting
 
Not in a great mood to look into it deep. but..

can u try it with this order.

char int float

or

int char float.

I forgot the size for padding. I think the first one is correct.
 
^^ tried same problem

Its really starting to pull my hair. Its such a basic program and I have copied it exactly from book. I am using gnu and it seems to be running all programs fine. googling for last half hour and no result :((

--- Updated Post - Automerged ---

Ok so finally program is running fine if I use \n in each scanf statement. But can someone explain why?
 
Dude, have you tried using \n. It clears the buffer.

--- Updated Post - Automerged ---

EDIT: Damn. You got it. This is a known issue (feature?). \n flushes the buffer.
 
The root cause of the error is that scanf is accepting the newline character '\n' as input for conversion specifier %c.Try this input for your code it will work.

a 1.2 3b 1.2 3c 1.2 3

Since there are no spaces or newlines to accept it works as intended.

Rememver whitespaces and newlines are ignored when parsing all types except char.:)
 
Alternatively, add a space before %c in scanf format specifier, this will make %c ignore whitespace. eg. "scanf(" %c%f%d", &b1.name, &b1.price, &b1.pages);"

Format specifiers such as %d, %s ignore leading whitespace, whereas %c doesn't. The %c matches your newline character entered in the previous line.
 
nuke'em said:
The root cause of the error is that scanf is accepting the newline character '\n' as input for conversion specifier %c.Try this input for your code it will work.

Since there are no spaces or newlines to accept it works as intended.
Rememver whitespaces and newlines are ignored when parsing all types except char.:)
Thanks a ton man! I got your point

--- Updated Post - Automerged ---

perfektionist said:
Alternatively, add a space before %c in scanf format specifier, this will make %c ignore whitespace. eg. "scanf(" %c%f%d", &b1.name, &b1.price, &b1.pages);"

Format specifiers such as %d, %s ignore leading whitespace, whereas %c doesn't. The %c matches your newline character entered in the previous line.
Thanks man It worked.

--- Updated Post - Automerged ---

Just one last question- I think It should also have been solved by fflush(stdin) as it is used to clear input buffer. So, it should clear the input of new line(enter)?
 
satan194p said:
Just one last question- I think It should also have been solved by fflush(stdin) as it is used to clear input buffer. So, it should clear the input of new line(enter)?
If fflush(stdin) worked it would be a good solution but according to the C standard using fflush on stdin leads to undefined behavior and is therefore not recommended.fflush is designed to used on stdout.It may work on some older platforms but not gcc.The text book you're using is probably old.Getting input in right format without bugs can be tricky especially in C.:)

In case you want to flush a line from input do it manually using a while loop.

while ((ch = getchar()) != '\n' && ch != EOF);

You can use a loop to get rid of leading whitespaces too.
 
satan194p said:
Thanks a ton man! I got your point

--- Updated Post - Automerged ---


Thanks man It worked.

--- Updated Post - Automerged ---

Just one last question- I think It should also have been solved by fflush(stdin) as it is used to clear input buffer. So, it should clear the input of new line(enter)?
ok, got it. Thanks!
 
Back
Top