[英]After the first loop run, i get stuck at the default case inside the switch
1.由于某种原因,我在第一次循环运行后陷入默认情况,无论输入'y'还是'n'都无关紧要。
while(edit==1){
option='\0';
printf("Would you like to edit?(y or n)\n");
scanf("%c",&option);
getchar();
// getchar() so if forces scanf()
switch(option){
// first run is flawless
case 'y':
printf("What would like to edit?\n");
printf("1)Edit an entire line.\n");
printf("2)Edit a substring of a line.\n");
scanf("%c",&option);
if(option=='1'){
} else if(option =='2'){
printf("What word to change :");
scanf("%s",toFind);
printf("What would to change \"%s\" to :", toFind);
scanf("%s",replaceWith);
printf("which line to search :");
scanf("%d",&line);
replaceInString(buffer[line], toFind, replaceWith);
printBuffer(buffer);
} else printf("Invalid input!!!\nTry again\n");
edit=1;
break;
case 'n':
edit=0;
printBuffer(buffer);
break;
default:
printf("Invalid input!!!\nTry again\n");
// first run is fine then i get stuck here.
}
}
当您使用scanf阅读角色时 - 您将输入y并按。 y将被分配给选项(即选项 - Y)并将在缓冲区中。 当您调用scanf时,缓冲区中的下一个时间将被分配给选项,因此转到默认情况。 为了避免这种情况,请在scanf中的%c之前留一个空格。 在整数或浮点数的情况下不会发生这种情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.