Jump to content
  • Advertisement
Sign in to follow this  
Tertsi

Any standard way/function like appendtoend(255, 21) = 25521

This topic is 4912 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

int x = 255 int y = 22 int z = 300 I'd like to be able to simply append them to variable xyz so that with a function call xyz would become 25522300. I know of a bad way to make a function like this but I'm wondering if anyone has an efficient way or knows of a standard C++ function that does this?

Share this post


Link to post
Share on other sites
Advertisement
If you know how many digits they're supposed to be then just multiply by 10^x, if you don't, then what's the point of this? You can't get your numbers back. Perhaps you would rather use strings instead of integers.

Share this post


Link to post
Share on other sites
Nope. Basically, just do the arithmetic:


int digitConcatenate(int prefix, int suffix, int base = 10) {
int counter = suffix;
int shifted = prefix;
do { shifted *= base; counter /= base; } while counter;
return shifted + suffix;
}

Share this post


Link to post
Share on other sites
I don't need to be able to get the numbers back from that xyz (or whatever) number.

It's not pre-determined how many digits they are supposed to be. I could of course do this with if statements so that I wouldn't need to know how many digits they are supposed to be but that is the 'bad way' that I mentioned.

Share this post


Link to post
Share on other sites
Well, here's a function:


#include <cmath>

int appendTo(int a, int b)
{
return b + a * static_cast<int>(std::pow(10.0, 1.0+std::floor(std::log10(static_cast<double>(b)))));
}




It doesn't do anything interesting with negative numbers, but then again I'm not really sure what appending -5 and -4 would be supposed to yeild.

Why you would need such a function beyond me, so back to my question, what's the point of this?

Share this post


Link to post
Share on other sites
The safe C++ way is to use:


#include <sstream>
std::stringstream ss;
ss << x << y << z;
int result;
ss >> result;
return( result );

// Or you could directly return a string:

return ss.str( );





An unsafe C-only alternative is:


buffer[256];
sprintf( buffer, "%i%i%i", x, y, z ); //Unsafe, can overflow!
return( atoi( buffer ) );



Share this post


Link to post
Share on other sites
Quote:
Original post by smart_idiot
sprintf is a safety hazard, use snprintf instead.
On the other hand snprintf isn't standard and would silently cut of the string if additional buffer space were required.
A better solution would be to approximate the maximum required buffer size instead.
enum { MAX_DIGITS = sizeof(unsigned) * CHAR_BIT / 3 };
char buf[MAX_DIGITS * 2 + 1];
However the arithmetic solution is probably preferable..

Share this post


Link to post
Share on other sites
Quote:
Original post by doynax
On the other hand snprintf isn't standard


[begin nit-pick]

Actually it is but its part of the C99 standard and C++ 0x03 TR1 standard ( std::tr1::snprintf ).

[end nit-pick]

Share this post


Link to post
Share on other sites
Quote:
Original post by snk_kid
Actually it is but its part of the C99 standard and C++ 0x03 TR1 standard ( std::tr1::snprintf ).
Yeah, I guess you're right.
I should've written "snprintf() isn't portable in practice" instead.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!