problem with scanf

hi all!

i've written a simple c program:

#include<stdio.h>
#include<stdlib.h>

int main()
{
int a;
char b[20];
char c[20];
printf("\nenter the value of b:");
scanf("%[^\n]",b);
printf("\nb=%s",b);
printf("\nenter the value of a:");
scanf("%d",&a);
printf("\na=%d",a);
printf("\n enter the values of c:");
scanf("%[^\n]",c);
printf("\nc=%s",c);
return 0;
}

the output i m gettin is:
enter the value of b:a

b=a
enter the value of a:1

a=1
enter the values of c:
c=?

the program does not wait to accept the value of c and just prints what ever value it holds.
can ny one help me out with this.....

thanks

you have instructed scanf to read for the variable
except "\n" but "\n" is still there in the buffer,
hence for concurrent reads of the same datatype
flush the "\n" (buffer) before you could proceed ...
to read the next variable's value

hi,
First, fflush on stdin is not guaranteed. Some ppl might suggest you to use getchar to flush the stdin. But again that is not a clean way, i think so.
There are other options like using the gets (or fgets which is secure ) then convert the input to whatever format you want using the sscanf (string scanf). At the moment I would suggest you to use fgets and sscanf. However in instance where you want a key board input such that you dont want to wait for the enter key, for eg, u want something to happen when a user types in 'q' ( for eg) without having to press the enter key after typing q, ( this is called as hotkey) you may want to write your own keyboard handling function, I use the one below sometimes, this is for unix system. for windows, it will be different.

I have also added the main function to this function so you can better understand how mygetch works:

/* 
Desc: The main intent of this program is the function mygetch. This function is
	written to input one character at a time. The input does not await the enter
	key response from the user.
Author: Hemant Borole
Date: 27Jan05 10:06 am
*/

#include <stdio.h>
#include <termios.h>
#include <unistd.h>

#define BUFFSIZE 64		/* Buffer size for input */
/* this function will return immediately after a key is
   pressed. You do not have to wait till the enter key is hit */

int mygetch( ) {

	/* Handles to old and new termios structures */
	struct termios oldt, newt;
	int ch;

	/* store current termios to restore back later */
	tcgetattr( STDIN_FILENO, &oldt );
	newt = oldt;

	/* set to canonical. turn off echo */
	newt.c_lflag &= ~( ICANON | ECHO );
	tcsetattr( STDIN_FILENO, TCSANOW, &newt );

	/* Read the character */
	ch = getchar();

	/* Reset terminal */
	tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
	return ch;
}

/* Main body, to test the mygetch, convert the character immediately
   to upper case. Do not have to wait till user inputs, presses enter
   and then convert to upper case */

int main()	{
	char c = '\0';
	char str[BUFFSIZE];
	char inp[BUFFSIZE];
	int i = 0;

	str[0] = '\0';

	printf("Type q to quit this program or press enter. \n");
	printf("Anyway the program will self kill after %d characters of input\n",BUFFSIZE);
	while ( 1 )	{
		if ( i >= BUFFSIZE ) break;

		c = mygetch();
		if ( c == 'q' || c == '\n' ) break;

		if ( c >='a' && c <='z' )	{
			printf("%c",c-32);
			str = c-32;
		}
		else	{
			printf("%c",c);
			str = c;
		}
		inp = c;
		i++;
	}
	printf("\nYou typed: %s\n",inp);
	printf("\nConverted output: %s\n",str);
}

Thanks for this code snippet. Could you please help me what if I have to do both simultaneously like in shell when Ctrl+l is pressed it clears the screen and the shell waits for the input also when it is a long command.

Thanks

linuxpenguin, please use code tags when you post large snippets of code. It makes code easier to read.