Thread: Splitting up a char array?

  1. #16
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Yes, char* and char array are very often interchangeable. In fact, if you take a look at major_small's code above, he uses char*'s wherever I use a char array instead (with very minor changes)
    Just Google It. √

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

  2. #17
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    >> you really have no need for a C++ string here (and in fact it might be harder to do it with one)
    Decide for yourself: substr() http://www.cppreference.com/cppstrin...ls.html#substr
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  3. #18
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Yes, I know about substr() and c_str(), and they will work fine. But I personally like the C-style more than the C++ style in this case, if only because the C++ style requires an integer counter rather than directly pointing to the next block of memory... besides which, atoi() takes a c-style string (and yes I know about c_str() and stringstreams).
    Code:
    char* iterator = data;
    
    strncpy(iterator, a, 2);
    iterator += 2;
    ...
    nA = atoi(a);
    Code:
    int pos = 0;
    
    a = data.substr(pos, 2);
    pos += 2;
    ...
    nA = atoi(a.c_str());
    Just Google It. √

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

  4. #19
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    Ok, now I got how to divide them into different strings, but can anyone tell me how to convert them into intergers?

  5. #20
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    look up atoi and/or stringstream.

  6. #21
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Someone already gave you the answer actually. Re-read the previous posts again, also in the cprogramming FAQ.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  7. #22
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    Yea, so I see. atoi is really simple, and does exactly what I want

    Once again, thanks guys.

  8. #23
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    Sorry to ask for help so soon. I have tested the atoi translations, and they seem to work fine, yet all the whole program outputs is 000-00.

    Code:
    #include <vcl.h>
    #include <iostream>
    #include <stdlib>
    #pragma hdrstop
    using namespace std;
    
    //---------------------------------------------------------------------------
    
    #pragma argsused
    
    int main()
    {
    
    int Iday, Imonth, Iyear, Ictrlnr, Igen, test;
    
    char input[20], day[3], month[3], year[3], ctrlnr[4], gen[2];
    
    char* i = input;
    
      cout << "Indtast cprnr. i formatet xxxxxx-xxxx ";
      cin  >> input;
    
      strncpy(i, day, 2);
      i += 2;
    
      strncpy(i, month, 2);
      i += 2;
    
      strncpy(i, year, 2);
      i += 3;
    
      strncpy(i, ctrlnr, 3);
      i += 3;
    
      strncpy(i, gen, 1);
    
      Iday = atoi(day);
      Imonth = atoi(month);
      Iyear = atoi(year);
      Ictrlnr = atoi(ctrlnr);
      Igen = atoi(gen);
    
      cout << Iday << Imonth << Iyear << "-" << Ictrlnr << Igen;
    
      cin  >> test;
    
      return 0;
    }

  9. #24
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Actually it looks like the strncpy() lines are wrong. It should be
    strncpy(month, i, 2);
    etc.
    Just Google It. √

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

  10. #25
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    And then it works

    Thanks Hunter, you've been a great help.

  11. #26
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    No problem
    Just Google It. √

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

  12. #27
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    I'm in trouble again.

    I made the if statements, and tested them with cout's, but when I threw in the bool, it suddenly doesn't work. I have tried to look up bool, but I can't find anything that helps me out, and I can't see where it messes up.

    Code:
    bool valid = true;
    
    int Iday, Imonth, Iyear, Ictrlnr, Igen, test;
    
    char input[20], day[3], month[3], year[3], ctrlnr[4], gen[2];
    
    char* i = input;
    
      cout << "Indtast cprnr. i formatet xxxxxx-xxxx ";
      cin  >> input;
    
      strncpy(day, i, 2);
      i += 2;
    
      strncpy(month, i, 2);
      i += 2;
    
      strncpy(year, i, 2);
      i += 3;
    
      strncpy(ctrlnr, i, 3);
      i += 3;
    
      strncpy(gen, i, 1);
    
      Iday = atoi(day);
      Imonth = atoi(month);
      Iyear = atoi(year);
      Ictrlnr = atoi(ctrlnr);
      Igen = atoi(gen);
    
      cout << Iday << Imonth << Iyear << "-" << Ictrlnr << Igen;
    
      if(Imonth == 1 || Imonth == 3 || Imonth == 5 || Imonth == 7 || Imonth == 9 || Imonth == 11)
        {
        if(Iday > 31)
          valid = false;  //Here I used cout << " Invalid cprnr."
        }
      else if(Imonth == 4 || Imonth == 6 || Imonth == 8 || Imonth == 10 || Imonth == 12)
        {
        if(Iday > 30)
          valid = false;
        }
      else if(Imonth == 2)
        {
        if(Iyear%4 == 0)
          {
          if(Iday > 29)
            valid = false; //same
          }
        else if(!Iyear%4 == 0)
          {
          if(Iday > 28)
            valid = false; //and so on
          }
        }
      else if(Imonth > 12)
        valid = false;
    
      if(Iyear > 99)
        valid = false;
    
      if(Iday > 31)
        valid = false;
    
    
      if(valid == true) //added this for output.
        cout << endl << input <<" is a valid cprnr. ";
      else if(valid == false)
        cout << endl << input << " is not a valid cprnr. ";
    It checks if the number is valid within the conditions given in the if files, and it worked. Then I added the bool variable, and the last part, and now the numbers don't work right. If I for ex. input 230378-4581, the integers return 232378-4581, when it should be 23378-3035.
    Last edited by stillwell; 10-18-2004 at 11:40 AM.

  13. #28
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Code:
    else if(!Iyear%4 == 0) // Problem here, take away the ! use != instead
          {
          if(Iday > 28)
            valid = false; //and so on
          }
    Not sure if this is the cause though, but still.

  14. #29
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    Ok, nevermind. The bool just seems to mess everything up, so I used the int variable test instead, and it works as it's supposed to.
    Last edited by stillwell; 10-18-2004 at 12:21 PM.

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