Home > OS >  I have two while loops next to each other, but how do I run the second while loop with the original
I have two while loops next to each other, but how do I run the second while loop with the original

Time:10-15

Sorry for this weird question but I'm only a beginner. I created a program with two while loops next to each other. The first while loop takes user input and repeats until input=0. When the second while loop begins, it takes the input = 0 from the first while loop. How to run the second while loop with original user input? Thank you.

unsigned long long int inp,inp1,rem,rem1,ans=0,ans1=0,place_value=1,place_value1=1;
            
        printf("Please input binary for conversion:\n");
        scanf("%llu", &input);
        
        inp=input;
            while (input>0){
                rem=input%10; 
                ans=ans rem*place_value;  
                input=input/10;
                place_value=place_value*2; 
            }
            while (input!=0){
                rem1 = inp1 % 10;
                ans1 = ans1   rem * place_value1;
                place_value1 = place_value1* 2;
                inp1 = inp1 / 10;
            }

        printf("%d in Binary is %llu in Decimal Form.\n\n", inp,ans);
        printf("%d in Binary is %d in Octal Form.\n", inp1,ans1);
        printf("");

CodePudding user response:

I finally found a fix without functions. Thank you to everyone for helping me. Declaring another variable was indeed the fix, I just needed to refine it a bit more.

        else if(op=='B' || op=='b'){
        unsigned long long int inp,rem=0,ans=0,place_value=1;

        printf("You have chosen Binary to Decimal and Octal\n");
        printf("Please input binary for conversion:\n");
        scanf("%llu", &input);

        inp=input;
            while (input>0){
                rem=input%10; 
                ans=ans rem*place_value;  
                input=input/10;
                place_value=place_value*2; 
            }
        unsigned long long int rem1=0,ans1=0,place_value1=1;
        input1=inp;
            while (input1!=0){
                rem1 = input1 % 10;
                ans1 = ans1   rem1 * place_value1;
                place_value1 = place_value1 * 2;
                input1 = input1 / 10;
            }
            
        printf("%llu in Binary is %llu in Decimal Form.\n\n", inp,ans);
        printf("%llu in Binary is %o in Octal Form.\n", inp,ans1);

        main();
    }

CodePudding user response:

The easiest solution would be to define a new variable to store the input value. long long value = input

But I think this problem would be easier to resolve/read with recursion.
Here you have a working example.

CodePudding user response:

You have way too many variables in your code. And similar names which only differ by some appended number is a mess. And you are mixing it all up. For instance you never assign a value to inp1 and you use input where it should have been inp1. You use rem where it should be rem1. It's too complicated... It's time to use functions...

unsigned long long int foo(unsigned long long int input)
{
    unsigned long long int rem;
    unsigned long long int ans=0;
    unsigned long long int place_value=1;

    while (input>0){
       rem = input % 10; 
       ans = ans   rem * place_value;  
       input = input / 10;
       place_value = place_value * 2; 
    }

    return ans;
}

unsigned long long int bar(unsigned long long int input)
{
    unsigned long long int rem;
    unsigned long long int ans=0;
    unsigned long long int place_value=1;

    while (input!=0){
        rem = input % 10;
        ans = ans   rem * place_value;
        place_value = place_value * 2;
        input = input / 10;
    }

    return ans;
}

and then use the functions like

unsigned long long int input, ans_foo, ans_bar; 
        
printf("Please input binary for conversion:\n");
scanf("%llu", &input);
    
ans_foo = foo(input);
ans_bar = bar(input);

printf("%d in Binary is %llu in Decimal Form.\n\n", input, ans_foo);
printf("%d in Binary is %d in Octal Form.\n", input, ans_bar);
printf("");

Note To me it seems that your two while loops are doing the same... but that's an unrelated mistake.

  • Related