Integer combination command. C++

Asked

Viewed 207 times

2

Is there a command that allows the combination of two integers?

I have a variable that counts 5 and one that is worth 6 for example, there is some command that makes it possible to play the combination of these variables, the value 56, in a third variable?

  • 1

    it seems to me that there is still no problem as defined in Help Center. Search for show what you did, better define your doubt.

  • Yes, there are several. Be a its first variable, and b the second. An example of command that does what you want is: c = a * 10 + b. But as already commented, your problem is not so well defined. As you can see, you have received several different responses, all valid in some context. And such questions are not very welcome on this site.

2 answers

3

The short answer is:

unsigned concatenate(unsigned x, unsigned y) {
    unsigned pow = 10;
    while(y >= pow)
        pow *= 10;
   return x * pow + y;        
}

All credit for: Reply in English

2


Follow another alternative using Stringstream:

#include <iostream>
#include <sstream>

using namespace std;

int main() {
    int num1, num2, num3;
    stringstream ss;

    num1 = 505;
    num2 = 560;

    ss << num1 << num2;
    ss >> num3;

    cout << num3 << endl;
    return 0;
}

See demonstração

Browser other questions tagged

You are not signed in. Login or sign up in order to post.