Public Group

# Basic C++ question

This topic is 5412 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

HI, Is it possible to add a number to an integer without actually 'adding'. Let me show you what i mean
//heres the integer
int number =5;

//heres another one
int number_2 = 1;

//is it possible to do something like
number = number & number_2;

//so instead of number == 6 it would == 51?


Thanks

##### Share on other sites
int n1 = 5
int n2 = 1
char str[16];
sprintf(str,"%d%d",n1,n2);
n1 = atoi(str);

##### Share on other sites
You could do something like number = number * 10 + number_2;, that's definitely the easiest way. It really depends on the context though; it won't work if number_2 is greater than 9.

EDIT: Since you're using C++, a stringstream will do the trick.

std::stringstream ss;ss << number << number_2;ss >> number;

##### Share on other sites
Quote:
 Original post by RaYou could do something like number = number * 10 + number_2;, that's definitely the easiest way. It really depends on the context though; it won't work if number_2 is greater than 9.

Thats definatly the most creative way. :)

Thanks Guys.

##### Share on other sites
Personally, I find the string-approach more foolproof.

Just my .02\$ ;)

##### Share on other sites
#include <iostream>#define num(x,y) x##yint main(){	std::cout << num(53,36);}

##### Share on other sites
that will only work for constants
so it would be easyer to type 5336

##### Share on other sites
Quote:
 Original post by mike25025that will only work for constantsso it would be easyer to type 5336

Yep

But that takes away all the fun

##### Share on other sites
Why wouldn't the concatenation method Hylo posted work? I was just about to post the same method...

##### Share on other sites
its because of the way the preprosseser works
num(53,36) = 5336
num(n1,n2) = n1n2

• 21
• 16
• 9
• 17
• 13