Thread: dynamic strings

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

    dynamic strings

    In web programming langauges like php, you can put variable names in your string declaration like this:


    var location = "Spain"
    var myString = "The rain in $location is falling on the plain"

    echo $myString; // outputs "The rain in Spain is falling on the plain"


    I was wondering if there's something similar to this you can do with c++?


    Thanks,


    Ham

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I'm sure you could overload the << operator for your new string type to achieve that.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    14
    Please explain

  4. #4
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Code:
    #include <iostream> //basic io
    #include <string> //obvious
    
    using namespace std;
    
    int main()
    {
        string location = "Spain";
        string my_string = "The rain in "+ location +" is falling on the plain";
        
        cout<<my_string;
        
        cin.get();
        return 0;
    }

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    14
    Thanks, that helps.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    BTW, that will only work for strings. If you want to add other variable types to the string (like numbers), you should look up stringstreams.

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    14
    I got around the problem using printf instead because the following wouldn't work:

    Code:
    #include <iostream> //basic io
    #include <string> //obvious
    
    using namespace std;
    
    int main()
    {
        string location;
        string my_string = "The rain in "+ location +" is falling on the plain";
        location = "Spain"    
    
        cout<<my_string;  // The rain in       is falling on the plain
        
        cin.get();
        return 0;
    }

  8. #8
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    define locations first:

    Code:
    #include <iostream> //basic io
    #include <string> //obvious
    
    using namespace std;
    
    int main()
    {
        string location;
        location = "Spain"  
        string my_string = "The rain in "+ location +" is falling on the plain";
      
    
        cout<<my_string;  // The rain in       is falling on the plain
        
        cin.get();
        return 0;
    }
    My Website
    010000110010101100101011
    Add Color To Your Code!

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    IMO, using tools from a different language just because you made a small mistake in the current one doesn't make a whole lot of sense.

    Don't forget the semi-colon after you assign Spain to location as well.

  10. #10
    Registered User
    Join Date
    Sep 2001
    Posts
    14
    It's not a mistake; I have all my string declarations at the beginning of the program, and those variables (e.g. location) are not set until further down the program during run-time.

    I found a solution by using printfs instead:

    Code:
    char * my_string = "The rain in %s is falling on the plain";
    // more code here
    // more code here
    // more code here
    // more code here
    char * location = "Spain";
    printf (my_string, location);
    I have no idea why the C++ syntaxes can't get this to work the way I want.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I don't understand why you think it wasn't a mistake. You posted the code that doesn't work, and it doesn't work because it is wrong. The code is right there and mrafcho001 indicated why it didn't work. Your C version would have failed as well if you had made the same mistake there.

    At the time you used the location variable, it was empty, so that is why it didn't work.

    If your code is more complicated, and you are having trouble translating the example to work for you, feel free to post more of the actual code so we can help you fix it (assuming you want to try and get it to work in C++).

  12. #12
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I don't think that any language can do what you want.
    Code:
        string location;    // this is an empty string
        string my_string = "The rain in "+ location +" is falling on the plain";
        location = "Spain"; // now it gets a value
        
        cout<<my_string;    // now you expect mystring to mysteriously change to 
                            // include the new value of location

  13. #13
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    [sigh]

    People can't even copy and paste nowadays

    [/sigh]


  14. #14
    Registered User
    Join Date
    Dec 2005
    Posts
    8

    regex

    Hello all, I'm pretty new to c++ so correct me where I'm wrong. In vb.net I would use regular expressions to do it. I would make a function that you input the string and a collection of replacments possibly like a hashtable with the string to replace as the key and the value as the replacement string. you could then set the value of the string any where so long as you cout the result of the function instead of the original string. I don't know how to do regular expressions or hashtables in c++ yet but I would think that you could.

  15. #15
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    If you're totally new to c++ then it wouldn't hurt looking over the tutorials on this board...
    You're experience in VB will help.

    Another thing, you need to start a new thread if you wish to discuss another topic.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fwrite can't write dynamic strings???
    By Devil Panther in forum C Programming
    Replies: 26
    Last Post: 05-19-2009, 02:18 AM
  2. Strings and Dynamic Memory
    By grooveordie in forum C Programming
    Replies: 3
    Last Post: 10-20-2008, 02:08 PM
  3. beginner: dynamic array of strings
    By pc2-brazil in forum C++ Programming
    Replies: 10
    Last Post: 04-29-2008, 04:29 PM
  4. Concatenating strings (dynamic array using pointers)
    By Tankndozer in forum C Programming
    Replies: 8
    Last Post: 07-01-2004, 07:27 AM
  5. Inputing strings into a dynamic array
    By Nakeerb in forum C++ Programming
    Replies: 1
    Last Post: 10-12-2002, 02:38 AM