这是一段从升序排列的数组中查询数据位置的程序,在程序末尾有一段询问“是否继续查询”的语句,里面决定着程序是否要继续While循环。但是它好像失效了?程序运行到Printf("Continue or not?(Y/N) ");之后就好像自动跳过了下面的scanf()和if()部分,直接循环了;请问是哪里出问题了吗?谢谢~
#代码如下:
#include<stdio.h>
#define N 15
int main(){
int i, a[N], loca, sign, mid, bott, top, flag = 1, number;
char ch;
printf("Enter data:\n");
scanf("%d", &a[0]);
for(i = 1; i < N; i ++){
scanf("%d", &a[i]);
while(a[i] < a[i - 1]){
printf("Enter error. The current data must be greater than before, enter again:\n");
scanf("%d", &a[i]);
}
}
printf("\n");
printf("The data is: \n");
for(i = 0; i < N; i ++){
printf("%5d", a[i]);
}
while(flag){
sign = 0;
top = 0;
bott = N - 1;
printf("\nWhich number that's you wanna look for? ");
scanf("%d", &number);
if(number < a[top] || number > a[bott]){
loca = -1;
}
while(!sign && top <= bott){
mid = (top + bott) / 2;
if(number == a[mid]){
printf("Has found. The position of %d is a[%d].\n", number, mid);
sign = 1;
}
else if(number > a[mid]){
top = mid + 1;
}
else{
bott = mid - 1;
}
}
if(loca == -1 || !sign){
printf("Sorry.cannot find it.\n");
}
printf("Continue or not?(Y/N) ");
scanf("%c", &ch);
if(ch == 'n' || ch == 'N'){
flag = 0;
}
}
return 0;
}