concatenate 2 integers and make 3rd integer

I have 2 integers 1 and 001 how do i make it 1001? Tried to convert both numbers into string using to_string and concatenated and again did stoi but this returns 11 not 1001.
Maybe your string concatenation resulted in 0011 instead of 1001.
basically to_string(001) return 1 , is there a way to get 001?
Only if you store it as a string.
Show your context. At the moment you haven't shown any requirement to do arithmetic, so you could just input two std::string and output them as the concatenation.

Or do integer arithmetic: 1000 * a + b.

Choice is yours. Show some context (and code).

this looks like you are asking the wrong question.
what do you have, what do you want to accomplish?
because to-string and back is very inefficient if you have 2 ints and just want to jack them together. But if you have 2 ints, you may need to think in binary/bits instead of base 10? because, at the moment, you have 1 representing 1 and 1 representing 8. Do you know that the one that represents 8 is 8? If so, just add them. 8+1 is 1001 is 9.
> I have 2 integers 1 and 001 how do i make it 1001?
I'm guessing the OP read in a country code and a phone number as integers, and now wants to construct a proper IDD number out of the resulting mess.
I have 2 integers 1 and 001
Those are the same number. So is 01, 0001 and 000000000000000000000000000000000000000000000001.

What makes the second number 001 one of the others? I the real problem to concatenate a single digit to a 3-digit number, padded with zeros if needed? Or some other requirement?
@Scarletpimp, why the reluctance to show what you've tried to code so far? This is a rather trivial use of std::string and a couple of numeric conversion functions in the <string> header.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>

int main()
{
   int a { 1 };
   int b { 1 };

   std::string str_num { std::to_string(a) + "00" + std::to_string(b) };

   std::cout << str_num << '\n';

   int num { std::stoi(str_num) };

   std::cout << num << '\n';
 }

001 is not an integer, it is a string construct based on an integer of 1.
I have 2 integers 1 and 001


As others have said, this makes no sense. If you have an int of value 1, it is 1. Not 001. 1.

You have two integers of the same value, 1.

What makes you think that one of your int values is 1, and the other 001 ?
basically to_string(001) return 1 , is there a way to get 001?
As it should, 001 is 1. Presentation is not storage.

You want 001 from 1? Easy peasy:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>

int main()
{
   int num { 001 };

   std::cout << num << '\n';

   std::string str { std::to_string(num) };

   std::cout << str << '\n';

   str = "00" + str;

   std::cout << str << '\n';
}

1
1
001

Maybe if you gave us more details on what you are trying to do we can give better guidance.
sorry i was looking for a way to concatenate integer 1 and another 0 padded number 001. so its like i will get this 2nd part as input and have to pad with zeroes which i have done with below code. I was looking for a way to concatenate these integer and padded value. I found the solution now.


int x = 1;
ostringstream ret;
ret << setw(3) << setfill('0') << x;
int test = stoi(to_string(1) + ret.str());

now test has 1001 which is what i was looking for. Thanks everyone for the hints.
Isn't that just int test = 1 * 1000 + x; ??
Topic archived. No new replies allowed.