求问:代码中为什么最后输入c值判断是否继续的部分代码执行出错
在做课后习题“折中查找”的题目时候,自己先写了一个只能查找一次的,之后看了课后答案,将课后答案的代码输入编译后,最后面判断是否继续查找的部分代码一直执行出错,该部分代码单独拿出来执行是没有问题的;请各位大佬帮忙看下,是哪地方出了问题。
全部代码如下:
程序代码:
#include<stdio.h> #define N 15 int main() { int i, number, top, bott, mid, loca, a[N], flag = 1, sign; char c; printf("enter data:\n"); scanf("%d", &a[0]); i = 1; while (i<N) { scanf("%d", &a[i]); if (a[i]>=a[i-1]) { i++; } else { printf("enter this data again:\n"); } } printf("\n"); for ( i = 0; i < N; i++) { printf("%5d", a[i]); } printf("\n"); while (flag) { printf("input number to look for:"); scanf("%d", &number); sign = 0; top = 0; bott = N - 1; if ((number<a[0])||(number>a[N-1])) { loca = -1; } while ((!sign)&&(top<=bott)) { mid = (bott + top) / 2; if (number==a[mid]) { loca = mid; printf("Has found %d,its position is %d\n", number, loca + 1); sign = 1; } else if (number<a[mid]) { bott = mid - 1; } else { top = mid + 1; } } if (!sign || loca == -1) { printf("cannot find %d.\n", number); } printf("continue or not(Y/N)?"); scanf("%c", &c); if (c == 'N' || c == 'n') { flag = 0; } } return 0; }
[此贴子已经被作者于2017-7-18 01:29编辑过]