Thread: Conversion problem not working properly

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

    Conversion problem not working properly

    Hi All,

    I have written a program to input a decimal string and convert it to character string. Below is my program:

    Code:
    /*this program converts the decimal representation of an input string to character representation*/
    #include<math.h>
    #include<stdio.h>
    #include<stdlib.h>
    char int2str(int n);
    int main()
    {
    int dec[100]={0},i,j,decindex,ndec=0,nchar;
    char character[100]={'\0'};
    printf("Enter the input decimal sequence length\n");
    scanf("%d",&ndec);
    printf("Enter the input decimal sequence\n");
    for(i=0;i<=nchar;i++)
    {
         scanf("%d",&j);
         dec[i]=j;
    }
    /*This loop converts the decimal to character*/
    for(i=0;i<=nchar;i++)
    {    
         if(dec[i]!='.')
         {
         character[i]=int2str(dec[i]);
         }
    }
    printf("The output sequence is");
    for(i=0;i<=nchar;i++)
    {
         printf("%c",character[i]);
    }
    system("pause");
    return 0;
    }
    
    /*this loop converts the decimal to character*/
    char int2str(int n)
    {
    char c;
    c=n+48;
    return c;
    }
    On running the program, It doesn't proceed after entering the input decimal sequence. Kindly point out bugs/errors and suggest fixes. Thank you.

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    You are assigning value to ndec but using uninitialized value of nchar.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    Thank you for your reply. The program still stops at the same point on changing nchar to ndec.

    Quote Originally Posted by DRK View Post
    You are assigning value to ndec but using uninitialized value of nchar.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > The program still stops at the same point on changing nchar to ndec.
    So what does your new indented code look like?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    @Salem: Now my code works, but is producing the wrong output.
    For example if I enter the input sequence length as 4 and input 1,9,.,8 one by one, my code gives me output as 1999 whereas It should give 19.8 in character representation.

    Below is my indented code:

    Code:
    /*this program converts the input decimal representation to character string representation*/
    #include<math.h>
    #include<stdio.h>
    #include<stdlib.h>
    char int2str(int n);
    int main()
    {
    int dec[100]={0},i,ndec=0,no;
    /*dec[100] contains the input decimal sequence, i is used for indexing,ndec for storing the number of digits in
    the input decimal number*/
    char character[100]={'\0'};
    printf("Enter the input decimal sequence length\n");
    scanf("%d",&ndec);
    printf("Enter the input decimal sequence\n");
    /*This loop inputs the decimal sequence*/
                for(i=0;i<ndec;i++)
                       {
                              scanf("%d",&no);
                              dec[i]=no;
                       }
    /*This loop converts the decimal to character*/
                for(i=0;i<ndec;i++)
                       {  
                          if(dec[i]!='.')
                          {  
                              character[i]=int2str(dec[i]);
                          }
                          else
                          {
                              character[i]=dec[i];
                          }   
                       }
    /*This loop outputs the sequence*/
    printf("The output sequence is");
                for(i=0;i<ndec;i++)
                       {
                       printf("%c",character[i]);
                       }
    system("pause");
    return 0;
    }
    /*This function converts integer to character representation and returns it to the main function*/
    char int2str(int n)
         {
                       char c;
                       c=n+48;
                       return c;
         }
    Please point out the errors. Thank you.

    Quote Originally Posted by Salem View Post
    > The program still stops at the same point on changing nchar to ndec.
    So what does your new indented code look like?

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    I'm not sure what this is even supposed to do.

    You are using `scanf' (a formatted input function) so one can safely assume the input is already in a character representation.

    The function `int2str' does not do what it says on the label; at best it is `intplus48convertedtochar'. You shouldn't use "magic numbers" and because this is unfiltered input shouldn't be making assumption about the value of `n'.

    Soma

  7. #7
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    The question is:

    Write a program to convert a decimal representation for a number to the corresponding character string representation.

    I am using scanf %d representation to input decimal number and store it in dec[i] array.

    Since the input number is in decimal representation, the character equivalent will be int+48. For example, if 2 is entered, 2+48=50 is 2 in char, that is what I intend to achieve using the 'int2str' function.

    As far as filtering of input decimal number is concerned, the user only enters values between 0-9. However, If a user enters a '.' point, then it is filtered using the following part of the program:

    Code:
    if(dec[i]!='.')                      {  
                              character[i]=int2str(dec[i]);
                          }
                          else
                          {
                              character[i]=dec[i];
                          }


    Quote Originally Posted by phantomotap View Post
    O_o

    I'm not sure what this is even supposed to do.

    You are using `scanf' (a formatted input function) so one can safely assume the input is already in a character representation.

    The function `int2str' does not do what it says on the label; at best it is `intplus48convertedtochar'. You shouldn't use "magic numbers" and because this is unfiltered input shouldn't be making assumption about the value of `n'.

    Soma

  8. #8
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    2+48=50 is 2 in char
    No.

    2+48=50 is '2' in ASCII
    That's kind of an important difference.

    the user only enters values between 0-9
    My point was that you've done nothing to ensure that.

    O_o

    So... your task is to take input in the form of:

    Code:
    12 1 2 3 4 5 6 7 8 9 8 7 6
    and turn it into:

    Code:
    "123456789876"
    ?

    That seems especially pointless even for early example work.

    In any event, you've said that '.' is valid input; you've also said that the user enters only integer values between zero and nine.

    These things do not get along. I'll point you to the documentation for `scanf' and "%d" format string.

    Soma

  9. #9
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    No.

    That's kind of an important difference.
    Thank you for your detailed reply.

    I am sorry for that obvious error, I meant if a user enters an ASCII decimal input he gets an ASCII character output.

    So... your task is to take input in the form of:
    Code:
    12 1 2 3 4 5 6 7 8 9 8 7 6

    and turn it into:
    Code:
    "123456789876"
    In essence, user has the choice to enter 12.43 in the sequence 1 2 . 4 3 (number of input digits ndec=5) individually or 12 . 43 (number of input digits ndec=3), either way my program should give 12.43 in ASCII character representation, I think that is what the question wants me to do.

    In any event, you've said that '.' is valid input; you've also said that the user enters only integer values between zero and nine.
    The user enters value between 0-9 including a decimal point '.'

    I have changed my program to accommodate '.'

    These things do not get along. I'll point you to the documentation for `scanf' and "%d" format string.
    I don't understand how a decimal point '.' is represented, ASCII table doesn't have a decimal equivalent for '.'

    Only the character equivalent of decimal 46 represents a '.' in ASCII
    Last edited by abhishekcoder; 04-17-2012 at 08:29 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Not Working properly
    By Tejas Sanap in forum C Programming
    Replies: 7
    Last Post: 05-10-2011, 06:52 AM
  2. Fscanf not working properly
    By thefinalhero in forum C Programming
    Replies: 9
    Last Post: 10-05-2009, 10:06 AM
  3. Calendar not working properly?
    By kawk in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-19-2007, 01:15 PM
  4. tic tac toe not working properly
    By h_howee in forum Game Programming
    Replies: 2
    Last Post: 01-01-2006, 01:59 AM
  5. HWND not working properly
    By loopshot in forum Windows Programming
    Replies: 1
    Last Post: 11-15-2005, 10:25 AM

Tags for this Thread