How to joint 2 int???

This is a discussion on How to joint 2 int??? within the C++ Programming forums, part of the General Programming Boards category; Hi, e.g. int a = 20; int b = 07; I want to joint a and b together to get ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    12

    How to joint 2 int???

    Hi,

    e.g.
    int a = 20;
    int b = 07;

    I want to joint a and b together to get 2007.
    Anybody know how to do it?

    lots of help,
    ken js

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    Like multiplying a by 100 perhaps?
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Not very generic. I think what you want is to convert both to strings and concatenate them. As in:
    Code:
    std::ostringstream str;
    str << a << b;
    std::cout << str.str() << std::endl;
    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

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    Or the answers on other forums - enjoy the variety in the answers.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,158
    Quote Originally Posted by CornedBee View Post
    Not very generic. I think what you want is to convert both to strings and concatenate them. As in:
    Code:
    std::ostringstream str;
    str << a << b;
    std::cout << str.str() << std::endl;
    That's going to result in "207" not "2007."

    The line:

    int b = 07;

    Is bogus anyway, because the literal is being treated as octal. For this value it doesn't matter, but...

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    It always amazes me how easy it is to overlook these things.
    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

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    Or, the convoluted way:

    Output 20,0,7 to a txt. file(app), close, reopen the file then retrieve as a string.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Replies: 2
    Last Post: 03-24-2006, 07:36 PM
  3. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  4. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-01-2003, 11:26 PM
  5. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 06:34 AM

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