Hi
This my first c programme.It asks 5 objective questions and displays the result at the end.
But when i compile it and run, it asks only for variables A,C and E .It doesn't ask for the inputs to question no. 2 and 4.It only displays these questions and skip the input part.
Please let me know what's the problem with it.

/*OBJ Ques*/
#include<stdio.h>
 main()
{
char A,B,C,D,E ;
int  AA,BB,CC,DD,EE,res;

printf("\n1.Who amongst the following is the Head of the RBI at present ?\na.Mr. 

M.V.Kamath\nb.Mr. Y.V.Reddy\nc.Mr. N.R.Narayanmurthy\nd.Mr.O.P.Bhatt");

scanf("%c",&A);

printf("\n2. Mjority of rural people still prefer to go to which of the following for their 

credit needs ?\na.Money lenders\nb.Foreign Bankers\nc.NABARD\nd.RBI");

scanf("%c",&B);

printf("\n3. India has different categories of commercial banks.Which of the following is 

NOT one such categories ?\na.Private Banks\nb.Commodities Banks\nc.Nationalized 

Banks\nd.Cooperative Banks");
 
scanf("%c",&C);

printf("\n4.Which of the following countries does not play International 

Cricket?\na.Russia\nb.England\nc.SouthAfrica\nd.Pakistan");

scanf("%c",&D);

printf("\n5.Which is the world’s first credit card?\na.American 

Express\nb.Visa\nc.Mastercard\nd.Diners Club Card");

scanf("%c",&E);

if(A=='b') AA=1; else AA=0;

if(B=='b') BB=1; else BB=0;

if(C=='b') CC=1; else CC=0;

if(D=='b') DD=1; else DD=0;

if(E=='b') EE=1; else EE=0;

res=AA+BB+CC+DD+EE ;

printf("\nYour score=%d",res);

}

I modified your code somewhat so you can see what's going on...
1. Run the code below, what values did you get for A, B, C, D, E ?
2. Now un comment the fgetc(stdin) lines and run the code again what are the values now

This should tell you exactly what's going on with your program and why its skipping every second prompt..

#include<stdio.h>

int main()
{
	char A,B,C,D,E ;
	int  AA,BB,CC,DD,EE,res;

	printf("\n1.Who amongst the following is the Head of the RBI at present ?\na.Mr. M.V.Kamath\nb.Mr. Y.V.Reddy\nc.Mr. N.R.Narayanmurthy\nd.Mr.O.P.Bhatt");

	scanf("%c",&A);
	/*fgetc(stdin);*/

	printf("\n2. Mjority of rural people still prefer to go to which of the following for their credit needs ?\na.Money lenders\nb.Foreign Bankers\nc.NABARD\nd.RBI");

	scanf("%c",&B);
	/*fgetc(stdin);*/

	printf("\n3. India has different categories of commercial banks.Which of the following is NOT one such categories ?\na.Private Banks\nb.Commodities Banks\nc.Nationalized Banks\nd.Cooperative Banks");
	 
	scanf("%c",&C);
	/*fgetc(stdin);*/

	printf("\n4.Which of the following countries does not play International Cricket?\na.Russia\nb.England\nc.SouthAfrica\nd.Pakistan");

	scanf("%c",&D);
	/*fgetc(stdin);*/

	printf("\n5.Which is the world’s first credit card?\na.American Express\nb.Visa\nc.Mastercard\nd.Diners Club Card");

	scanf("%c",&E);
	/*fgetc(stdin);*/

	fprintf(stdout, "a->%u\n", A);
	fprintf(stdout, "b->%u\n", B);
	fprintf(stdout, "c->%u\n", C);
	fprintf(stdout, "d->%u\n", D);
	fprintf(stdout, "e->%u\n", E);
	return 0;
}

Edited 6 Years Ago by gerard4143: n/a

Every time you type a character in at scanf("%c",); , do you hit an ENTER? Where does that second character go? It gets read with the next scanf() . See this series about why you should not use scanf() for character & string input. Use fgets() instead.

And in your case, you can define a string instead of a single character, use fgets() to read the input (which automatically clears the input buffer -- within reason), and simply look at the first character in the string for your character. Yes, it's a little kludgy, but that's the problem with keyboard input in C unfortunately.

After reading your replies my doubt about buffer problem is verified and i corrected it by using "fflush(stdin) ;" after every "printf();".I found the explanation for it in a book . Thanks for the hints ......@gerard414 and @WaltP because i am a beginner and i wasn't able to understand your full explanations but i took the hints.(major hint was given by stranger510)

Edited 6 Years Ago by sunshine24: n/a

Comments
WRONG - fflush(stdin) is NOT the answer to your problem - read the thread again, and learn all about fgets()
This question has already been answered. Start a new discussion instead.