0

I have a task to add up all the numbers(or subtract them if they meet criteria). I did everything and the code works but instead of exiting when I enter '0' it runs again...

It works if I type in two zeros and the problem is that the value I enter is somehow not stored in the place that I send to...

Here`s the code:

#include <stdio.h>

int main()
{
    int sum = 0;
    int enterednumber;
    int max;
    int min;


    while (enterednumber!= 0)
    {
        scanf("%d\n", &enterednumber);

        min= enterednumber% 10;

        if(enterednumber> 9)
        {
            max = enterednumber;
            while(max> 10)
            {
                max = min / 10;
            }
        }

        if(max > min)
        {
            sum = sum + enterednumber;
        }
        else
        {
            sum = sum- enterednumber;
        }

    }

    printf("Your sum is: %d", sum);

    return 0;
}

Please don`t bother with the task itself, I got it working and I only need to know why is this happening so that I can avoid it in the future!

Stenky
  • 1
  • It should work if you take the `\n` out of the `scanf` call - `scanf` ignores whitespace by default, but adding it explicitly makes it mandatory. – Michael Horn May 24 '22 at 23:45

0 Answers0