Michele Galli
Michele Galli

Reputation: 13

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. 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. So this is what i wrote, i used stdio.h and stdlib.h as libraries :

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.

Upvotes: 0

Views: 190

Answers (4)

LSerni
LSerni

Reputation: 57418

In C, declaring a variable does not initialize it. So the initial value of decision is more or less random. 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. 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:

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. Either start from 0 or increase the value after decision confirmation.

Upvotes: 1

Anurag Verma
Anurag Verma

Reputation: 495

You have not initialized the decision variable and that is why the error.

int decision = 0;

Upvotes: 0

PC Luddite
PC Luddite

Reputation: 6108

In C, simply declaring a variable does not assign it a value of 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.

int decision = 0;

Upvotes: 1

jh314
jh314

Reputation: 27812

You should initialize decision to 0

int decision = 0;

so that the while loop is true

while (decision == 0) {

on the first iteration.

Upvotes: 4

Related Questions