greatings to all of you!!

I start learning c++ and i getting a problem with the ENTER key... i want to ignore it and i just know how.

do{
	printf("\n");
	printf(" A : Quadrado\n ");
	printf("B : Rectangulo \n");
	printf(" C : Circulo\n");
	printf(" D : Triangulo\n");
	printf(" E : Trapezio\n");
	printf(" Indique a opcao : ");
	scanf("%c",&op);}
  //  cin.ignore(256);
//	printf(" Opcao Invalida \a");
	
 while((op!='A')&&(op!='a')&&(op!='B')&&(op!='b')&&(op!='C')&&(op!='c')&&
(op!='D')&&(op!='d')&&(op!='E')&&(op!='e')^(op==256));

i do this and when i type an invalid key ( diferent of : a,b,c,d,e) and press enter the result is that i get 2 menus 'cause it also counts the enterkey.
it's probably simple but i just don't get it!

Recommended Answers

All 6 Replies

I think your loop is wrong

int main()
{
    char op;
do{
	printf("\n");
	printf(" A : Quadrado\n ");
	printf("B : Rectangulo \n");
	printf(" C : Circulo\n");
	printf(" D : Triangulo\n");
	printf(" E : Trapezio\n");
	printf(" Indique a opcao : ");
	scanf("%c",&op);

    op = toupper(op);
  //  cin.ignore(256);
//	printf(" Opcao Invalida \a");
	
} while( op < 'A' || op > 'E');

}

Sorry, what did you want to achieve with this senseless subexpression: (op!='e')^(op==256) ?

i was trying to do the cicle while the key were dif from 'e' and equal to enter putting the ascii code of enter

while( op < 'A' || op > 'E');

this option it's not working...

I see what you mean. The Enter key remains in the keyboard buffer after scanf(), so you need to flush it out. Here is one way to do it, just call scanf() again with a different variable.

int main()
{
    char op, c;
do{
	printf("\n");
	printf(" A : Quadrado\n ");
	printf("B : Rectangulo \n");
	printf(" C : Circulo\n");
	printf(" D : Triangulo\n");
	printf(" E : Trapezio\n");
	printf(" Indique a opcao : ");
	scanf("%c",&op);
	scanf("%c",&c);

    op = toupper(op);
  //  cin.ignore(256);
//	printf(" Opcao Invalida \a");
	
} while( op < 'A' || op > 'E');

}
do{
	printf("\n");
	printf(" A : Quadrado\n ");
	printf("B : Rectangulo \n");
	printf(" C : Circulo\n");
	printf(" D : Triangulo\n");
	printf(" E : Trapezio\n");
	printf(" Indique a opcao : ");
	scanf("%c",&op);
    fflush(stdin);
    op = toupper(op);
	printf(\n);
    if ( op < 'A' || op > 'E')
	printf(" Opcao Invalida \a");
	
} while( op < 'A' || op > 'E');

this is the way that stayed...
TKX to you guys!!
my first time here..

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.