OK, now I have rather stupid question, but i really need answer...
I have 2 variables-
Now, how do I make them sit together in one variable, like int xy=43; ?Code:int x=4; int y=3;
OK, now I have rather stupid question, but i really need answer...
I have 2 variables-
Now, how do I make them sit together in one variable, like int xy=43; ?Code:int x=4; int y=3;
int xy = x * 10 + y;
?
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
use pointers;
Code:#include <cstdlib> #include <iostream> using namespace std; int main() { int a = 56; // a equals 56 int *b = &a; // b is an int pointer, pointing to address of a *b = 23; // set value of address pointed by b to 23 cout << "a = " << a << endl << "b = " << *b << endl; // dump both vars to screen return 0; }Code:outcome: a = 23 b = 23
Here's one way:
Code:#include <iostream> #include <string> int main() { int x = 4; int y = 3; char buff1[200]; std::string temp1 = itoa(x, buff1, 10); std::string temp2 = itoa(y, buff1, 10); std::string result = temp1 + temp2; int xy = atoi(result.c_str()); std::cout<<"x is: "<<x<<", y is: "<<y<<", xy is: "<<xy<<std::endl; std::cin.ignore(); std::cin.get(); }
Last edited by Loctan; 08-04-2006 at 03:41 PM.
Might as well use a stringstream instead of atoi and the non-standard itoa.
I don't know anything about stringstreams so I will go and read up on them. I didn't even realize that itoa was non standard. Thanks!Originally Posted by Daved
cppreference is always good