When I execute this code ,it accepts 2 nos,prints the msg enter your choice and terminates..

#include<stdio.h>
int main()
{
char ch;
int a,b;
printf("enter 2 nos :");
scanf("%d %d",&a,&b);

printf("\nenter your choice:");
ch=getchar();
printf("%c",ch);
switch(ch)
{
case '1':
        printf("sum : %f",a+b);
        break;
case '2':
        printf("diff : %f",a-b);
        break;
case '3':
        printf("mul : %f",a*b);
        break;

}

return 0;
}

But If I accept choice before accepting nos. then it works.. As follows.. Can any any body tell me d reason 4 it.. and solution as well.. Thanx in advance..
#include<stdio.h>
int main()
{
char ch;
int a,b;
printf("\nenter your choice:");
ch=getchar();
printf("%c",ch);

printf("enter 2 nos :");
scanf("%d %d",&a,&b);

switch(ch)
{
case '1':
        printf("sum : %f",a+b);
        break;
case '2':
        printf("diff : %f",a-b);
        break;
case '3':
        printf("mul : %f",a*b);
        break;

}

return 0;
}

You are attempting to read a single character, right?

When you enter that character, what key(s) to you type? The character and ENTER, right?

Well, the ENTER is also put into the input buffer, but when you read a single character, only the first character was removed from the buffer. The ENTER is left for the next input.

The same happens when typing numbers. The numbers are read and the ENTER is left.

So here's what is happening. With this code (please note how the code looks when you use CODE tags as The Forum Rules recommend -- you must have missed that when you read them after registering :icon_wink:) :

printf("enter 2 nos :");
scanf("%d %d",&a,&b);

printf("\nenter your choice:");
ch=getchar();
printf("%c",ch);

Here's what happens:
Display message "enter 2 nos :"
You enter for example "2 3[ENTER]" -- note that is 4 characters, including the SPACE scanf() reads the two numbers like this
a) '2' is read, converted to integer, and loaded into a
b) Whitespace is ignored (SPACE is skipped over) and the 3 is loaded into b
c) ENTER is left in the input buffer.
Display message"enter your choice"
ENTER is read by getchar() and loaded into ch.


Your next attempt is:

printf("\nenter your choice:");
ch=getchar();
printf("%c",ch);

printf("enter 2 nos :");
scanf("%d %d",&a,&b);

Here's what happens this time:
Display message"enter your choice"
You enter for example "x[ENTER]"
'x' is read by getchar() and loaded into ch.
ENTER is left in the input buffer
Display message "enter 2 nos :" scanf() reads the ENTER which could not be converted into an integer, and since it is considered whitespace, it is simply ignored. scanf() continues to wait until another ENTER is typed.
You now type in "2 3[ENTER]" and everything is handled just like before.

Check out this series on scanf() for more information.

So the solution for that porblem is to stop using scanf function and perhaps WaltP has give you a good link on what are the function which need to avoided and it also explains about the scanf in more details so start thinking about an alter native way of reading values from the user.

Or if you still wanted to use scanf function then call the following function after every call on scanf function.

void Clear_Buffer(void)
{
     int ch;
     while((ch = getchar()) != '\n' && ch != EOF);
}

What this code does is just clears the input buffer, which resolved your problem.

Hope helps :)

ssharish

U can try to use function like "fflush(stdin)",when u call it ,it can also clear the input buffer

U can try to use function like "fflush(stdin)",when u call it ,it can also clear the input buffer

Don't use fflush to clear input buffer, it there to clear just the output buffer. Read this

ssharish

You are attempting to read a single character, right?

When you enter that character, what key(s) to you type? The character and ENTER, right?

Well, the ENTER is also put into the input buffer, but when you read a single character, only the first character was removed from the buffer. The ENTER is left for the next input.

The same happens when typing numbers. The numbers are read and the ENTER is left.

So here's what is happening. With this code (please note how the code looks when you use CODE tags as The Forum Rules recommend -- you must have missed that when you read them after registering :icon_wink:) :

printf("enter 2 nos :");
scanf("%d %d",&a,&b);

printf("\nenter your choice:");
ch=getchar();
printf("%c",ch);

Here's what happens:
Display message "enter 2 nos :"
You enter for example "2 3[ENTER]" -- note that is 4 characters, including the SPACE scanf() reads the two numbers like this
a) '2' is read, converted to integer, and loaded into a
b) Whitespace is ignored (SPACE is skipped over) and the 3 is loaded into b
c) ENTER is left in the input buffer.
Display message"enter your choice"
ENTER is read by getchar() and loaded into ch.


Your next attempt is:

printf("\nenter your choice:");
ch=getchar();
printf("%c",ch);

printf("enter 2 nos :");
scanf("%d %d",&a,&b);

Here's what happens this time:
Display message"enter your choice"
You enter for example "x[ENTER]"
'x' is read by getchar() and loaded into ch.
ENTER is left in the input buffer
Display message "enter 2 nos :" scanf() reads the ENTER which could not be converted into an integer, and since it is considered whitespace, it is simply ignored. scanf() continues to wait until another ENTER is typed.
You now type in "2 3[ENTER]" and everything is handled just like before.

Check out this series on scanf() for more information.

Thanx alot..
Understood d concept...
I will definately take care of d tag next time...

Don't use fflush to clear input buffer, it there to clear just the output buffer. Read this

ssharish

I don't agree with U.
u can search "fflush" in MSDN!
and it's give the example like this:

/* FFLUSH.C */

#include <stdio.h>
#include <conio.h>

void main( void )
{
   int integer;
   char string[81];

   /* Read each word as a string. */
   printf( "Enter a sentence of four words with scanf: " );
   for( integer = 0; integer < 4; integer++ )
   {
      scanf( "%s", string );
      printf( "%s\n", string );
   }

   /* You must flush the input buffer before using gets. */
   fflush( stdin );
   printf( "Enter the same sentence with gets: " );
   gets( string );
   printf( "%s\n", string );
}
#include <stdio.h>
#include <conio.h>

void main( void )
{
   int integer;
   char string[81];

   /* Read each word as a string. */
   printf( "Enter a sentence of four words with scanf: " );
   for( integer = 0; integer < 4; integer++ )
   {
      scanf( "%s", string );
      printf( "%s\n", string );
   }

   /* You must flush the input buffer before using gets. */
   fflush( stdin );
   printf( "Enter the same sentence with gets: " );
   gets( string );
   printf( "%s\n", string );
}

Windows tent to have their own standards, but not on Unix world. And hence u find those code with uses gets and fflush function. Very interesting code.

I am sorry, i am on unix world.

ssharish

>I don't agree with U.
You can disagree all you want, but you're still wrong.

>u can search "fflush" in MSDN!
MSDN doesn't define the standard library, the C standard does. And the C standard says that calling fflush on an input stream is undefined. If your compiler happens to support an extension that allows fflush(stdin) , feel free to use it. But until this forum is renamed to "C for Beair.GQ's compiler", you'll be promptly corrected if you try to encourage undefined behavior with constructs like that.

>and it's give the example like this:
MSDN's examples are well known to be examples of what not to do. The code you posted is broken in four places, has three instances of non-portable constructs, and three instances of non-portable usage. Using MSDN to back up your claims is a folly.

Comments
Well said, and beat me to it :)
This article has been dead for over six months. Start a new discussion instead.