Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

nextgengamer

breaking apart an int

This topic is 5988 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi does anyone know of a way to take an int and break it down into its seperate numbers. ex) int x = 1234; I want to do something like int y[4]; y[0] = 1; y[1] = 2; y[2] = 3; y[3] = 4; any help would be appreciated. thanks NextGenGamer

Share this post


Link to post
Share on other sites
Advertisement
I guess it wouldnt be the fastest way... but you could convert it to a string, separate it, then convert it back to int?

Simon

Share this post


Link to post
Share on other sites
Thats an idea im not too concerned with time at the time of this operation anyway since it is simply displaying the top 10 scores on screen

thanks for the idea though

Nextgengamer

Share this post


Link to post
Share on other sites
char szText[10];
int iScore = 1230;

itoa(iScore, szText, 10);

szText will now contain the number as a string.

You can access it like your example szText[0], szText[1], etc..

[edited by - granat on June 27, 2002 1:17:49 AM]

Share this post


Link to post
Share on other sites
For C, look into
sprintf  


For C++, look into
< sstream > (NOT < strstream > !)   


/*=========================================*/
/* Chem0sh */
/* Lead Software Engineer & Tech Support */
/* http://www.eFaces.biz */
/*=========================================*/

[edited by - Chem0sh on June 27, 2002 1:22:19 AM]

Share this post


Link to post
Share on other sites
You could also do it with some simple math if you want.

int num=1234;
int y[4];
int x=0;
int holder=1;
while(num!=0)
{
if(num%(holder*10)==0)
{
x++;
holder=holder*10;
}
num-=holder;
y[x]++;
}

should work.

Share this post


Link to post
Share on other sites
quote:
Original post by nextgengamer
Hi does anyone know of a way to take an int and break it down into its seperate numbers.

ex) int x = 1234;

I want to do something like

int y[4];
y[0] = 1;
y[1] = 2;
y[2] = 3;
y[3] = 4;



At first I wanted to say use left shifts, 4 bits for each digit, but that would only work in Hex. Consider how numbers are built

1234 = 1 * 1000 + 2 * 100 + 3 * 10 + 4 * 1

and work backwards from this - but if you''re seeking to display these number on screen, it''s probably best to use a string anyways.

Share this post


Link to post
Share on other sites
quote:
Original post by granat
itoa(iScore, szText, 10);



itoa is evil, non-standard, unportable and non typesafe.

quote:
Original post by Chem0sh
For C++, look into <sstream> (NOT <strstream> !)


Or go there, grab Boost''s lexical_cast.hpp which does it for you.


#include <boost/lexical_cast.hpp>
#include <string>

using namespace std;
using namespace boost;

string s = lexical_cast<string>(1234); // cast to string
string s = lexical_cast<string>(123.4); // cast to string

long i = lexical_cast<long>("1234"); // cast to long
float f = lexical_cast<float>("123.4") // cast to float


And so on... You''ll admit it is a bit more intuitive.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

Share this post


Link to post
Share on other sites
umm, why not the simple portable way?

  
snprintf(outBuff, OUT_BUFF_SIZE, "%s %08d", playerName, playerScore);


much easier since you can format everything nice like for pronting staright to the screen instead of having to offset stuff. ie names and scores will take only a single line and be printed correctly. this can be used for quake style console support if you add \n to your printed buffer and your text output system handles that sort of thing.

Share this post


Link to post
Share on other sites
quote:
Original post by a person
snprintf(outBuff, OUT_BUFF_SIZE, "%s %08d", playerName, playerScore);



How about


  
#include <string>

#include <sstream>

#include <boost/format.hpp>


using namespace std;
using namespace boost;

ostringstream ss;
format frm( "%s %08d" );
ss << frm % playerName % playerScore;
string str = ss.str();


Yes, I admit it, I''m a Boost addict.
(Note: boost::format is still in beta)

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!