Thread: Not able to print the output of the conversion problem

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    69

    Not able to print the output of the conversion problem

    Dear All,


    I have written a program which converts an input decimal number into any number in a base between 2 and 36. Below is my program:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
        int newbase,n,i,ascii,ndigit=0,q,r,zero;
        char newrep[100];
        printf("Enter the number to be converted");
        scanf("%d",&n);
        printf("Enter the base between 2 and 36 to which the number is to be converted");
        scanf("%d",&newbase);
        zero=48;
        q=n;
        ndigit=0;
        while(q!=0)
        {
        r=q%newbase;
        ndigit+=1;
        ascii=zero+r;
        if(ascii>57)
        {
                ascii=ascii+7;
        }
        newrep[ndigit]=(char)ascii;
        q=q/newbase;
        }
        printf("Base %d representation of %d is\n",newbase,n);
        for(i=ndigit;i<1;i--)
        {
        printf("%s",newrep[ndigit]);
        }                              
        system("pause");
        return 0;       
    }
    I am getting correct values of newbase and ndigit but am unable to print the value of the converted output stored in newrep array. For example if I enter input number as 15 and base as 8 then I should get 17 as output. Kindly help me out to print the value of the output array. Thank you.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Your indentation could use some work. One of the first 3 styles mentioned here is good: Indent style - Wikipedia, the free encyclopedia.

    When I compile your code, I get the following warning. If you don't, turn up the settings on your compiler.
    Code:
    $ make foo
    gcc -Wall -g -std=c99  -lm -lpthread -lcurses -lefence  foo.c   -o foo
    foo.c: In function ‘main’:
    foo.c:29: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’
    You need a string to pass to printf if you are using the %s modifier. Namely, a char array that is null terminated. More than likely though, you want to print individual characters with the %c modifier. A couple other things though.

    Lastly, don't use magic numbers like 48 and 57 and 7, or confusing variable names like zero, which is set to 48. Not only are they ASCII specific, they're confusing. Use char literals like '0', '9' and 'A'. Better yet, use an array with all the digits in it. Try:

    Code:
    char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    ...
    newrep[ndigit] = digits[r];
    That way, you are not ASCII specific, and don't use awful magic numbers.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    Hi,

    Thank you for the reply and suggestions, i tried out the modifications that you suggested, but I am still unable to print the output array of the converted number. Below is my modified code:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
        int newbase,n,i,ascii,ndigit=0,q,r,zero;
        char newrep[100];
        char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        printf("Enter the number to be converted");
        scanf("%d",&n);
        printf("Enter the base between 2 and 36 to which the number is to be converted");
        scanf("%d",&newbase);
    /*  zero=48;*/
        q=n;
        ndigit=0;
        while(q!=0)
        {
        r=q%newbase;
        ndigit+=1;
    /*  ascii=zero+r;
        if(ascii>57)
        {
                ascii=ascii+7;
        }*/
        newrep[ndigit] = digits[r];
    /*    newrep[ndigit]=(char)ascii; */
        q=q/newbase;
        }
        printf("Base %d representation of %d is\n",newbase,n);
        for(i=ndigit;i<1;i--)
        {
        printf("%c",newrep[ndigit]);
        }                              
        system("pause");
        return 0;       
    }

    Quote Originally Posted by anduril462 View Post
    Your indentation could use some work. One of the first 3 styles mentioned here is good: Indent style - Wikipedia, the free encyclopedia.

    When I compile your code, I get the following warning. If you don't, turn up the settings on your compiler.
    Code:
    $ make foo
    gcc -Wall -g -std=c99  -lm -lpthread -lcurses -lefence  foo.c   -o foo
    foo.c: In function ‘main’:
    foo.c:29: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’
    You need a string to pass to printf if you are using the %s modifier. Namely, a char array that is null terminated. More than likely though, you want to print individual characters with the %c modifier. A couple other things though.

    Lastly, don't use magic numbers like 48 and 57 and 7, or confusing variable names like zero, which is set to 48. Not only are they ASCII specific, they're confusing. Use char literals like '0', '9' and 'A'. Better yet, use an array with all the digits in it. Try:

    Code:
    char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    ...
    newrep[ndigit] = digits[r];
    That way, you are not ASCII specific, and don't use awful magic numbers.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Your for-loop is wrong. The loop will only execute while the condition between the semicolons is true. Converting your for-loop to the exact equivalent while loop produces:
    Code:
        i = ndigit;
        while (i < 1)
        {
            printf("%c", newrep[ndigit]);
            i--;
        }
    Do you see the problem now?!

    Also take note of the much better indentation and formatting.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    Hi iMalc,


    I have used the code provided by you, thank you for helping me out, but am still unable to see the output on my screen. For example, if I enter 15 as input number and 8 as the base, I should get 17 on my screen as the output, whereas I am not getting anything. Kindly help me out.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
        int newbase,n,i,ascii,ndigit=0,q,r,zero;
        char newrep[100];
        char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        printf("Enter the number to be converted");
        scanf("%d",&n);
        printf("Enter the base between 2 and 36 to which the number is to be converted");
        scanf("%d",&newbase);
    /*  zero=48;*/
        q=n;
        ndigit=0;
        while(q!=0)
        {
        r=q%newbase;
        ndigit+=1;
    /*  ascii=zero+r;
        if(ascii>57)
        {
                ascii=ascii+7;
        }*/
        newrep[ndigit] = digits[r];
    /*    newrep[ndigit]=(char)ascii; */
        q=q/newbase;
        }
        printf("Base %d representation of %d is\n",newbase,n);
        i = ndigit;
        while (i < 1)
        {
            printf("%c",newrep[ndigit]);
            i--;
        }              
        system("pause");
        return 0;       
    }
    Thank you for your reply.

  6. #6
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Sigh. The code that iMalc posted was an equivalent of your (BROKEN!) for loop, to show you what was wrong with it. It was not a copy/paste source for a fix. Of *course* the program is still broke. Because you still haven't worked out what's broke in order to fix it despite iMalc given you a HUGE clue.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  7. #7
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    I have replaced the old for loop with the new while loop suggested by iMalc. I have used the debugger and according to it everything up to the while loops works perfectly. The problem is just in printing the output which is stored in newrep array. Looking forward to your suggestions.

    Quote Originally Posted by ledow View Post
    Sigh. The code that iMalc posted was an equivalent of your (BROKEN!) for loop, to show you what was wrong with it. It was not a copy/paste source for a fix. Of *course* the program is still broke. Because you still haven't worked out what's broke in order to fix it despite iMalc given you a HUGE clue.

  8. #8
    Registered User
    Join Date
    Apr 2012
    Posts
    13
    Add this line above your last while loop and check what it gives:

    Code:
    printf("\ni = %d\n", i);
    Now check again your while loop and voila

  9. #9
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Quote Originally Posted by abhishekcoder View Post
    I have replaced the old for loop with the new while loop suggested by iMalc.
    The one that they and I have just told you is 100% the same as your for loop (which didn't work)? And that maybe that's a hint as to why it doesn't work? And maybe that's a hint that the ORIGINAL loop and any EQUIVALENT loop are fundamentally wrong because of something quite basic. Maybe that's WHY iMalc posted a simplified, but equivalent, example that should be setting off alarm bells as soon as you read it?

    Oh, no, sorry. You were just after a copy/paste no-brainer fix to make things work, obviously.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  10. #10
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    Hi,

    Thanks for your reply. This line gives me the number of digits in my converted number. I am already able to print/retrieve this using ndigit variable. However I am unable to understand why my final printf statement is unable to print the value of the converted number, I have tried changing the output formats in my printf statement (%s, %c, %d etc.) but am still unable to print the output.

    @ledow - thank you for your clarification. I hope you try to put your suggestions in a more legible format rather than throwing in self-concocted slangs. If you are such an ace, I hope you have seen the thing that is "fundamentally wrong" right away and can point it out. For your information, I am looking forward to learn from my mistakes rather than simply copy-pasting code and getting away with a solution.

    Quote Originally Posted by dpitz View Post
    Add this line above your last while loop and check what it gives:

    Code:
    printf("\ni = %d\n", i);
    Now check again your while loop and voila

  11. #11
    Registered User
    Join Date
    Apr 2012
    Posts
    13
    I think you didn't check your while loop after seeing the printed value of i ^^.
    If you choose to convert 10 to base 2 (1010), you'll get i=4, since 1010 has 4 digits. What we're trying to say is that your while loop is
    Code:
    while(i < 1) { ... }
    , what doesn't make sense, since i isn't smaller than 1.

    I just would like to say that the fact that he hasn't found the error does not mean he didn't try or that he just copy-pasted the code. Some errors are so obvious that we don't see them.

    Hope it's clear now.

  12. #12
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    Thanks dpitz. Your suggestion was right on target,I missed to spot the obvious, I have changed the statement to
    Code:
    while (i>1)
    in my code and also found that I was printing with the wrong index values.

    Instead of printing with index value i, I was printing with index value ndigit in my output loop. Here's my modified code that gives me the right answer.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
        int newbase,n,i,ascii,ndigit=0,q,r,zero;
        char newrep[100];
        char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        printf("Enter the number to be converted");
        scanf("%d",&n);
        printf("Enter the base between 2 and 36 to which the number is to be converted");
        scanf("%d",&newbase);
    /*  zero=48;*/
        q=n;
        ndigit=0;
        while(q!=0)
        {
        r=q%newbase;
        ndigit+=1;
    /*  ascii=zero+r;
        if(ascii>57)
        {
                ascii=ascii+7;
        }*/
        newrep[ndigit] = digits[r];
    /*    newrep[ndigit]=(char)ascii; */
        q=q/newbase;
        }
        printf("Base %d representation of %d is\n",newbase,n);
        i = ndigit;
        printf("\ni = %d\n", i);
        while (i>=1)
        {
            printf("%c",newrep[i]);
            i--;
        }              
        system("pause");
        return 0;       
    }

    Quote Originally Posted by dpitz View Post
    I think you didn't check your while loop after seeing the printed value of i ^^.
    If you choose to convert 10 to base 2 (1010), you'll get i=4, since 1010 has 4 digits. What we're trying to say is that your while loop is
    Code:
    while(i < 1) { ... }
    , what doesn't make sense, since i isn't smaller than 1.

    I just would like to say that the fact that he hasn't found the error does not mean he didn't try or that he just copy-pasted the code. Some errors are so obvious that we don't see them.

    Hope it's clear now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 17
    Last Post: 10-23-2008, 09:10 PM
  2. How do I print my output?
    By Hexadakota in forum C++ Programming
    Replies: 1
    Last Post: 01-22-2008, 04:29 PM
  3. print output problem
    By chor in forum C Programming
    Replies: 1
    Last Post: 12-29-2002, 09:45 AM
  4. Link list problem->can't print the output...
    By frankiepoon in forum C Programming
    Replies: 1
    Last Post: 11-09-2002, 12:01 AM

Tags for this Thread