Simple coversion program hanging on input

This is a discussion on Simple coversion program hanging on input within the C Programming forums, part of the General Programming Boards category; Dear All, I have written a code to implement an algorithm for base conversion from decimal to any base between ...

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

    Simple coversion program hanging on input

    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:

    Code:
    #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!

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by abhishekcoder View Post
    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!
    1. it isn't unless you decide to store each individual digits in the array as ascii characters.

    2. All your atoi calls will cause segfaults, it expects a string not an integer. But the use of atoi for this is pointless since atoi("0") will return 0 so you can just assign 0 directly to 'zero', ditto for the rest where atoi is used. There are lot's of other problem too, you overwrite 'ascii' in each iteration of the loop for one. Edit: actually you don't but your indentation makes that hard to see.

    Some more comments:

    If you want to represent numbers above 9 with letters from a - z, you can assign the ascii value directly with a single quote around it, example: zero = '0'

    On line 12 you update your index before you use the array so, it will actually be indexed from 1 not from 0

    The last for loop does not make sense in any way, you want to print each character in the array, so start from your first index 0 (1 in your current case) and use ndigit as ending condition. In the loop you want to print characters so: %c should be used in the format string.
    Last edited by Subsonics; 03-10-2012 at 06:49 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple coversion program hanging on input
    By abhishekcoder in forum C Programming
    Replies: 7
    Last Post: 02-09-2012, 10:23 AM
  2. Program is hanging
    By mramazing in forum Networking/Device Communication
    Replies: 7
    Last Post: 03-18-2009, 05:13 PM
  3. Hang man program hanging
    By pieisgood in forum C++ Programming
    Replies: 14
    Last Post: 02-21-2009, 06:08 PM
  4. Can't figure out what keeps hanging up my program
    By shays in forum C Programming
    Replies: 7
    Last Post: 11-12-2007, 02:59 PM
  5. Code is hanging - Very simple C (Beginner)
    By pc_doctor in forum C Programming
    Replies: 10
    Last Post: 10-29-2007, 06:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21