Thread: Splitting up a char array?

  1. #1
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80

    Splitting up a char array?

    I need to split up an input, into 5 different int variables. the format is like so: aabbcc-ddde. I need to take 2, 2, 2, 3 and 1 of the numbers, and assign them to int variables. But how do I do that?

    Code:
    int day, month, year, ctrlnr, gender;
    char input[12];
    
      cout << "Indtast venligst cprnr. i formatet ddmmċċ-cccc \n";
      cin  >> input;
      day = ?
      month = ?`
      etc.

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Here is a tip: You know how many characters each number will have. So start with splitting up that string to many small ones.

  3. #3
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    But how will that help me move them into int variables? I could just split them up into lesser strings, but they'd still be strings, not values.

    Hmmm, can I maybe assign the whole string to an int variable after I split it up? I'll try.
    Last edited by stillwell; 10-15-2004 at 06:38 AM.

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    true, you have a couple of options, either look up atoi or look up stringstream (search the board in both cases).

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    this input needs to be taken in as a char array?
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    No, I need it to devide the string into int values. 2 first numbers needs to be the day value, two next the month value and so on.

    Code:
    int i=0, l=0;
    char input[12], day[3], month[3], year[3], ctrlnr[4], gender[2];
    
      cout << "Indtast venligst cprnr. i formatet xxxxxx-xxxx \n";
      cin  >> input;
    
      while(i<2)
      {
      day[l] = input[i];
      i++;
      l++;
      }
      l=0;
      while(i<4)
      {
      month[l] = input[i];
      i++;
      l++;
      }
      l=0;
      while(i<6)
      {
      year[l] = input[i];
      i++;
      l++;
      }
      cout << day << month << year << "-";
    This is what I made, but it doesn't work, and I still can't figure out how to make the char arrays into int variables.
    Last edited by stillwell; 10-15-2004 at 06:57 AM.

  7. #7
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    you must have misunderstood... I was asking if you could instead just take in integers directly instead of dealing with a character array...

    for example:

    Code:
    ...
    int day,month,year;
    cin>>day>>month>>year;  //make yours more user-friendly by providing direction
    cout<<day<<'/'<<month<<'/'<<year;
    ...
    Last edited by major_small; 10-15-2004 at 07:32 AM. Reason: fixt end-user
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  8. #8
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    Ahh.

    Can I just do that? I thought it needed a pish on the enter key to go to the next input.

    Thanks, I'll try it out.

  9. #9
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    it does... you want everything on one line with just one press of the enter key?

    in that case you were moving in the right direction the first time... you may want to invest some time in strncpy
    Last edited by major_small; 10-15-2004 at 07:38 AM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  10. #10
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    Yea, I found out.

    Any idea how to do it the other way?

  11. #11
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    you could try somethign like this:

    Code:
    ...
    void clip(char*,int);   //prototype for a function to come later
    ...
    int main()  //start the driver function after the includes and prototype
    ...
    char*Data,cDay,cMonth,cYear;  //declare char arrays for each
    
    Data=new char[20];   //allocate memory
    cDay=new char[5];     //allocate memory
    cMonth=new char[5]; //more of the same
    cYear=new char[5];    //one more time
    
    int Day,Month,Year;    //create integers (allocate memory ;) )
    ...
    strncpy(data,cDay,2); //copy the first two characters into cDay
    Day=atoi(cDay); //turn cDay into an integer and store in Day
    clip(Data,2);  //trim off those first two characters
    ...
    }
    
    void clip(char*Data,int index)  //custom function
    {
        int Length=strlen(Data)-index;  //the new length is going to be the current length minus what you take off
    
        for(int i=0;i<index;i++)  //go through the inner loop for as many chars as your taking off
        {
            for(int x=0;x<Length;x++)  //go through the entire string
                Data[x]=Data[x+1];  //move each character up one space
        }
    
        Data[Length]='\0';  //make sure there's a null-termination where you want it
    }
    IANAL (heh), and I have no clue if that will work... there's also probably a standard (or at least widely accpted) function to take the place of my 'clip' function, but I can't seem to remember one for now...

    your best bet is to probably look into the string class because that would make all this alot easier and get you away from the C-style strings you've been using...
    Last edited by major_small; 10-15-2004 at 07:58 AM. Reason: comment your code!
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  12. #12
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    Yea, maybe it's my best bet to try something with string instead. What you showed me there, was a little too advanced, even thou you tried your best to explain it

  13. #13
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>Data=new char[20]; //allocate memory
    There is absolutely no reason to dynamically allocate memory in this situation, and you're also not delete[]'ing it at any point. Also, you don't need a clip() function at all:

    Code:
    char data[20];
    char a[3];
    char b[3];
    char c[3];
    char d[4];
    char e[2];
     
    char* iterator = data;
     
    strncpy(iterator, a, 2);
    iterator += 2;
     
    strncpy(iterator, b, 2);
    iterator += 2;
     
    strncpy(iterator, c, 2);
    iterator += 3; //because there's a '-' between cc and ddd
     
    strncpy(iterator, d, 3);
    iterator += 3;
     
    strncpy(iterator, e, 1);
    Now it's all split up into different strings, assuming I have the parameters for strncpy correct. The trick is, you set 'iterator' to the start of the string, copy 2 characters from that position, move 'iterator' 2 characters to the right, copy 2 characters, move it 2 to the right, etc... You'll find that pointers are very nice for this kind of thing, and you really have no need for a C++ string here (and in fact it might be harder to do it with one). Then just use atoi() or a std::stringstream to convert the broken up strings into integers.

    Hope this helps!

    **P.s.
    major_small, I believe you're a step-2 pointer user
    http://cboard.cprogramming.com/showt...eps#post393652
    Last edited by Hunter2; 10-15-2004 at 08:45 AM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  14. #14
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    Thanks alot.

    I don't really get pointers yet, so could you explain something?

    Code:
    char* iterator = data;
    Why do I need the pointer? Why can't I just declare it like this?

    Code:
    char iterator = data;

  15. #15
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    data was declared as an array of char, not as a single char. therefore, trying to assign data to a single char won't work. On the other hand, the name of an array acts like a pointer to the first element of the array, so the name of an array can be assigned to a pointer of the same type as the array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  3. code help required
    By Yobbo in forum C Programming
    Replies: 9
    Last Post: 09-02-2005, 11:15 PM
  4. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 11:44 AM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM