Add strings together

This is a discussion on Add strings together within the C++ Programming forums, part of the General Programming Boards category; I have a string that I created and passed as a parameter to my output function. I need to add ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    122

    Add strings together

    I have a string that I created and passed as a parameter to my output function. I need to add this string to an existing string as an out.open() parameter.

    This is what I tried to do:

    Code:
    out.open("page"+pageNo+".html");
    It's obviously not working but if anyone could help me that would be great. Thanks!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    open requires a C string, not a std::string. So create a temporary string object, set it to "page"+pageNo+".html", and then in the open call you would use tempstring.c_str().

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,170
    I am guessing pageNo is an int or something.

    Google std::stringstream.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    Quote Originally Posted by cyberfish View Post
    I am guessing pageNo is an int or something.

    Google std::stringstream.
    yeah I used sstream to convert the int to a string.

  5. #5
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Just allocate some memory and use sprintf.
    sprintf(szBuffer, "page %d .html", pageNo);
    out.open(szBuffer);

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,170
    No need to go C way here.

    Exploit the overloaded operators of std::string.

    Code:
    out.open((std::string("page")+pageNo+".html").c_str());
    Last edited by cyberfish; 12-03-2008 at 11:09 PM.

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Doesn't he need
    Code:
    out.open((std::string("page")+pageNo+".html").c_str());
    ?

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,170
    hmm. Right .

    Thanks for the correction. Edited my post.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    Here is the more Efficient, Lightweight way of doing what you want to do:
    Note: Only works with 1 digit, you'll need a function (like iota) if you want more digits etc.

    Code:
    int pageNo = 1;
    char page[] = "page       ";
    page[4] = '0'+pageNo;
    page[5] = '.';
    page[6] = 'h';
    page[7] = 't';
    page[8] = 'm';
    page[9] = 'l';
    
    // now page = page1.html
    out.open(page);
    ★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★

  10. #10
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    You have one more space at char page[] = "page "!

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by execute View Post
    Here is the more Efficient, Lightweight way of doing what you want to do:
    Note: Only works with 1 digit, you'll need a function (like iota) if you want more digits etc.

    Code:
    int pageNo = 1;
    char page[] = "page       ";
    page[4] = '0'+pageNo;
    page[5] = '.';
    page[6] = 'h';
    page[7] = 't';
    page[8] = 'm';
    page[9] = 'l';
    
    // now page = page1.html
    out.open(page);
    But also very error prone and quite tedious to type - yes, it's only the once, but if you have to do it slightly differentely (e.g. the "page" changes to some other word in a foreign language or some such), you have to count the characters, and get everything right. How muhc more work is it to use a more automatic method - considering that this probably ends up with either reading or writing some sort of html content from/to a file...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    Well if you wanna make it all complicated, and take into account: "Well what if they need it in KOREAN!" or that sort of thing, then obviously, you would make your own string classes and functions.

    I was just giving the simplest form of the task (the very very simplest method).

    Yes, tedious to type, but it would be worth it in certain situations! I dunno, like an embedded system that only has 50 bytes of memory!
    ★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Quote Originally Posted by execute View Post
    Yes, tedious to type, but it would be worth it in certain situations! I dunno, like an embedded system that only has 50 bytes of memory!
    Are we writing for those systems here, though?
    Premature optimization is a bad thing, as it usually complicates and obfuscates code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You could change it to:
    Code:
    int pageNo = 1;
    char page[] = "pageZ.html";
    page[4] = '0'+pageNo;
    ... and it would be less tedious to type but still not particularly robust.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  15. #15
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    Quote Originally Posted by matsp View Post
    But also very error prone and quite tedious to type - yes, it's only the once, but if you have to do it slightly differentely (e.g. the "page" changes to some other word in a foreign language or some such), you have to count the characters, and get everything right. How muhc more work is it to use a more automatic method - considering that this probably ends up with either reading or writing some sort of html content from/to a file...

    --
    Mats
    I think that comment was firmly tongue in cheek

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vector vs. array.
    By matsp in forum C++ Programming
    Replies: 37
    Last Post: 06-23-2008, 12:41 PM
  2. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 02:24 PM
  3. Problem with Strings, Please help!
    By varus in forum C++ Programming
    Replies: 8
    Last Post: 11-27-2006, 10:47 PM
  4. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07:07 PM
  5. Table mapping Strings to Strings
    By johnmcg in forum C Programming
    Replies: 4
    Last Post: 09-05-2003, 11:04 AM

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