简体   繁体   English

在C中循环编程

[英]Programming while loop in C

First, I am a total beginner, so the question is probably very obvious for all of you, but i don't get what's wrong with the while loop in this program. 首先,我是一个初学者,所以对于所有人来说,这个问题可能都很明显,但是对于该程序中的while循环,我没有任何问题。 Te aim of the program is to calculate the average between numbers where the user inputs 0 when he wants to continue putting numbers in and inputs 1 when he wants to stop, so the loop is supposed to stop when the user puts 1 and to compute a sum of the values when he enters 0 at the end. 该程序的目的是计算用户想要继续输入数字时输入0的数字与想要停止输入数字时输入1的数字之间的平均值,因此,当用户输入1时,循环应该停止并计算a他最后输入0时的值之和。 So this is what i wrote, i used stdio.h and stdlib.h as libraries : 这就是我写的,我使用stdio.hstdlib.h作为库:

int decision;
int value;
int sum = 0;
float av;
int order = 1;

printf ("for continue press: 0\n ");
printf ("for stopping press: 1\n ");

while (decision == 0) {
    printf("input value:");

    scanf("%d", &value);
    sum = sum + value;

    printf ("continue?");

    scanf("%d", &decision);
    order = order + 1;
}
av = (float) sum / (float) order;

printf("the average is: %.2f", av);
return EXIT_SUCCESS;

what the terminal displays is just "the average is:0.00", it skips the whole operation above. 终端显示的只是“平均值为:0.00”,它跳过了上面的整个操作。

You should initialize decision to 0 您应该将decision初始化为0

int decision = 0;

so that the while loop is true 这样while循环为真

while (decision == 0) {

on the first iteration. 在第一次迭代中。

In C, declaring a variable does not initialize it. 在C语言中,声明变量不会初始化它。 So the initial value of decision is more or less random. 因此, decision的初始值或多或少是随机的。 If it's not zero (and it likely is not), the cycle is never entered. 如果它不为零(很可能不是零),则永远不会输入循环。

Perversely, when in "debug mode" or using some instrumentation such as valgrind , memory might be either zeroed or initialized consistently, leading to "unreproducible" bugs that may be difficult to track. 相反,在“调试模式”下或使用诸如valgrind工具时,内存可能被清零或被一致地初始化,从而导致“不可复制”的错误,可能难以跟踪。 That is why you really want to always initialize your variables 这就是为什么您确实要始终初始化变量的原因

Try with: 尝试:

int decision = 0;

Also, turn on all compiler warning flags. 另外,打开所有编译器警告标志。 You want to be warned when such things happen, and the compiler can do so if you tell it to. 您希望在发生此类情况时得到警告,如果您告诉您,编译器可以这样做。

Another way 其他方式

You do not need decision anywhere else, so it's good to have one less variable in the outer scope: 您无需在其他任何地方进行decision ,因此最好在外部范围中减少一个变量:

for (;;) {
    int decision; /* The variable only lives inside this loop */
    printf("input value:");

    scanf("%d", &value);
    sum = sum + value;

    printf ("continue?");
    scanf("%d", &decision);
    if (0 == decision) {
        break;
    }
    order = order + 1;
}

Notice 注意

If you start order from 1, and enter only one value, order will be increased to 2, and this will get your calculation off. 如果您从1开始order ,并且仅输入一个值,那么order将增加到2,这将使您的计算无法进行。 Either start from 0 or increase the value after decision confirmation. 从0开始或在决策确认后增加该值。

In C, simply declaring a variable does not assign it a value of 0 . 在C语言中,仅声明变量不会将其赋值为0 You have to do that. 您必须这样做。 In fact, actually using a variable that has not been initialized is undefined behavior. 实际上,实际上使用未初始化的变量是未定义的行为。 Most likely, the variable contains whatever contents was in the memory location assigned to it. 变量很可能包含分配给它的内存位置中的所有内容。

The solution is to actually define decision . 解决方案是实际定义decision

int decision = 0;

您尚未初始化decision变量,这就是为什么会出错。

int decision = 0;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM