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.

6
  • First stdio.h and stdlib.h are not libraries. Commented Oct 9, 2015 at 22:41
  • They are headers from the standard library. Commented Oct 9, 2015 at 22:50
  • ok thanks i just started 2 weeks ago and i still struggle to spot mistakes and i don't really know all the terms Commented Oct 9, 2015 at 22:52
  • @MicheleGalli - Using a copy/paste response to thank everyone for their answers is not necessary. The tooltip above add comment says "avoid comments like '+1' or 'thanks'." Commented Oct 9, 2015 at 23:01
  • yes, clearly i'm new to the website as well Commented Oct 9, 2015 at 23:04

4 Answers 4

4

You should initialize decision to 0

int decision = 0;

so that the while loop is true

while (decision == 0) {

on the first iteration.

Sign up to request clarification or add additional context in comments.

6 Comments

But prefer an infinite loop with a break.
@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.
@KarolyHorvath Why would an infinite loop be preferred?
Well you could argue that it's a matter of taste (like early returns), 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.
@KarolyHorvath I suppose it's a matter of taste, but loops use conditions for a reason. It's silly not to utilize them.
|
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;

Comments

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.

Comments

0

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

int decision = 0;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.