0

I already tried looking for this and they gave me to just do along the lines of:

int x = 1;
while (x != 11)
{
    x = x * 10 + (x+1);
}
cout<<x;

output: 12345678910

While this is good and all I have the problem that if the first number is a zero, it ignores that. so it would do

0 * 10 + (0+1)

which would come out to "1" not "01". Is there a good alternative way to add numbers together in this manner?

Sabishī
  • 373
  • 1
  • 2
  • 11
  • 1
    Use `std::string` for that – Fureeish Dec 31 '18 at 23:51
  • 3
    Displaying a leading 0 is a visualization problem. – drescherjm Dec 31 '18 at 23:51
  • 01 and 1 denote the same number though – yuri kilochek Dec 31 '18 at 23:52
  • Yeah but Im making a binary converter and some numbers start with 0, not 1 – Sabishī Dec 31 '18 at 23:54
  • 1
    Also, literal numbers with leading zeros are interpreted to be in base 8. E.g. 0123 and 123 are not the same numbers and `0123==123` returns `false`. – Quimby Dec 31 '18 at 23:56
  • You could use `std::bitset` and convert to a string. https://stackoverflow.com/questions/7349689/how-to-print-using-cout-the-way-a-number-is-stored-in-memory – drescherjm Dec 31 '18 at 23:57
  • 1
    @Sabishī -- `01` is `1` in binary. So I don't know why a "binary converter" is anything special to consider. – PaulMcKenzie Dec 31 '18 at 23:57
  • @PaulMcKenzie I actually realized that my converter did the conversion in reverse so I have to reverse it. I got that all setup but now this is my problem that when a lot of numbers in binary like for example "2" in decimal is "10" in binary it would first give the system "01" but because of my code it gives "1". – Sabishī Jan 01 '19 at 00:02
  • Tbh the whole code is quite a mess but I was wondering if there was a way to do it like this instead of redoing the code so it converts the right way. – Sabishī Jan 01 '19 at 00:03
  • You can't convert while using an integer and keep the leading 0s in the int. if you are not permitted to use std::bitset do it using a string. – drescherjm Jan 01 '19 at 00:03
  • Then I guess i'll convert it into a string then. – Sabishī Jan 01 '19 at 00:04
  • Do you know how long your final number is going to be? – andrewec Jan 01 '19 at 00:29
  • No, its for a binary converter so its up to the user to choose how long its going to be. – Sabishī Jan 01 '19 at 00:31
  • So the user is inputting something like "0110100101" to a console, and expecting to get the decimal equivalent as a response? – andrewec Jan 01 '19 at 00:32
  • Well yeah, just the other way so a decimal to binary converter. – Sabishī Jan 01 '19 at 00:34

2 Answers2

2

It's not clear why you're trying to do, but it may help to think in terms of either strings or numbers. For a number there is no difference between 1 and 01, but for a string there is. So if the difference matters to you, use a string.

For example, you can simply use the << operator to convert individual digits to strings as you go:

int x = 0;
while(x<11)
{
    cout << x;
    x++;
}

Output:

012345678910
Heath Raftery
  • 3,643
  • 17
  • 34
  • I appreciate the response and I guess this would work but as I said in an earlier comment the final number still gets reversed after the while loop. Is there a way I could assign the final number to an int? – Sabishī Jan 01 '19 at 00:17
  • so I could declare like x = x + x+1 (but without summing x to x+1, rather adding it at the end of the number) – Sabishī Jan 01 '19 at 00:21
  • Sure. The "adding" operation you refer to is actually a "string concatenation". The `*10` trick is just that - a trick to perform actual string concatenation. To make it easier, just do: 1) integer addition, 2) integer to string conversion, 3) string concatenation, and finally 4) string to integer conversion. Try each of those steps - you'll find heaps of examples with the right keywords. If you get stuck, ask a new specific question. – Heath Raftery Jan 01 '19 at 05:29
0

Might be the case that bitsets are what you're looking for. In a very quick example...

#include <iostream>
#include <bitset>

using namespace std;

int main(){
   int                  input;
   bitset<32>           result(0);   //32 bits in an int

   cin >> input;
   result = input;
   cout << result;

   return 0;
}
andrewec
  • 136
  • 9