Shoutbox

C++ help :P - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: C++ help :P (/showthread.php?tid=48922)

C++ help :P by leito on 08-13-2005 at 11:08 PM

Hey guys, I really need your help with this. I want to concatenate 1,2...n to a string with this...

            for (int i = 1; i<=n; i++){
                s+= i;
                s+= ",";
            }

But it just don't work, I think I have to converto from int to string, but I can't find a good way... :D

Thank you in advance for your help! I'll really appreciated, cuz I'm tired of this program.


RE: C++ help :P by TheBlasphemer on 08-13-2005 at 11:31 PM

First of all, what is s ?
a string? or a char *?
Furthermore, a variable definition in a for loop isn't valid C++ either (although it will compile)

string s;
int i;
int n=100;
for (i=1; i<=n; i++) {
  stringstream ss;
  i>>ss;
  s<<ss;
  s+=",";
}

I *think* that might work, although I don't really use those std things a lot ;)


RE: C++ help :P by leito on 08-14-2005 at 01:04 AM

Ok, It work, thanks. I did a method that changes it int to string.

#include <string>
#incluce <sstream>

std::string Extras::intToString(int n){
    std::string s;
    std::stringstream ss;
    ss << n;
    s = ss.str();
    return s;

}