Exploding a string in c++, need help.

Discussion in 'Programming' started by Barti1987, Jun 19, 2008.

  1. #1
    I am trying to explode a string in c++ using string operations:

    
    // read in user input
    std::cout << "Please enter operation:\n";
    std::cin >> operation;
         
    while(operation.empty()){
         string::size_type loc = operation.find( " ", 0);
         cout << "Found space at " << loc << endl;
         operation.erase(operation.at(loc).size());
    }
    
    Code (markup):

    I have looked many places, and size should be an operation for strings, however, I keep getting this error:

    error C2039: 'size' : is not a member of 'System::SByte'

    I also tried length (which should be the same thing), similar error.

    Thanks for any help,

    Peace,
     
    Barti1987, Jun 19, 2008 IP
  2. Vallikkannu

    Vallikkannu Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I think u can try it as len instead of length...if any error PM me
     
    Vallikkannu, Jun 19, 2008 IP
  3. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #3
    Check this out please, I hope this is exactly what you need

    http://www.winapizone.net/tutorials/winapi/functions/explode.php

    regards
     
    Vooler, Jun 20, 2008 IP
  4. it career

    it career Notable Member

    Messages:
    3,562
    Likes Received:
    155
    Best Answers:
    0
    Trophy Points:
    270
    #4
    I guess you need to use substr api .
     
    it career, Jun 20, 2008 IP
  5. brian65

    brian65 Active Member

    Messages:
    1,172
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    88
    #5
    Do you mean collapse instead of explode? erase will remove characters. If you do mean collapse, try:
    string::size_type loc = operation.find( " ", 0);
    while(loc != -1){
    cout << "Found space at " << loc << endl;
    operation.erase(loc,1);
    loc = operation.find( " ", 0);
    }
     
    brian65, Jun 20, 2008 IP