• Advertisement
Sign in to follow this  

Basic C++ question

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
Quote:
Original post by Ra
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.


Thats definatly the most creative way. :)

Thanks Guys.

Share this post


Link to post
Share on other sites

#include <iostream>
#define num(x,y) x##y

int main()
{
std::cout << num(53,36);
}


Share this post


Link to post
Share on other sites
Quote:
Original post by mike25025
that will only work for constants
so it would be easyer to type 5336


Yep

But that takes away all the fun

Share this post


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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
a = 2, b = 5;
num(a,b) ==> ab

Share this post


Link to post
Share on other sites
How about this:


#include <iostream>
#include <string.h>
using namespace std;

int main()
{
char *number_1 = new char [30];
char *number_2 = new char [30];
cout<<"Enter a number or word (without spaces): \n\n";
cin>>number_1;
cout<<"\n\nEnter another number or another word (without spaces): \n\n";
cin>>number_2;
cout <<"\n\n"<<"Concatenated = "<<strcat(number_1, number_2)<<"\n\n";

system ("pause");
return 0;
}







[Edited by - Sshado on September 22, 2004 12:28:10 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement