【发布时间】:2021-04-09 11:12:41
【问题描述】:
我目前正在学习 C 并想编写一个程序,该程序采用数字 ganzeZahl 来确定数组长度。
然后您必须输入存储在大小为n 的数组中的数字,然后它应该进行选择排序(我在这里剪掉了,因为我的程序甚至没有到达那部分)。
每当我尝试运行 while 循环时,我都无法通过它。它编译得很好。
它永远不会到达printf("!!!--------!!!"); //由于某种原因没有到达这部分?测试5。
#include<stdio.h>
int main() {
int ganzeZahl;
scanf("%d", &ganzeZahl);
//printf("ganze Zahl ist: %d\n", ganzeZahl); //test
int array[ganzeZahl];
int i = 0;
for(int z = 0; z < ganzeZahl; z++) {
printf("%d\n", array[z]);
}
while(i<ganzeZahl) {
//printf("!!!hier!!!\n"); //test2
scanf("%d", &array[i]);
//printf("zahl gescannt!\n"); //test 3
i++;
//printf("i erhöht!\n"); //test 4
}
printf("!!!--------!!!"); //this part isn't reached for some reason? test5
//selection sort here
//[...]
return 0;
}
【问题讨论】:
-
无法重现 - 当我使用
ganzeZahl输入 3 进行测试时,您的程序运行良好,然后输入 11 22 33。您输入了什么?以及如何? -
只是一个想法——也许在你的“丢失”
printf调用中添加一个换行符:printf("!!!--------!!!\n");——只是为了确保输出缓冲区被刷新。 -
将循环打印结果从before数据输入移动到after。
标签: c while-loop scanf