Dear All,


I have written a code to implement an algorithm for base conversion from decimal to any base between 2 and 36 given in RG Dromey. Below is my code and I have a few questions relating to it:

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int newbase,zero=atoi(0),q,ndigit=0,r,ascii,i;
    char newrep[100];
    printf("Enter number and base(between 0 and 36) to be converted");
    scanf("%d%d",&q,&newbase);
    while(q!=0)
    {
    r=q%newbase;
    ndigit+=1;
    ascii=zero+r;
    if(ascii>atoi(9))
    {
                      ascii=ascii+7;
    }
    newrep[ndigit]=(char)ascii;
    q=q/newbase;
    }
    for(i=ndigit;i<=1;i++)
    {
    printf("%d",newrep[ndigit]);
    }
    system("pause");
    return 0;      
}

1. Why is there a need for reverse conversion to characters of the ascii value after conversion before storing them in the array newrep.
2. My program hangs when I input the values of q and newbase on dev C platform. Please help!

Recommended Answers

All 6 Replies

1) Are you putting a space between the number and the base?
-- if not, how does the program know where the base starts? << problem

2) Why is there an "atoi()" on line 5?
-- can you just init that variable to 0?
-- please initialize all variables

3) Please look up the documentation on "atoi()".
-- It looks like you are trying to convert something that is already a number to a number

Thanks for your reply. I have edited the code as per your suggestions:

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int newbase=2,zero=0,q=0,ndigit=0,r,ascii=0,i=0;
    char newrep[100];
    printf("Enter the number to be converted");
    scanf("%d",&q);
    printf("Enter the base between 0 and 36 in which the number is to be converted");
    scanf("%d",&newbase);
    zero=atoi(0);
    while(q!=0)
    {
    r=q%newbase;
    ndigit+=1;
    ascii=zero+r;
    if(ascii>atoi(9))
    {
                      ascii=ascii+7;
    }
    newrep[ndigit]=(char)ascii;
    q=q/newbase;
    }
    for(i=ndigit;i<=1;i++)
    {
    printf("%d",newrep[ndigit]);
    }
    system("pause");
    return 0;       
}

No more errors, but the code still hangs after I execute the program on Dev C. Please suggest some fixes. Thank you.

1) Are you putting a space between the number and the base?
-- if not, how does the program know where the base starts? << problem

2) Why is there an "atoi()" on line 5?
-- can you just init that variable to 0?
-- please initialize all variables

3) Please look up the documentation on "atoi()".
-- It looks like you are trying to convert something that is already a number to a number

atoi is used to convert a string to an integer
Quick question: why use atoi() on an integer?

For the first time, Atoi is used to convert the value of character into an integer. For the second time, it is used in the if loop to make sure that the Ascii values are correctly chosen for proper representation of the resulting integer values.

atoi is used to convert a string to an integer
Quick question: why use atoi() on an integer?

For the first time, Atoi is used to convert the value of character into an integer.

if it's the one below then i'm sorry to inform you but zero(0) is not a character but an integer

zero=atoi(0);

1. this is what causes the program to hang in the first place
2. why use this at all when you already initialized the variable's value to zero
3. plus your trying to convert somethings that's already a number

if(ascii>atoi(9))

this should have an error about expected 'const char *' but found 'int'.

like what thines01 said

-- It looks like you are trying to convert something that is already a number to a number

Here's a sample reference of atoi
http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/

it's parameter looks like this
int atoi ( const char * str ); where str is a C string beginning with the representation of an integral number.

abhishekagrawal, as I suggested before, look up atoi() in the documentation.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.