Thread: string problem, someone please!!

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    71

    string problem, someone please!!

    firstly i want to thank for someone who reply me

    my problem is

    Code:
    // Join solutions together as string, and brek up after two numbers.
        
        stringstream numbers;
    
        // Insert integer into stringstream
        numbers <<numLowerCaseLetter(name)<<sumAscii(name)<<lastValue<<counter<<sum;
    
        // Convert stringstream into string
        string str = numbers.str();
        
        for(int i = 0; str[i]; i=i+2)
        {
            cout<<str.substr(i,2)<<" ";   
        }

    EXPLANATION OF MY PROBLEM::

    output is: 55 20 11 84 18

    for the code above, it is to split strings after 2 numbers.
    when i enter str[1] it displays only 5. how can i do to make it display 55 when i asked for str[0]?

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    how can i do to make it display 55 when i asked for str[0]?
    You can't. str[0] is a char type, which means it only stores one character.

    output is: 55 20 11 84 18

    for the code above, it is to split strings after 2 numbers.
    It appears to have done a pretty good job!

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >for the code above, it is to split strings after 2 numbers.
    Your code should work, except:
    Code:
        for(int i = 0; str[i]; i=i+2)
        {
            cout<<str.substr(i,2)<<" ";   
        }
    Instead this should be:
    Code:
        for(int i = 0; i<str.length(); i=i+2)
        {
            cout<<str.substr(i,2)<<" ";   
        }
        cout << endl;
    Or maybe:
    Code:
        for(int i = 0; i+1<str.length(); i=i+2)
        {
            cout<<str.substr(i,2)<<" ";   
        }
        cout << endl;

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    71
    thanks guyz but tat is not wat my problem is .. thnx all of you ..

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    #include <iostream>
    #include <sstream>
    #include <string>
    using namespace std;
    
    int main(void) 
    {
       stringstream text("55 20 11 84 18");
       string before, after;
       for (int i = 0; i < 2; ++i)
       {
          text >> after;
          before += after + ' ';
       }
       getline(text, after);
       cout << "before: " << before << '\n';
       cout << "after:  " << after  << '\n';
       return 0;
    }
    
    /* my output
    before: 55 20 
    after:  11 84 18
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  4. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  5. Replies: 4
    Last Post: 03-03-2006, 02:11 AM