Sign in to follow this  
jsloan

Basic C++ question

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this