Thread: string find

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    35

    string find

    heyas,

    if anyone could enlighten me as to why this is not acting as intended i'd be greatly apprecitive

    it is *meant* to store the following

    vector index 0: assume
    vector index 1: nothing
    vector index 2: expect
    vector index 3: anything

    what it is _actually_ storing is the following:

    vector index 0: assume
    vector index 1: nothing expect
    vector index 2: expect anything
    vector index 3: anything

    also, i recieve different still results from using cin>>paramCommand;

    any ideas why using cin would give different results to my static string in the example below?

    i'm very keen to get on the top of both of these issues, thanks in advance.

    Code:
    int index = 0;
    int loc = 0;
    
    /*keep iterating through string until the end
    using spaces as delimiter's */
    
    paramCommand = "assume nothing expect anything";
    
    while(loc <= paramCommand.length()) {
    loc = paramCommand.find(" ", index);
    command.push_back(paramCommand.substr(index, loc));
    index = loc+1;
    }

  2. #2
    Registered User
    Join Date
    Jul 2005
    Posts
    25
    The second parameter to basic_string::substr() is the length of the substring, not the position of the last character. Perhaps you should use it like this?
    Code:
    command.push_back(paramCommand.substr(index, loc - index + 1));

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    35
    fantastic, thanks for the help

    any ideas when i call

    cin >> paramCommand;

    it does not function?

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    A stringstream would be easier to use (IMO) in this instance:

    Code:
    #include <vector>
    #include <string>
    #include <sstream>
    
    ...
    
    std::vector<std::string> command;
    std::string paramCommand = "assume nothing expect anything";
    std::string temp;
    
    // Initialize stringstream using 'paramCommand' string
    std::stringstream sstr(paramCommand);
    
    // Extract individual strings from stringstream (uses spaces by default) and push onto vector
    while(sstr >> temp)
        command.push_back(temp);
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by twans
    fantastic, thanks for the help

    any ideas when i call

    cin >> paramCommand;

    it does not function?
    cin >> paramCommand; stops when it finds a space. use getline instead.
    Kurt

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    35

    thanks!

    thanks heaps guys.

    very helpful

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. find a string based on the location of another string
    By rlilley in forum C Programming
    Replies: 3
    Last Post: 02-19-2009, 12:29 PM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  4. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM