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;
Printable View
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;
?
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();
}
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!Quote:
Originally Posted by Daved
cppreference is always good