?
Solved

[noob][c++]  combining numbers

Posted on 2007-11-16
9
Medium Priority
?
268 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
Comment
Question by:Troudeloup
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 580 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

by:UrosVidojevic
UrosVidojevic earned 520 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

by:jkr
jkr earned 600 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 7

Assisted Solution

by:UrosVidojevic
UrosVidojevic earned 520 total points
ID: 20301589
:-)
0
 

Author Comment

by:Troudeloup
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

by:Infinity08
Infinity08 earned 580 total points
ID: 20301939
Did you see my post ?
0
 
LVL 11

Assisted Solution

by:Deepu Abraham
Deepu Abraham earned 300 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

by:jkr
jkr earned 600 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

by:UrosVidojevic
UrosVidojevic earned 520 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

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

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

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

Join & Ask a Question