Solved

# [noob][c++]  combining numbers

Posted on 2007-11-16
259 Views
Last Modified: 2010-04-01
in python i'd do this

a = "1"
b = "2"

a = a + b = "12"

I need to combine int in c++

a = 12
b = 14

somehow I want to end up with

c = 1214

is there any way to do that?

0
Question by:Troudeloup
• 3
• 2
• 2
• +2
9 Comments

LVL 53

Accepted Solution

Infinity08 earned 145 total points
ID: 20301432
Do you want it with strings ? Then :

std::string a = "12";
std::string b = "14";
std::string c = a + b;

or you could do this :

int a = 12;
int b = 14;
int c = (a * 100) + b;
0

LVL 7

Assisted Solution

UrosVidojevic earned 130 total points
ID: 20301445
You can accomplish that this way. I hope, it isn't to complicated.

int a = 12, b = 14;
string stra, strb;
stringstream outa, outb;
outa << a; outb << b;
stra = outa.str();
strb = outb.str();
string strc = stra + strb;

of course you must include string and sstream headers with:

#include <string>
#include <sstream>
0

LVL 86

Assisted Solution

jkr earned 150 total points
ID: 20301572
Actually, uusing a stringstream you could just

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

stringstream ss;
int a = 12;
int b = 14;

ss << a << b;

cout << ss.str() << endl;

int c;

ss >> c;

cout << c << endl;
0

LVL 7

Assisted Solution

UrosVidojevic earned 130 total points
ID: 20301589
:-)
0

Author Comment

ID: 20301931
is it possible to do it without using strings?

I gave the string example from python just to be an example.
0

LVL 53

Assisted Solution

Infinity08 earned 145 total points
ID: 20301939
Did you see my post ?
0

LVL 11

Assisted Solution

DeepuAbrahamK earned 75 total points
ID: 20301948
another way,

int a = 12,b = 14 ;
char c[5];

sprintf(c,"%d%d",a,b);

use strtol() to convert it to long.

Best Regards,
DeepuAbrahamK
0

LVL 86

Assisted Solution

jkr earned 150 total points
ID: 20301963
Well, as above:

stringstream ss;
int a = 12;
int b = 14;
int c;

ss << a << b;

ss >> c;
0

LVL 7

Assisted Solution

UrosVidojevic earned 130 total points
ID: 20301976
you can use this function which takes two int arguments and returns another int.

int merge(int a, int b) {
int dec = (int)log10((double)b) + 1;
return a*(int)pow((double)10, dec) + b;
}
0

## Join & Write a Comment Already a member? Login.

### Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

#### 760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

#### Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!