H! plz solve a problem..............!
See I want to take continuous four input from the user, out of which three are character type and one is of int type. So my problem actually is that, it accepts only three input i.e. 2 char and one int & for remaining char it will keep blank.
I know why it is happening. Because if u have entered any char input & on next again char i/p is there then, it will take new line(\n) i/p which is present in the stack(by stream concept) by default without waiting to respond for i/p to be entered and goes for further execution.
But I want a solution on it. What can i do, if i want to take more than one char i/p from the user?:(

Eg: o/p will be like:
Enter Gender Male/Female(M/F): M
Enter Residence City/village(C/V): (it'll not wait here to take i/p & jumps on next line)
Enter Health Excellent/poor(E/P): E
Enter Age: 25

& because of this my further o/p will be wrong.
so, what can i do if i want to take i/p in this way only?

Recommended Answers

All 4 Replies

To get ths output u will have 2 flush the input buffer after every char input

Enter Gender Male/Female(M/F): M
Enter Residence City/village(C/V): (it'll not wait here to take i/p & jumps on next line)
Enter Health Excellent/poor(E/P): E
Enter Age: 25

printf("Enter Gender Male/Female:");
scanf("%c",&gender); //gender is char type variable
fflush(stdin);
printf("Enter Residence City/village:");
scanf("%c",&residence);
fflush(stdin);

and so on :)

To get ths output u will have 2 flush the input buffer after every char input

Enter Gender Male/Female(M/F): M
Enter Residence City/village(C/V): (it'll not wait here to take i/p & jumps on next line)
Enter Health Excellent/poor(E/P): E
Enter Age: 25

printf("Enter Gender Male/Female:");
scanf("%c",&gender); //gender is char type variable
fflush(stdin);
printf("Enter Residence City/village:");
scanf("%c",&residence);
fflush(stdin);

and so on :)

Absolutely the wrong way to flush the input buffer. See this.
And also a terrible way to input a single character. Here's why.

The concept is correct, though. Instead, use a loop and a character input function:

gender = getchar();
while (ch != '\n') ch = getchar();

thax to solve may query!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.