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

Started by
11 comments, last by ToohrVyk 19 years, 2 months ago
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?
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.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
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;}
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.

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?
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
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 ) );
sprintf is a safety hazard, use snprintf instead.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
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..
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]
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.

This topic is closed to new replies.

Advertisement