convert array numbers from binary to octal

Nov 12, 2020 at 5:33pm
hi, ive asked this before and i know how to convert it now, thanks to the people that helped, but is there a way i can to it without using 'string', 't.length ; erase; empty' ? beacause i don't get how those work and bacically we haven't learned it for this question at school yet

Q: given an array that's formed from binary numbers, create a new array by converting the binary numbers of the first to octal

**also, i need to input the array from the console too

thanks in advance:)

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
  #include <iostream>
using namespace std;

int main()
{
    string s[3] = { "1101101","101","11" };
    string t;
    int d = 0;

    for (int i = 0;i < 3;i++) {
        t = s[i];
        s[i] = "";
        while (t.length() % 3 != 0)
            t.insert(0, "0");
        while (!t.empty()) {
            d = 0;
            d = 4 * (t[0] - '0') + 2 * (t[1] - '0') + (t[2] - '0');
            s[i] += d + '0';
            t.erase(0, 3);
        }
    }
    for (int i = 0;i < 3;i++)
        cout << s[i] << " ";

}
Last edited on Nov 12, 2020 at 5:46pm
Nov 12, 2020 at 6:01pm
So I guess my previous code using streams and the random class was no good either.

You should be using string to input the number, though. You can use a c-style string for input, but that really isn't good practice.

I'll have a look.......
Nov 12, 2020 at 6:29pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
include <iostream>

int main()
{
	const int maxbin = 32;

	char inpbin[maxbin] {};

	std::cout << "Enter binary number (max " << maxbin - 1 << " digits): ";
	std::cin.getline(inpbin, maxbin);

	unsigned int dec {};

	for (char* inp = inpbin; *inp; dec = dec * 2 + *inp++ - '0');

	int octal {};

	for (int i = 1; dec != 0; i *= 10, dec /= 8)
		octal += (dec % 8) * i;

	std::cout << inpbin << " is " << octal << '\n';
}


Using char array instead of string. Note no input error checking
Nov 12, 2020 at 6:31pm
@seeplus it was really good, much more practical i guess, but the teacher said i need to find another way to do it or else she'll deduct marks! :/
Nov 12, 2020 at 6:34pm
else she'll deduct marks!


She should be encouraging you to get to understand what's already available within c++!

However, see my above post.

PS We don't know what's been covered in class - or how the teacher is expecting things to be done. Without existing code on which to guide, we can only provide sample code as we see the requirement.
Last edited on Nov 12, 2020 at 6:38pm
Nov 12, 2020 at 8:55pm
This may be the way she's expecting:

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>

void digits(unsigned n, unsigned& dec)
{
	if (n >= 10)
		digits(n / 10, dec);

	dec = dec * 2 + n % 10;
}

int main()
{
	unsigned inpbin {}, dec {}, octal {};

	std::cout << "Enter binary number: ";
	std::cin >> inpbin;

	digits(inpbin, dec);

	for (unsigned i = 1; dec != 0; i *= 10, dec /= 8)
		octal += (dec % 8) * i;

	std::cout << "Binary " << inpbin << " is " << octal << " Octal\n";
}


NOTE No error checking!
Last edited on Nov 12, 2020 at 8:55pm
Nov 13, 2020 at 7:49am
@seeplus thank you sooo much!!:)
Topic archived. No new replies allowed.