0

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.

Joshua Dannemann
  • 2,003
  • 1
  • 14
  • 34

4 Answers4

4

You should initialize decision to 0

int decision = 0;

so that the while loop is true

while (decision == 0) {

on the first iteration.

jh314
  • 27,144
  • 16
  • 62
  • 82
  • But prefer an infinite loop with a `break`. – Karoly Horvath Oct 09 '15 at 22:45
  • 2
    @KarolyHorvath No, I think this is a fine way to use the loop. If possible, I always try to have my loops have meaningful exit conditions. `while(1)` isn't very descriptive. – PC Luddite Oct 09 '15 at 22:47
  • 2
    @KarolyHorvath Why would an infinite loop be preferred? – Kyle Strand Oct 09 '15 at 22:48
  • Well you could argue that it's a matter of taste (like early `return`s), but if you are interested in my opinion, I prefer to keep the condition and exiting from the loop close by. With the current solution you have to scan the code to figure out what happens with `decision`. – Karoly Horvath Oct 09 '15 at 23:14
  • @KarolyHorvath I suppose it's a matter of taste, but loops use conditions for a reason. It's silly not to utilize them. – PC Luddite Oct 10 '15 at 05:17
  • @PCLuddite: Well, it's silly to test a condition which is always true the first time. Use a `do while` loop then.. which, in this case, would keep the condition and exiting from the loop close by. – Karoly Horvath Oct 10 '15 at 10:45
1

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.

LSerni
  • 55,617
  • 10
  • 65
  • 107
1

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;
PC Luddite
  • 5,883
  • 6
  • 23
  • 39
0

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

int decision = 0;
Anurag Verma
  • 485
  • 2
  • 12