Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: [C++] Integer to String and Vice Versa  (Read 3527 times)

0 Members and 1 Guest are viewing this topic.

King Tetiro

Leader of Phoenix Heart
[C++] Integer to String and Vice Versa
« on: November 19, 2010, 02:02:35 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3549
Hey dudes, Im having a bit of trouble with C++ and as this is the only place I know that has people with C++, well you know.

Very simple to start off, how do I convert integer to string and string to integer?

I can grab the code I have tonight.

(This problem has taken me hours and Im still stuck, but I solve views in a matter of minutes. What's up with that?)
Logged
  • Phoenix Heart
Re: [C++] Integer to String and Vice Versa
« Reply #1 on: November 19, 2010, 02:17:25 pm »
  • (y)(;>.<;)(y)
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3293
The std::stringstream class is a typical way of doing this in C++

Code: [Select]
#include <string>
#include <sstream>

template<typename T> inline std::string ToString(const T& o)
{
     std::stringstream stream; stream << o;
     return stream.str();
}

template<typename T> inline T FromString(const std::string& s)
{
      T o; std::stringstream stream;
      stream << s; stream >> o;
      return o;
}

int main()
{
      int i = 5;
      std::string si = ToString(i);

      std::string age = "7";
      int nage = FromString<int>(age);
      return 0;
}
« Last Edit: November 19, 2010, 02:21:31 pm by TheDarkJay »
Logged
Re: [C++] Integer to String and Vice Versa
« Reply #2 on: November 19, 2010, 03:46:45 pm »
  • Doesn't afraid of anything
  • *
  • Reputation: +42/-0
  • Offline Offline
  • Gender: Male
  • Posts: 7002
for string to into, the atoi() function will take care of it.  For the other way around, do what TDJ said :P
Logged



i love big weenies and i cannot lie

Xiphirx

wat
Re: [C++] Integer to String and Vice Versa
« Reply #3 on: November 19, 2010, 05:46:33 pm »
  • Xiphirx
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3007
The std::stringstream class is a typical way of doing this in C++
*snip*

Couldn't you just do it like this?

Code: [Select]
#include<string>
#include<sstream>

int main()
{
std::stringstream stream;
int myInt = 9;
stream << 9;
std::string myString = "";
myString = stream.string();
return 0;
}

?
Logged
  • For The Swarm
Re: [C++] Integer to String and Vice Versa
« Reply #4 on: November 20, 2010, 12:33:15 am »
  • (y)(;>.<;)(y)
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3293
...that's what I did just not in their own functions? :huh: (I just copied those functions straight out of one of my own helper code collections).

for string to into, the atoi() function will take care of it.  For the other way around, do what TDJ said :P

If memory serves, atoi is generally considered the "C" way of doing things whilst stringstream the "++C" way. (++C being a slang-term for "screw C, we love polymorphism!) Whether or not this is a good thing is a matter of opinion xD
« Last Edit: November 20, 2010, 12:36:57 am by TheDarkJay »
Logged

King Tetiro

Leader of Phoenix Heart
Re: [C++] Integer to String and Vice Versa
« Reply #5 on: November 22, 2010, 11:36:59 am »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3549
Okay none of the codes worked. Wtf?
Logged
  • Phoenix Heart
Re: [C++] Integer to String and Vice Versa
« Reply #6 on: November 22, 2010, 02:19:08 pm »
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3725
Did you include the right headers?
Logged
Re: [C++] Integer to String and Vice Versa
« Reply #7 on: November 22, 2010, 02:25:01 pm »
  • 笑い男
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 2124
I doubt your computer said "none of the codes worked"
Saying exactly what did go wrong will help fix the problem a lot.

Also, trying to figure out such a common thing shouldn't take you hours.
All you have to do is use google.
You could even google exactly what you said (how do I convert integer to string and string to integer?) and probably find the answer in seconds.
Logged

この世に悪があるとすれば、それは人の心だ
  • .hack//The World

King Tetiro

Leader of Phoenix Heart
Re: [C++] Integer to String and Vice Versa
« Reply #8 on: November 26, 2010, 10:36:20 am »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3549
What's the 'const T& o'?
Logged
  • Phoenix Heart
Re: [C++] Integer to String and Vice Versa
« Reply #9 on: November 26, 2010, 04:45:12 pm »
  • Doesn't afraid of anything
  • *
  • Reputation: +42/-0
  • Offline Offline
  • Gender: Male
  • Posts: 7002
A parameter called of type const T, which is also a reference.  I don't think that it's necessary to make it const, or a reference for that matter..unless there's some specification of the stringstream class i'm unaware of.  Can we see what your error message is?  Also, the way you're using it will be helpful.
Logged



i love big weenies and i cannot lie

King Tetiro

Leader of Phoenix Heart
Re: [C++] Integer to String and Vice Versa
« Reply #10 on: November 26, 2010, 05:08:54 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3549
I shall post it next week. Next week Im implementing better graphics

For my assignment, I've made NES styled graphics :D
Logged
  • Phoenix Heart

Mamoruanime

@Mamoruanime
Re: [C++] Integer to String and Vice Versa
« Reply #11 on: November 26, 2010, 05:16:36 pm »
  • ^Not actually me.
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 9786
I'm reasonably confused as to why you're starting out with graphics in C++... That language is probably best started via console apps :\
Logged
Re: [C++] Integer to String and Vice Versa
« Reply #12 on: November 26, 2010, 05:34:16 pm »
  • Doesn't afraid of anything
  • *
  • Reputation: +42/-0
  • Offline Offline
  • Gender: Male
  • Posts: 7002
I'm reasonably confused as to why you're starting out with graphics in C++... That language is probably best started via console apps :\

I have to second this...you're jumping around too quickly.
Logged



i love big weenies and i cannot lie
Re: [C++] Integer to String and Vice Versa
« Reply #13 on: November 26, 2010, 08:01:09 pm »
  • (y)(;>.<;)(y)
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3293
I'm reasonably confused as to why you're starting out with graphics in C++... That language is probably best started via console apps :\

I have to second this...you're jumping around too quickly.

Third.

You want a really good introduction to C++ game project? Look at Interactive Fiction. Ever read any of the books in the Fighting Fantasy series? Making that simple kind of text-based game makes for a great beginners project.
Logged
Pages: [1]   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.02 seconds with 61 queries.