Thread: Problem converting from char array to int array.

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

    Problem converting from char array to int array.

    I'm using the following code to do so:

    Code:
    #include <stdio.h>
    
    int main ()
    {
    int i;
    char ch[20]="1 2 34 4 56672332432";
    int ch1[20];
    for (i=0; i<20; i++)
    {
          ch1[i]=(int)ch[i]-'0';
    }
    for (i=0; i<20; i++)
    {
          printf ("%d\n ", ch1[i]);
    }
    return 0;
    }
    The problem is I can't an integer more than 9 as it is. For example, the array is reading and converting each char separately, so for example if I want to get 34 form char array into an integer 34, it won't do so. It picks and gives 3 separately and 4 separately.

    What should I do for this problem?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Use sscanf instead.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    36
    Ok, instead of the following code:

    Code:
    for (i=0; i<20; i++)
    {
          ch1[i]=(int)ch[i]-'0';
    }
    I tried the following:

    Code:
    for (i=0; i<20; i++)
    {
    sscanf("%s", ch1);
    }
    and also:

    Code:
    for (i=0; i<20; i++)
    {
    ch1[i] = sscanf("%s", ch);
    }
    But neither works. What would be the proper code?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Let's clarify what you are trying to do. Are you trying to convert the string "1 2 34 4 56672332432" into a sequence of 5 integers, 1, 2, 34, 4 and 56672332432?
    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
    Aug 2009
    Posts
    36
    String would be: ""1 2 34 4 56"

    I want to read this as an int array of size 5.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ah. Okay, one approach is to use a stringstream. The idea is to treat the string as if it were the contents of a file. For example:
    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    
    int main()
    {
        using namespace std;
        string str = "1 2 34 4 56";
    
        stringstream ss(str);
        int numbers[5];
        for (int i = 0; i < 5; ++i)
        {
            ss >> numbers[i];
        }
    
        for (int i = 0; i < 5; ++i)
        {
            cout << numbers[i] << '\n';
        }
    }
    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
    Join Date
    Aug 2009
    Posts
    36
    This is C++. I'm doing in C.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Right. This thread has been moved to the C programming forum.
    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
    oh sorry, I didn't know I was in C++ forum! :s

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by TheUmer View Post
    Ok, instead of the following code:

    Code:
    for (i=0; i<20; i++)
    {
          ch1[i]=(int)ch[i]-'0';
    }
    I tried the following:

    Code:
    for (i=0; i<20; i++)
    {
    sscanf("%s", ch1);
    }
    and also:

    Code:
    for (i=0; i<20; i++)
    {
    ch1[i] = sscanf("%s", ch);
    }
    But neither works. What would be the proper code?
    Have you used any scan functions before? Have you even bothered to look up sscanf anywhere at all?

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    #include <stdio.h>
    
    int main ()
    {
        int count = 0;
        int i;
        char ch[20]="1 2 34 4 56";
        int num[20];
        int poz;
        const char* str = ch;
        while(sscanf(str,"%d%n",&num[count], &poz) == 1)
        {
            count++;
            str += poz;
            if(count == 20) break;
        }
        for (i=0; i<count; i++)
        {
            printf ("%d\n", num[i]);
        }
        return 0;
    }
    Last edited by vart; 03-26-2010 at 11:42 AM.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #12
    Registered User
    Join Date
    Aug 2009
    Posts
    36
    @vart:

    Thanks a lot. That really worked. I'm highly obliged. Now I can carry on with my assignment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM