Hi,
I want to know how to concatenate numbers in C++.

int a = 4;
int b = 5;

int c = a+b;

I have tried this but this give me sum of int a and b. I want 4 and 5 concatenate in c variable and give me out 45. Anyone please help. Thanks.

Recommended Answers

All 16 Replies

You'll want to convert both a and b to strings, concatenate them, then convert the result back to an integer. Here's an example using string streams:

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main()
{
    int a = 4;
    int b = 5;
    ostringstream oss;

    oss << a << b;

    istringstream iss(oss.str());
    int c;

    iss >> c;
    cout << c << endl;
}

or you can just use stringstream, which is basically i/o-stringstream.

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main(){
    string a="a";
    int one=1;
    stringstream concat;
    concat<<a<<one;
    //a=concat.str(); //defines the string concat.
    cout<<concat.str()<<endl;
    return (0);
}

This would work as long as the numbers contain only one digit.

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main()
{
    int a, b, c;
    cout << "First: ";
    cin >> a;
    cout << "\nSecond: ";
    cin >> b;
    a = a * 10;
    c = a + b;
    cout << "\nResult: " << c;

    return 0;
}

I would not use your program Albino because it's very restrincting to only 1 digit. Instead, either you can use the previous examples or you can do the concatenation "hard coding":

#include <iostream>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string>
using namespace std;

bool check(string a){
    for (int i=0;i<(int)a.length();i++){
        if (!isdigit(a[i])) return (false);
    }
    return (true);
}

int main(){
    string a, b;
    cout<<">a: ";
    cin>>a;
    cout<<">b: ";
    cin>>b;
    int ac=0;
    if (check(a) && check(b)){
        a=a+b;
        for (int i=a.length();i!=-1;i--)
            ac=atoi(&a[i]); //this is the conversion from the char a[i] to the int.
        cout<<"Int resulted: "<<ac<<endl; //this is the combined int.
    }
    else cout<<"Invalid input.\n";

    return (0);
}

Either ways, the answer will be the same.
If you have further questions just ask.

A much simpler way, if this concatenation is just part of a much bigger program, is to use atoi and itoa. itoa() the two numbers, then strcat(), then atoi() back.

There are multiple ways indeed. You should post that for the sake of making an almost complete version of that.

I am a simple man so I prefer just simple multiplication:

#include <iostream>

using namespace std;

int together(int a, int b) {
    int magnitude = 1;
    while(magnitude <= b) magnitude *= 10;
    return magnitude * a + b;
}

int main()
{
    int a=99,b=10;

    cout << together(a,b) << endl;
    return 0;
}
commented: An old fashion and simple multiplication indeed. +2
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int combine(int x, int y)
{
    int z;
    if(y >= 10)
        x *= 10;
    x *= 10;
    z = x + y;
    return z;
}

int main()
{
    int a, b, c;
    cout << "First: ";
    cin >> a;
    cout << "\nSecond: ";
    cin >> b;
    c = combine(a, b);
    cout << "\nResult: " << c;
    return 0;
}

Updated version, now works for two digits. Somewhat inspired by pyTony.

A much simpler way, if this concatenation is just part of a much bigger program, is to use atoi and itoa. itoa() the two numbers, then strcat(), then atoi() back.

atoi() is simpler than (x1*10) + x2? How do you figure that? Please explain...

And itoa() is not generally available (see portability).
Or did you mean Illinois Tactical Officers Association? :-P

atoi() is simpler than (x1*10) + x2? How do you figure that? Please explain...

And itoa() is not generally available (see portability).
Or did you mean Illinois Tactical Officers Association? :-P

Well, for a start, it works only for single digit numbers. The method of multiplying by 10, I mean.

Did you run my code? If not enough digits use unsigned long long

#include <iostream>

using namespace std;

    unsigned long long together(unsigned long long a, unsigned long long b) {
        unsigned long long magnitude = 1;
        while(magnitude <= b) magnitude *= 10;
        return magnitude * a + b;
    }

    int main() {
        unsigned long long a=123456789, b=123456789;

        cout << together(a,b) << endl;
        return 0;
    }

That wouldn't work though.

If you found bug, you should state the inputs and incorrect output produced. If the total length of integers exceed the length of valid unsigned long long, and produces overflow, I do not consider invalid behaviour.

When I tried running it, it worked correctly. All it does though is multiply a by ten and then adds it with b. Is there something I'm missing?

The while loop.

I did find one case, which my code does not manage right and it is if second number is 0, so I special cased it. Here with interactive loop, which does not make much sense for usage, but is good for testing (typically you would use this kind of function to do say pandigital number from single digits) Giving negative input for unsigned long long also gives quite interesting output, but this I do not consider bug but feature.

#include <iostream>

using namespace std;

unsigned long long together(unsigned long long a, unsigned long long b) {
    unsigned long long magnitude = 1;
    if (b == 0){
        magnitude = 10;
    }else{
        while(magnitude <= b) magnitude *= 10;
    }
    return magnitude * a + b;
}

int main()
{
    unsigned long long a=1, b;

    while(a != 0) {
        cout << "First (0 to quit): ";
        cin >> a;
        if(a) {
            cout << "Second: ";
            cin >> b;

            cout << "Together as one number: " << together(a,b) << endl;
        }
    }
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.