Thread: Conversion from char to int?

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    36

    Conversion from char to int?

    What could be wrong with this code?:

    Code:
    int arr[10];
    char arr2[10];
    for (int i=0; i<10; i++)
    {
            arr[i]=(int)arr2[i];
    }
    When I output the supposedly converted array, it's outputs some crazy numbers.
    ???

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    For one thing, you have not initialised arr2.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    36
    any random characters including spaces.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Okay, so what is so crazy about the numbers? Don't you get the (presumably) ASCII values as you intended?

    You might want to post the smallest and simplest compilable program that demonstrates the problem. Post your sample input, expected output, and actual output.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2008
    Location
    INDIA
    Posts
    64
    Try this , you will get to know the problem.

    Code:
    #include<stdio.h>
    #include<string.h>
    main()
    {
    int arr[10];
    char arr2[10]="abcdefghi"; // Initialize the array
    int i;
    for (i=0; i<10; i++)
    {
            arr[i]=(int)arr2[i]; // Converting to int .(ASCII value)
    }
    
    for (i=0; i<10; i++)
    {
            printf("===> %d\n",arr[i]);
    }
    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by karthigayan
    Try this , you will get to know the problem.
    Sorry, but your program does not explain the problem. On the other hand, you are not TheUmer
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    arr2 is only initialised with 9 values in the last example by the way

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by rogster001
    arr2 is only initialised with 9 values in the last example by the way
    No, it is correctly initialised with 10 values, including the null character.

    Personally, I thought that this might be a "convert digit to integer" problem, but the "any random characters including spaces" comment makes me think otherwise.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Aug 2009
    Posts
    36
    @karthigayan:

    I ran your code and it gives the 'segmentation fault' error.

    @laserlight:

    Input string is: [p 1 (a tab) 0]

    And it outputs: 80 49 9 48 0 0 0 0 0 0

    I want to get 1 and 0 as integers.
    ?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by TheUmer
    Input string is: [p 1 (a tab) 0]

    And it outputs: 80 49 9 48 0 0 0 0 0 0

    I want to get 1 and 0 as integers.
    ?
    Ah. In that case, you do want to convert a digit to an integer. Since you want to leave those characters that are not digits untouched, it implies that you need some logic in the loop, e.g., check if arr2[i] >= '0' && arr2[i] <= '9'. If it is, then you subtract '0' from arr2[i] to get the integer value. Otherwise, you just assign the value as per normal.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Aug 2009
    Posts
    36
    Alright. The problem is solved to some extend. But a little more problem:

    For the given string [P 1 (tab) 0] it outputs fine: 1 0
    But when I have a string [P 2 (tab) 34] it outputs: 2 3 (no four.)

    What could be the problem?

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by TheUmer View Post
    Alright. The problem is solved to some extend. But a little more problem:

    For the given string [P 1 (tab) 0] it outputs fine: 1 0
    But when I have a string [P 2 (tab) 34] it outputs: 2 3 (no four.)

    What could be the problem?
    You only read in a single character, which is 3.

  13. #13
    Registered User
    Join Date
    Aug 2009
    Posts
    36
    I wonder why you never reply with complete answer? How can I solve this problem?

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your current code?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Aug 2009
    Posts
    36
    Code:
    int arr[10];
    char arr2[10];
    for (int i=0; i<10; i++)
    {
          if (arr2[i]>=0 && arr2[i]<=9)
          {
                arr[i]=(int)arr2[i]-48;
          }
    }
    for a single 34, it takes 3 but not 4.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 11-30-2007, 03:51 AM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM