c programing

i have compiled the following code

#include<stdio.h>
struct book{
	char name;
	float price;
	int page;
}b1,b2,b3;
int main(){
	printf("entr the data\n");
	scanf("%c %f %d",&b1.name,&b1.price,&b1.page);
	scanf("%c %f %d",&b2.name,&b2.price,&b2.page);
	scanf("%c %f %d",&b3.name,&b3.price,&b3.page);
	printf("%c %f %d\n",b1.name,b1.price,b1.page);
	printf("%c %f %d\n",b2.name,b2.price,b2.page);
	printf("%c %f %d\n",b3.name,b3.price,b3.page);
	return 0;
}

it takes only first two scanf and print all three printf statement but second one is giving garbage value.what is the problem ?thanks in advance.

1 Like

modify your code a little bit :smiley:

scanf("%c %f %d\n",&b1.name,&b1.price,&b1.page);
scanf("%c %f %d\n",&b2.name,&b2.price,&b2.page);
scanf("%c %f %d",&b3.name,&b3.price,&b3.page);
9 Likes

Maybe try using a getchar() at the end of each scanf. It takes the new-line as a character for b2.

1 Like

When you press ENTER in c it takes it as a character and for %c you store enter. So garbage values are coming. What u need to do is flush the standard input.
Just type fflush(stdin); before each scanf() statement.

From what I understand, the modification provided by ravi works because the \n in scanf eats the white space you provide in between the two inputs which is the new line(\n) character in this case. Since you hit ENTER after the first series of inputs and the waiting place holder is %c - it reads \n from buffer and moves to the next input(correct me if I am wrong).

You can also put a space to eat white space instead of \n like

scanf("%c %f %d ",&b1.name,&b1.price,&b1.page);

scanf("%c %f %d ",&b2.name,&b2.price,&b2.page);

scanf("%c %f %d",&b3.name,&b3.price,&b3.page);

Another alternative is to clear the buffer using fflush like -

scanf("%c %f %d",&b1.name,&b1.price,&b1.page);
fflush(stdin);

scanf("%c %f %d",&b2.name,&b2.price,&b2.page);
fflush(stdin);

scanf("%c %f %d",&b3.name,&b3.price,&b3.page);

alt text

Change your scanf as below. See change in space. Live example here.

scanf(" %c%f%d",&b1.name,&b1.price,&b1.page);  /* Extra space prepended */
scanf(" %c%f%d",&b2.name,&b2.price,&b2.page);
scanf(" %c%f%d",&b3.name,&b3.price,&b3.page);

check how scanf respond for space character. It says:

Whitespace character: the function
will read and ignore any whitespace
characters encountered before the next
non-whitespace character (whitespace
characters include spaces, newline and
tab characters – see isspace). A
single whitespace in the format string
validates any quantity of whitespace
characters extracted from the stream
(including none).

Which means always use space before %c in scanf if you want to ensure that the character read is not a white space.

2 Likes

first define the size of string as u will enter book name which is a string not a character…

instead of %c use %s in both scanf & printf statement…

have a look on this code as i have made some minor changes as mentioned above…

#include "stdio.h"

struct book
{

char name[20];

float price;

int page;

}
b1,b2,b3;

int main()

{
    printf("enter the data\n");
    
    scanf("%s %f %d",&b1.name,&b1.price,&b1.page);
    scanf("%s %f %d",&b2.name,&b2.price,&b2.page);
    scanf("%s %f %d",&b3.name,&b3.price,&b3.page);
    printf("%s %f %d\n",b1.name,b1.price,b1.page);
    printf("%s %f %d\n",b2.name,b2.price,b2.page);
    printf("%s %f %d\n",b3.name,b3.price,b3.page);
    return 0;
}

how can i use fflush() in above code.

@nitya I dont think so, I tried using fflush(stdin) to clear the input stream, but that didn’t seem to work. I am not so sure if that can be done. Just in case you get to know how to use fflush() to solve this then please do let me know as well. Thanks

it is not working ,it gives garbage value for 2nd printf…i am using code block ide…

could u plz take the pain to explain why it started working after “\n”??

giving space is working but fflush(stdin) is not working.i am using code blocks.

I forgot to remove the space after using fflush. It works now. Check :slight_smile:

it’s not working even after removing space

fflush will not work in this case

It works in Code::blocks 12.11. I ll add an image.

@grvana it just eats up the enter key that you press after your first input, so that the value of your character of the second scanf doesn’t take the value \n, but the desired character.

1 Like

@thezodiac I tried this and it works in my code blocks now… But initially when this question was posted I was using Dev-C++ and it didnt work in it. Does this mean that this is not a kind of “universally accepted” solution, and might not work everywhere. Just like it doesnt work in nitya’s code blocks???

@coderzz027 Never fflush(stdin) as the behaviour of flushing stdin in C is an undefined behaviour.

Never fflush(stdin) as the behaviour of flushing stdin in C is an undefined behaviour.