第二个scanf_s没执行直接跳过了是什么情况?
程序代码:
#include "stdio.h" #include "stdlib.h" #include "ctype.h" int main(void) { double *ptr = NULL; double *pstr = NULL; double sum = 0.0; int count = 0; int increment = 5; int capacity = 0; char answer = 'n'; do { if (count == capacity) { capacity += increment; if (!(pstr = (double*)malloc(capacity*sizeof(double)))) { printf("储存出错!"); exit(1) ; } if (!ptr) ptr = pstr; else { for (int i = 0; i < count; i++) *(pstr + i) = *(ptr + i); free(ptr); ptr = pstr; } pstr = NULL; } printf("请输入元素:"); scanf_s("%lf", ptr + count++); printf("你还想要输入更多元素吗?"); scanf_s("%c", &answer); } while (tolower(answer) == 'y'); for (int i = 0; i < count; i++) sum += *(ptr + i); printf("平均数是:%.2lf.\n", sum / count); free(ptr); return 0; }