Help Autofilling a variable in C++

Hi! I'm learining c++, and for a project I need to work with binary numbers.

I already have a function that translates from Decimal to binary, but I need to auto fill with ceros until it is 8 characters long.

Example: 10 in decimal is translated as 1010, but I need it to be 00001010.

Setfill tool doesn't work as I need the integer to be 8 digits long to use it in another function (as far as I've understood Setfill only works at the "cout").

Can anyone help me to find a solution?

The code is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <cmath>

using namespace std;
int binario(int n);
int main(int argc, char *argv[]) {
	int n;
	cin>>n;
	int binn=binario(n);
	
	cout<<binn;
	return 0;
}

int binario(int n){
	int exp, digito;
	double binarion;	
	
	exp=0;
	binarion=0;
	while(n/2!=0&&exp<8)
	{
		digito = n % 2;
		binarion = binarion + digito * pow(10.0,exp);
		exp++;
		n=n/2;
	}
	binarion = binarion + n * pow(10.0,exp);
	return binarion;	
}


I'm on Windows10 using Zinjai
Last edited on
bitset already does this, you can set it to a decimal number and printing it will give leading zeros
is that ok, or do you need to recreate it for learning?

eg
bitset<8> b = 10;
cout << b << endl;
Last edited on
but I need to auto fill with ceros until it is 8 characters long.


The binario() function doesn't return a string, but an int. You can't left fill an int with 0's. You can only specify how it is printed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

unsigned binario(unsigned n);

int main() {
	unsigned n {};

	cin >> n;
	cout << setw(8) << setfill('0') << binario(n) << '\n';
}

unsigned binario(unsigned n) {
	double binarion {};
	unsigned exp {};

	for (; n / 2 != 0 && exp < 8; ++exp, n /= 2)
		binarion = binarion + (n % 2) * pow(10.0, exp);

	return static_cast<unsigned>(binarion + n * pow(10.0, exp));
}



10
00001010

Well, if you store it as an int you probably get 32 bits whether you like it or not.

Preceding zeros are a representation-in-output thing.

I guess you could always try bitset<8>:
http://www.cplusplus.com/reference/bitset/bitset/bitset/



I need the integer to be 8 digits long to use it in another function

I doubt it.
Setfill tool doesn't work as I need the integer to be 8 digits long to use it in another function (as far as I've understood Setfill only works at the "cout").
Digit count is a property of a representation, not of a number. Consider this:

10024

That's a representation. Here's another one:

23450

Yet another one:

Ten thousand and twenty four.

One more:

万二十四

All of them mean the same thing: the number 10024. The number itself doesn't have digits, it's just an abstract entity. Only its representations may have digits, and only some of them do.


In other words, if you need something with "digits" then binario() should not be returning an integer at all. It should most likely return a string containing the integer's representation.
Topic archived. No new replies allowed.