A basic syntax question

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 11-23-2006
IdioticCreation
A basic syntax question
Hey,

I am just getting started with c++ and I am adjusting from php, which has proved difficult so far. Variable types are throwing me off.

I can't find anywhere how you would 'combine' two integers. For example:

int x = 5;
int y = 2;

and I want to make them 52 or 25. In php you can just separate them with a period and they would append. Is it similar in C++ or will I have to write a math formula in a function to derive the desired number from two augments?

Thanks!
David
• 11-23-2006
Tonto
>> I have to write a math formula in a function to derive the desired number from two augments

That's the one.
• 11-23-2006
IdioticCreation
*Curse words*

,but thanks.

Could you supply me with this formula?

thanks
• 11-23-2006
zx-1
You use a stringstream:

Code:

```#include <iostream> #include <sstream> int main() {         int x = 5, y = 2;         std::stringstream s;         // Convert 'x' and 'y' to strings and concatenate them         s << x << y;         // Get full string with str() method         std::cout << "'" << s.str() << "'" << std::endl;                 return 0; }```
• 11-23-2006
IdioticCreation
Awesome, I love you!

But one more thing. How can I convert it back into an integer.

Sorry, I feel like I should be looking this stuff up myself, but I'm not on my best game today.
• 11-23-2006
Mario F.
you can use atoi()

Code:

`int value = atoi(s.str().c_str());`
Will you love me too? :) ;) :)
• 11-23-2006
zx-1
Code:

```#include <iostream> #include <sstream> int main() {         int x = 5, y = 2;         std::stringstream s;         // Convert 'x' and 'y' to strings and concatenate them         s << x << y;         // Get full string with str() method         std::cout << "'" << s.str() << "'" << std::endl;         int z;         // Extract an integer         s >> z;         std::cout << z << std::endl;                 return 0; }```
• 11-23-2006
IdioticCreation
Thanks, and I love you too Mario :p.

It all works now, just seems like it took a lot to do something so small.
• 11-23-2006
zx-1
Bump because you mightn't have seen my reply.
• 11-23-2006
CornedBee
This "something small" is not exactly something you commonly need. Usually what you do with numbers is add them, subtract them, divide them and so on. These things are easy to do in C++.
Concatenating numbers is not something you do often. You do that with strings. With strings, concatenation is easy: just use the + operator.
So to achieve what you want, you have to convert the numbers to strings, concatenate them, then convert the resulting strings back to a number.

It's the same in PHP, by the way, except that, due to PHP's weak typing, these conversions are implicit. Try it out:
Code:

```\$a = 1; \$b = 2; \$c = \$a . \$b; // Implicitly convert the values of \$a and \$b to strings. echo get_type(\$c); // Echos 'string'. \$b += \$c; // Implicitely convert the value of \$c to int.```
• 11-23-2006
IdioticCreation
Thanks for all the help guys, I am actually working with an array.

I would appreciate any help with this code, it crashes onces it gets to board[8][2], I guess its probably a memory problem.
Code:

```#include <iostream> #include <sstream> using namespace std; int main() {                                          //begin main program   int x = 1, y = 1;                                  //declare x and y   int board[8][8];                                  //declare array board   while ( y < 9 ) {                                //loop 8 times for y     while ( x < 9 ) {                              //loop 8 times for x       std::stringstream c;       c << y << x;                                //append the values of x and y       int tc = atoi(c.str().c_str());          //convert the result to an integer       board[y][x] = tc;                            //set the integer value into the array       cout<< tc;                                      //print the integer value       cout<<  "\n\n\n";       x = x + 1;                                    //incriment to x     }//end first loop     x = 1;                                          //reset x to 1     y = y + 1;                                    //incriment to y   }                                                      //end second loop cout<< board[1][1] << "\n"; cout<< board[8][8] << "\n"; cout<< board[2][1] << "\n"; cout<< board[3][2] << "\n"; cout<< board[1][6] << "\n"; cin.get(); return 0; }```
sorry if the comments are bothersome, but they help me learn.
• 11-23-2006
manutd
Code:

` y = y + 1;//incriment to y`
This can be simplified to:
Code:

`y++;`
EDIT: Same for x.
• 11-23-2006
IdioticCreation
Thanks manutd, do you have any idea why this program is crashing?
• 11-23-2006
h_howee
since you're using it as an integer, wouldnt it be easier to
Code:

`board[x][y] = y*10 + x;`
• 11-23-2006
CornedBee
Arrays are 0-based, but you treat them as 1-based.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last