Connecting Tech Pros Worldwide Forums | Help | Site Map

INT to STR

Marcel
Guest
 
Posts: n/a
#1: Jul 19 '05
The small code underneath is displaying the text 'hello2' as output. I
supposed it to display hello50. I think i will have to convert the int to a
string or am i wrong? If so, what is the right command? Sorry i am a
beginner....

Thanks in advance!

#include <iostream>
#include <string>

int main() {

int red = 50;

std::string temp = "";

temp += "hello";

temp += red;

std::cout << temp;

std::cin.get();

return 0;
}

Regards,

Marcel



keanu
Guest
 
Posts: n/a
#2: Jul 19 '05

re: INT to STR


Marcel wrote in <3fa8d7f0$0$9476$edd6591c@news.versatel.net>:
[color=blue]
> The small code underneath is displaying the text 'hello2' as output. I
> supposed it to display hello50. I think i will have to convert the int to
> a string or am i wrong? If so, what is the right command? Sorry i am a
> beginner....
>
> Thanks in advance!
>
> #include <iostream>
> #include <string>[/color]
#include <sstream>
template<typename T> std::string toStr(T var) {
std::ostringstream tmp; tmp << var; return tmp.str();
}[color=blue]
> int main() {
> int red = 50;
> std::string temp = "";
> temp += "hello";[/color]
//temp += red;
temp += toStr<int>(red);[color=blue]
> std::cout << temp;
> std::cin.get();
> return 0;
> }[/color]

Marcel
Guest
 
Posts: n/a
#3: Jul 19 '05

re: INT to STR



"keanu" <usenet@keanu.be> schreef in bericht
news:2R4qb.4519$Pk1.226425@phobos.telenet-ops.be...[color=blue]
> Marcel wrote in <3fa8d7f0$0$9476$edd6591c@news.versatel.net>:
>[color=green]
> > The small code underneath is displaying the text 'hello2' as output. I
> > supposed it to display hello50. I think i will have to convert the int[/color][/color]
to[color=blue][color=green]
> > a string or am i wrong? If so, what is the right command? Sorry i am a
> > beginner....
> >
> > Thanks in advance!
> >
> > #include <iostream>
> > #include <string>[/color]
> #include <sstream>
> template<typename T> std::string toStr(T var) {
> std::ostringstream tmp; tmp << var; return tmp.str();
> }[color=green]
> > int main() {
> > int red = 50;
> > std::string temp = "";
> > temp += "hello";[/color]
> //temp += red;
> temp += toStr<int>(red);[color=green]
> > std::cout << temp;
> > std::cin.get();
> > return 0;
> > }[/color]
>[/color]


Thanks a lot,

That's more complicated than i expected!

Marcel


red floyd
Guest
 
Posts: n/a
#4: Jul 19 '05

re: INT to STR


Marcel wrote:[color=blue]
> The small code underneath is displaying the text 'hello2' as output. I
> supposed it to display hello50. I think i will have to convert the int to a
> string or am i wrong? If so, what is the right command? Sorry i am a
> beginner....
>
> Thanks in advance![/color]

The other poster showed what to do. I'll explain why it happened.[color=blue]
>
> #include <iostream>
> #include <string>
>
> int main() {
>
> int red = 50;[/color]

Red is of type int.
[color=blue]
> std::string temp = "";
>
> temp += "hello";[/color]

So far you're fine.
[color=blue]
> temp += red;[/color]

There is no string::operator+=(int), however there is a string::operator+=(char).
So, what happens is that the standard conversion from int to char takes place, and operator+=(char) is called, and the character
with the value of 50 (which in ASCII is '2') is appended to your string.
[color=blue]
> std::cout << temp;
>
> std::cin.get();
>
> return 0;
> }
>[/color]

I suspect that you were expecting a std::string to behave similarly to a VB String variable. Alas, it doesn't work that way, and
you need to do the string conversion yourself before using +=.

Closed Thread