When you input something that doesn't matches the required format in the scanf(), that value stays in the input buffer. So what you can do is add a getchar() in your default case.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int v, kapcs = 0;
printf("Szerinted Szaki Trisztan egy kocsog?\n1 Igen\n2 Nem\n");
do
{
kapcs = 0;
scanf("%d", &v);
switch (v)
{
case 1:
printf("Igen, o egy kocsog!");
break;
case 2:
printf("Helytelen... O egy kocsog!");
break;
default:
printf("NEM VALASZLEHETOSEG, VALASSZ: 1 VAGY 2\n");
kapcs = 1;
getchar();
break;
}
}while(kapcs == 1);
return 0;
}
Also to support my argument you could do the following:
#include
#include
int main()
{
int v, kapcs = 0;
printf("Szerinted Szaki Trisztan egy kocsog?\n1 Igen\n2 Nem\n");
do
{
kapcs = 0;
scanf("%d", &v);
switch (v)
{
case 1:
printf("Igen, o egy kocsog!");
break;
case 2:
printf("Helytelen... O egy kocsog!");
break;
default:
printf("NEM VALASZLEHETOSEG, VALASSZ: 1 VAGY 2\n");
kapcs = 1;
printf("%c",getchar());
break;
}
}while(kapcs == 1);
return 0;
}
By adding this little printf() you will notice that the getchar doesn't seem to wait for the input. Instead it simply outputs that character you just provided in the input stream.
P.S. :Please note that this answer was 'just' made for the clarification on that unmatched input part. You NEED to be careful if you input a number that doesn't match your case. In that case simply check if the scanf() conversion was successful or not by taking a count of it as following:
count=scanf("%d",&v);
and then checking in the default case as:
if(count==0){
getchar();
}