Ad

Why Does My While Loop (z==0) Runs Multiple Times After I Set It To Z==0 Only Once?

- 1 answer

#include <stdio.h>
int main(void) {

    int x,y,z=0,number,digit;
    char c;
    int sum = 0;
    int temp = 0;

    while (z==0){

        printf("enter 2-9 for hex\n");
        scanf("%c",&c);
        x = (int)c-48;
        if((int)c>49 && (int)c<59){
            printf("enter number");
            scanf("%d",&number);
            break;
        }
        else if ((int)c<50 || (int)c>58){
            printf("wrong number\n");
            z==0;
        }
        else
            printf("error");
            z==0;

    }
while (number != 0) {
        digit = number % 10;
        number = number / 10;
        if (digit >= x) {
            printf("wrong input!!!");
            return 0;
        }
        else {
            int temp1 = temp;
            if (temp1 == 0) {
                digit = digit * 1;
            }
            else {
                while (temp1 != 0) {
                    digit = digit * x;
                    temp1--;
                }
            }
        }
        sum = sum + digit;
        temp = temp + 1;
    }
    printf("\nThe number in Decimal is:%d", sum);

}

This is my code, its about transforming hex into decimals.The question is when I enter 2-9 everything is good, but when I enter 1, the first while loops for 2 times, if I enter 10-99 it loops 3 times, if I enter 100-999 it loops 4 times etc...

Why is it that it still runs multiple times after I set Z==0;

Ad

Answer

the situation is you condition of z is all the time true. Your start condition i z == 0, this starts the while loop. The while loop stops, when z is not more 0. So somewhere in your while loop, you have to change z at be not zero. Then your while loop stops. And please use closing and opening "{}" for if-else. That helps and increases readability.

for example:

#include <stdio.h>
int main(void) {

    int x,y,z=0,number,digit;
    char c;
    int sum = 0;
    int temp = 0;

    while (z==0){

        printf("enter 2-9 for hex\n");
        scanf("%c",&c);
        x = (int)c-48;
        if((int)c>49 && (int)c<59){
            printf("enter number");
            scanf("%d",&number);
            break;
        }
        else if ((int)c<50 || (int)c>58){
            printf("wrong number\n");
            z==0;
        }
        else {
            printf("error");
            z==0;
}
z = 1;
    }
while (number != 0) {
        digit = number % 10;
        number = number / 10;
        if (digit >= x) {
            printf("wrong input!!!");
            return 0;
        }
        else {
            int temp1 = temp;
            if (temp1 == 0) {
                digit = digit * 1;
            }
            else {
                while (temp1 != 0) {
                    digit = digit * x;
                    temp1--;
                }
            }
        }
        sum = sum + digit;
        temp = temp + 1;
    }
    printf("\nThe number in Decimal is:%d", sum);
}

Hope this helps.

Ad
source: stackoverflow.com
Ad