Aviv Gold Aviv Gold - 4 days ago 6
C Question

scanf and the char variable don't get a long

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

void main(){
int turn,i=1,num;
char answer;

for(i>0;i++;){
printf("Please enter a number in the range 1-5:");
scanf("%d",&num);

if (num == 1){
printf("Disconnecting\n");
continue;
}

else if(num == 2){
continue;
}

else if(num == 3){
printf("Are you sure you would like to finish your order??? \nplease enter one char:");
scanf("%c", &answer);
if(answer == 'y'){
printf("Canceled");
break;
}

else{
continue;
}
}

else if(num == 4){
turn=i-1;
printf("your position in queue is:%d\n",turn);
continue;
}

else if(num == 5){
break;
}

else {
printf("Wrong input\n");
continue;
}

}
getch();
}


I'm using c language for this,
if You look at the


else if(num == 3)


it should function in a way that when I enter the letter y,Y it will say canceled and end the program and if not it will just reset the loop.
now when I use the number 3 and give the variable c the letter 'y'
it just says nothing and acts like I gave it the command "continue" + pressed the number 3 again although all I have done is press y or Y and enter.
everything else is good.
I would be glad if anyone can tell me how to fix that.

Answer
for(i>0;i++;){
printf("Please enter a number in the range 1-5:");
scanf("%d",&num);getchar();
//... rest of your code
}

use getchar or getch to consume extra newline character

Comments