Clear input buffer

Hello,
For a project I have to write a program that controls a sorting machine connected to a PC running QNX 6.5.0 on it. To control the machine I use a switch case.
The problem is I don’t know how to clear the input buffer.

Here is the code:

[code]do
{
read ( petra_in , &u_capt.byte , 1 );

	printf("\n\n\n\tACTIVATEURS\n");
	printf("*******************\n\n");
	printf("CP (Carriage position) : %x \t[1, 2, 3, 4]\n", u_act.act.CP);
	printf("C1 (Convoyeur 1): %x \t[a]\n", u_act.act.C1);
	printf("C2 (Convoyeur 2): %x \t[z]\n", u_act.act.C2);
	printf("PV (Plongeur vaccum): %x \t[v]\n", u_act.act.PV);
	printf("PA (Plongeur activate): %x \t[b]\n", u_act.act.PA);
	printf("AA (Arm activate): %x \t[e]\n", u_act.act.AA);
	printf("GA (Arm Gripper activate): %x \t[g]\n", u_act.act.GA);

	printf("\n\n");
	printf("\n\tCAPTEURS\n");
	printf("*******************\n\n");
	printf("DE (Parts dispenser sensor): %x \n\r", u_capt.capt.DE);
	printf("AP (Arm position sensor): %x \n\r", u_capt.capt.AP);
	printf("CS (Carriage in position): %x \n\r", u_capt.capt.CS);
	printf("PP (Plunger position): %x \n\r", u_capt.capt.PP);
	printf("T (Material thickness sensor): %x \n\r", u_capt.capt.T);
	printf("S (Slot detection sensor): %x \n\r", u_capt.capt.S);
	printf("L1 (Cut-out detection sensor): %x \n\r", u_capt.capt.L1);
	printf("L2 (Cut-out detection sensor): %x \n\r", u_capt.capt.L2);

	printf("\nq pour quitter\n\n");
	printf("-> ");


            fflush(stdin);

	scanf("%c", &c);

	switch(c)
	{
		case '1' ://CPpose1

				u_act.act.CP = 0;

			break;
		case '2' ://CPpose2

				u_act.act.CP = 1;

			break;
		case '3' ://CPpose3

				u_act.act.CP = 2;

			break;
		case '4' ://C1pose4

				u_act.act.CP = 3;

			break;
		case 'a' ://C1

				if(u_act.act.C1 == 1)
					u_act.act.C1 = 0;
				else
					u_act.act.C1 = 1;

			break;
		case 'z' ://C2

				if(u_act.act.C2 == 1)
					u_act.act.C2 = 0;
				else
					u_act.act.C2 = 1;

			break;
		case 'v' ://PV

				if(u_act.act.PV == 1)
					u_act.act.PV = 0;
				else
					u_act.act.PV = 1;

			break;
		case 'b' ://PA

				if(u_act.act.PA == 1)
					u_act.act.PA = 0;
				else
					u_act.act.PA = 1;

			break;
		case 'e' ://AA

				if(u_act.act.AA == 1)
					u_act.act.AA = 0;
				else
					u_act.act.AA = 1;
			break;
		case 'g' ://GA

				if(u_act.act.GA == 1)
					u_act.act.GA = 0;
				else
					u_act.act.GA = 1;
			break;

        default : printf("Touche inconnue \n");
            //break;
	}
	rc= write ( petra_out , &u_act.byte , 1 );
	if (rc<=0)
	{
		printf ("rc: %d\n ",rc);
		perror("");
	}
	printf("\n\n");
}while(c != 'q');[/code]

I know that fflush(stdin) is not portable, so I have tried the following:

  1. Replacing fflush(stdin); with fseek(stdin, 0, SEEK_END); Didn’t work.

2.Replacing fflush(stdin); scanf("%c", &c); with

do{ c = getchar(); while(c != '\n' && c != EOF);Didn’t work either (Maybe this one doesn’t work because I have not placed c = getchar() in the while loop, but im not sure)

Or is it because of the ‘default’ case in the switch case(); ?

I have no way to test and I must have finished the project for next week, that’s why I’m asking.

Bonjour,

Why do you want to flush stdin ?

Nicolas

Bonjour 8)

Because of the ‘\n’ new line character, when I press a key the process is always looping twice:
First loop: the caracter (i.e. a, b, c, d…)
Second loop: the new line caracter, that’s where is goes in the default case and prints “Touche inconnu”

Daniel

Please try “scanf(”%c\n", &c);"

Take a look at

tcflush()

eg.

  tcflush(fileno(stdin), TCIFLUSH);

char buff[5];

I used fgets(buff, 5, stdin); which also can be used with stdin but I just read the first case of the array (buff[0]) to be sure that I will get the first character and nothing else