Thread: Another Quick Question

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    4

    Another Quick Question

    if I create a string like

    string filepath;

    how can I change it's variable, blah, blah, blah. An example would be better. For example, for cout it would be

    cout<<"users/"<<Name<<".txt";

    how do I get the same thing into the variable of filepath??

  2. #2
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    what? Say that again, maybe with complete sentences this time.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Look into std::stringstream and its variants (std::ostringstream if you're only using the various writing functions, std::istringstream if you're only using the reading functions).
    Code:
    #include <sstream>
    
    ...
    
    std::ostringstream sstr;
    sstr << "users/"<<Name<<".txt";
    
    std::string filepath = sstr.str();
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Hi,

    You can do something like this as well:
    Code:
    string filename = "";
    string Name = "myfile";
    filename = "users/" + Name + ".txt";
    
    cout<< filename<<endl;
    That's called 'concatenating' strings, but you can also just call it "adding" strings together, and most people will understand what you mean.

    how can I change it's variable
    how do I get the same thing into the variable of filepath??
    A variable stores values. In this line:

    string Name = "myfile";

    Name is a variable, and it's value is "myfile". So, you would say, "how do I assign the variable Name a value". Or, "how can I change the variable's value?". Assignment is what you do when you use the equals sign(=).
    Last edited by 7stud; 02-16-2005 at 09:44 PM.

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Good point. If you're patching only strings together, then stringstream is a bit overkill. If you ever intend to mix in ints and other common datatypes though, that you might use with cout, then you'd probably like to use a stringstream.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM