Thread: convert

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    4

    convert

    Hi I have a problem.
    This program is something like calculator just x + y. But i dont know how to convert number in int array to char from decimal. I know it can be easy but I must do it this way.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    void reada(int *a);
    int number(int *a);
    
    int length=0;
    int m=0;
    
    void reada(int *a)
    
    {
        int b;
        while((b=getchar()) != '\n' && b != EOF && length != 40)
        {
            a[length]=b;
            length++;
            if (length == 40)  printf("end :D \n");
        }
    
    }
    
    int number(int *a)
    {
        int b=0;
    
        while(isdigit(a[m]) && m <= length)
        {
        b = (b*10) + a[m];
        m++;
        }
        return b;
    }
    int main(void)
    {
    int a[42]={0};
    int *b;
    int x;
    int y;
     
    b=a;    
    reada(b);
    x = number(b);
    m++;
    y = number(b);
    
    printf("result is %d \n",x+y);
    
    return 0;
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well I see the problem.
    Code:
    2
    result is 50
    You have to remember that the numerical character constants do not correspond to their numerical values. See Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion for example. So you have to do math, take away the value '0', to store the right values.

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    4
    I see but I dont know how to do it in c language. I know ASCII table. The proglem is here : b = (b*10) + a[m]; because a[m] is not number it is char of number for example (chr 5 : dec 53). I need convert 53 to 5.

    INPUT:
    123+234
    OUTPUT:
    11013

    It is not corret.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Do as whiteflags has told you
    insted of a[m] use a[m]-'0'
    Kurt

  5. #5
    Registered User
    Join Date
    Aug 2012
    Posts
    4
    Thanks

  6. #6
    Registered User
    Join Date
    Aug 2012
    Posts
    4
    And one more think.
    What is opposite operation?

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by rootkit View Post
    And one more think.
    What is opposite operation?
    You mean how to convert to text again. Just add '0' back to the digit.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 17
    Last Post: 07-26-2012, 09:32 PM
  2. Convert c to c++
    By AlexWu in forum C++ Programming
    Replies: 17
    Last Post: 05-14-2010, 03:59 AM
  3. convert from c to cpp
    By shaolin in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2009, 01:31 PM
  4. Convert dms to dd
    By JacquesLeJock in forum C Programming
    Replies: 13
    Last Post: 06-06-2007, 01:44 PM
  5. cannot convert
    By opafire in forum C++ Programming
    Replies: 3
    Last Post: 12-10-2003, 08:41 AM