A basic syntax question

This is a discussion on A basic syntax question within the C++ Programming forums, part of the General Programming Boards category; Hey, I am just getting started with c++ and I am adjusting from php, which has proved difficult so far. ...

  1. #1
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229

    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

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    >> I have to write a math formula in a function to derive the desired number from two augments

    That's the one.

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  3. #3
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    *Curse words*

    ,but thanks.

    Could you supply me with this formula?

    thanks

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    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;
    }
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

  5. #5
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    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.
    Last edited by IdioticCreation; 11-23-2006 at 04:13 PM.

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,436
    you can use atoi()

    Code:
    int value = atoi(s.str().c_str());
    Will you love me too?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    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;
    }
    More info: http://www.cplusplus.com/ref/iostream/stringstream/
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

  8. #8
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    Thanks, and I love you too Mario .

    It all works now, just seems like it took a lot to do something so small.

  9. #9
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    Bump because you mightn't have seen my reply.
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    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.
    Last edited by IdioticCreation; 11-23-2006 at 05:13 PM.

  12. #12
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Code:
     y = y + 1;//incriment to y
    This can be simplified to:
    Code:
    y++;
    EDIT: Same for x.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  13. #13
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    Thanks manutd, do you have any idea why this program is crashing?

  14. #14
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    since you're using it as an integer, wouldnt it be easier to
    Code:
    board[x][y] = y*10 + x;

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Arrays are 0-based, but you treat them as 1-based.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. PlaySound
    By cangel in forum C++ Programming
    Replies: 16
    Last Post: 10-08-2009, 05:29 PM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. Basic question - press any key to continue
    By PedroTuga in forum C++ Programming
    Replies: 4
    Last Post: 05-19-2005, 10:38 PM
  4. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 12:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21