简体   繁体   中英

Trouble changing variable in loop process

So the assignment is to ask for a variable in a certain range the print the individual digits of the numbers with three spaces in between the digit. For example 1234 should print

0 1 2 3 4
1 2 3 4
2 3 4
3 4 
4

I'm pretty sure I have most of the assignment done I am just having trouble changing the variable number in my loop. The number sorts itself into the right if statement but then when it loops instead of the number going down a digit (ie 2343 to 343) all it does is print the same number 5 times. I have researched in my book and looked online but i'm not seeing it. It's probably something simple just not sure what. Here's the code:

#include <stdio.h>
#include <stdlib.h>
void loopingDigitprinter(int digit);
int division(int* digit);

int main() 
{
    int digitPrint;



    printf("Please enter a number between 0 and 32,767: ");
    scanf("%d", &digitPrint);
    loopingDigitprinter (digitPrint);

    return 0;
}

void loopingDigitprinter(int digit)
{
    int loopLine= 0;
   int thousand;
   int hundred;
   int original;
   original = digit;



    while(loopLine < 4)
    {
        if (digit > 10000 && digit <= 32767)
            {
                thousand = digit/ 1000;
                hundred = digit % 1000;
                printf("%02d%03d\n",thousand, hundred);
                digit %= 10000;


            }
        else if (digit < 10000 && digit > 1000)
            {

              if (original > 10000)
                    {   thousand = digit/ 1000;
                        hundred = digit % 1000;
                        digit %= 1000;
                        printf("%01d%03d\n",thousand, hundred);
                    }
            else
                    {
                        thousand = digit/ 1000;
                        hundred = digit % 1000;
                        printf("%02d%03d\n",thousand, hundred);
                        digit %= 1000;
                    }

            }
         else if (digit < 1000 && digit > 100)
            {
                if (original > 10000)
                    {
                         hundred = digit % 1000;
                         printf("%d\n",  hundred);
                         digit %= 100;
                    }  
                 else if (original < 10000 && original > 1000)
                    {
                        thousand = original / 1000;
                        hundred = digit % 1000;
                        printf("%d%d\n",thousand,digit);
                        printf("%d\n", digit);
                        digit %= 100;
                        digit %= 100;

                    }
                else
                    {
                         thousand = digit/ 1000;
                        hundred = digit % 1000;
                         printf("%02d%03d\n",thousand, hundred);
                        digit %= 1000;
                         thousand = original / 1000;
                         hundred = digit % 1000;
                         printf("%d%d\n",thousand,digit);
                         printf("%d\n", digit);
                         digit %= 100;
                         printf("%d\n", digit);
                         digit %= 10;
                         printf("%d\n", digit);
                     }

             }
         else if (digit < 100 && digit > 10)
            {
                if (original > 10000)
                     {   hundred = digit % 1000;
                         printf("%d\n", hundred);
                         digit %= 10;
                         printf("%d\n", digit);
                     }
                 else if (original < 10000 && original > 1000)
                     {
                         thousand = original / 1000;
                         hundred = digit % 1000;
                         printf("%d\n",hundred);
                         digit %= 10;
                        printf("%d\n", digit);
                    }
                 else if (original < 1000 && original > 10)
                    {
                        thousand = digit/ 1000;
                        hundred = digit % 1000;
                        printf("%02d%03d\n",thousand, hundred);
                        digit %= 1000;
                        thousand = original / 1000;
                        hundred = digit % 1000;
                        printf("%d%03d\n",thousand,digit);
                        printf("%03d\n", digit);
                        digit %= 100;
                        printf("%d\n", digit);
                        digit %= 10;
                        printf("%d\n", digit);

                    }
                else
                   printf("1");
               }

        else if(original > 0 && original < 10)

            {
                 printf("0000%d\n", original);
                 printf("000%d\n", original);
                 printf("00%d\n", original);
                 printf("0%d\n", original);
                 printf("%d\n", original);
                break;
            }



         loopLine++;
        }

        return;
 }

I dont know what you are doing there as i did not read the code.

But do refer to my sample code. It pretty much does the needful

#include <stdio.h>
#include <string.h>

int main(int argc, char*argv[]){

int n,k,i=0;

printf("Enter a number please\n");
scanf("%d",&n);

while(i<=n){

    for(k=i;k<=n;k++){

        printf("%d ",k);

    }

    printf("\n");   
    i++;
}

return 0;
}


Output
------
0 1 2 3 4
1 2 3 4
2 3 4
3 4 
4

The loopLine++ is called each loop so you eventually exit, but double check your last equality checks. Digit can't be greater than 10 AND less than 0.

You may need to call division(digit) in your last else if to make it change.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM