string operations

This is a discussion on string operations within the C++ Programming forums, part of the General Programming Boards category; Bades,I want to parse through the string .The below code is compliing but i am not getting the desired output. ...

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

    string operations

    Bades,I want to parse through the string .The below code is compliing but i am not getting the desired output.

    [code
    main()
    {
    string str;

    cin>>str;

    int size=str.size();

    for (int i=0; i<=size ;i++ )
    {
    cout<<str[i];
    }


    }]

    can u please tell what is worng here..

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    what output are you expecting? Since you did not put a space between characters they will all be scrunched up together so that it looks like out output the string instead of individual characters. try this
    Code:
    cout<<str[i] << " ";

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    206
    Specify what the undesired result is.

    If it cuts off at spaces, use cin.getline();

  4. #4
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    Can't say for sure since you don't note what kind of error you're getting, but I'd guess you're entering a space in the input intended for str. cin only grabs from the input stream until the next whitespace, so if you enter:

    why is this not working

    to the program, str will only be assigned "why".

    Also, make sure you're using code tags properly. That's what the edit button is for.

    [code]
    enter your code here
    [/code]

    Will end up looking like this:
    Code:
    enter your code here
    (Thanks quzah)
    Last edited by Decrypt; 11-02-2005 at 07:27 PM.
    There is a difference between tedious and difficult.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    The only thing that is "wrong" with your code is that you access a character passed the string's bounds. Using [] with an index greater than or equal to size() is undefined.

    You should use i < size instead of i <= size.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Just to explain a little further what Daved is saying. If you have an string of length 3,

    string str = "yes";

    you can treat it as an array. But, arrays start at index position 0. So, the 'y' is at index position 0, the 'e' is at index position 1, and the 's' is at index position 2. Note that index position 3 doesn't exist. The general rule is that index values go up to one less than the size of the array.

    You also might want to consider proofreading. Is it possible you didn't notice that your code tags failed to work?
    Last edited by 7stud; 11-02-2005 at 03:59 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. standard String operations
    By kolucoms6 in forum C++ Programming
    Replies: 3
    Last Post: 03-09-2008, 12:47 PM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21