Converting Decimal to Binary only returning first two digits

I want to use a while loop to convert decimal to binary, I have it almost I'm just not sure how to get the rest. Right now it is printing the last two digits of each binary number but not the first to (i.e. 10 = 1010, but is just printing 10).
I'm think I need another loop to keep returning the sum and remainder but I can figure it out. I tried asking my teacher, but he laughed and said quote; 'I'm not helping you, no one will help you when you get a job.'
First off, this is an intro course, I have no idea how to do anything.
Second, I'm going to school to learn how to do it so I can figure problems like this out on my own at a job.
Third, Where the fuck am I working that I wouldn't have anyone else in the department to ask. - sorry needed to blow off some steam.

(The commented out portion is me trying to figure out how to print the first two digits, but failing).

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
31
32
33
34
35
36
37
38
39
40
41
42
  #include "stdafx.h"
#include <iostream>

using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	int low;
	int high;
	int a;


	cout << "Enter the low number: ";
	cin >> low;
	cout << "Enter the high number: ";
	cin >> high;

	int rem, con, aaa, bbb, ccc;
	int sum, dec;
	dec = low;
	while (dec <= high)
	{

		sum = dec % 2;
		rem = dec / 2;
		con = rem % 2;
		cout << con << sum << endl;
		dec++;
	}
		/*aaa = sum % 2;
		bbb = sum / 2;
		ccc = bbb % 3;
		cout << bbb << aaa << con << sum << endl;
		cout << con << endl;
		cout << sum;
		aaa = sum % 2;
		bbb = sum / 2;
		ccc = bbb % 2;*/
	
	return 0;
}
Your code is rather wide of the mark, but I can see some statements which are part of the solution.

Now, I could give you the answer, but that wouldn't necessarily be helpful.

Can you describe in words what you need to do?

I assume you get how / and % behave (esp. integer division.) If not, try playing with them, e.g.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int main() {
    int val = 134652;

    do {
        cout << val << "\n";
        val /= 10; // i.e. val = val / 10
    } while (0 < val);

    return 0;
}


Andy

PS I guess you're using Visual Studio?

When you create a new project it is prob better to "Add" a "New Project..." of "Win32 Console Application" type, but make it an "Empty project". And then add just a single .cpp file (I usually use main.cpp) That way you avoid the precompiled header file (stdafx.h) I have a txt file with a hello program which I then copy and paste into the new file as a starting point.
Last edited on
@andywestken: I like your approach - Good advice for a new-to-C++ programmer, and a handy tip to use with MSVisual Studio!
For converting to binary I'll use 10 as an example. 10%2 = 5 rem = 0 5%2 = 2 rem=1 2%2=1 rem=0 2%1=0 rem=1 so 10 = 1010
I think i got that in my code the only problem is it is not repeating. So it looks like I want to print the remainder every time the code runs, but when I printed 'rem' I would get 5 for 10 because 10/2 =5 so I took the modulus of the rem and printed that, which worked.
So next I would need to do 5 % 2 and get the remainder. I guess I'm iffy on how that translates to code.
5 would now be stored in 'sum' and I thought to find the modulus of sum. That is all the code that is commented out. That's basically where I am, and what I understand. :(

Yep, VS. My teacher wants us to use the precompiled header as they "run faster."
If your rem is 5, it's not the remainder. For / 2 the remainder can only be 0 or 1.

The modulus operator is the right one to use; it could be called the remainder operator.

Working out how to print out the number in decimal might help, as you can see what's going on. Then apply the same approach to binary (or whatever base you feel like.)

123 % 10 = 3 <= keep this
123 / 10 = 12 <= use as i/p for next round

12 % 10 = 2
12 / 10 = 1

1 % 10 = 1
1 / 10 = 0

no more number!!

Andy

PS Regarding...

My teacher wants us to use the precompiled header as they "run faster."

Ho hum. That is true, but it's aimed at big projects where you have loads of system headers (esp. windows.h and all the includes it includes!) But if you're building a single cpp file which includes a few standard headers...

In particular, the mechanism only works if you put the common includes in stdafx.h. If you don't do this then the compiler is still processing all the header file each time you compile and the stdafx.h is a waste of time.

e.g.

1
2
3
4
// stdafx.h
#pragma once

#include <iostream> // not worth it for one measely header 


1
2
3
// stdafx.cpp

#include "stdafx.h" 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// main.cpp

#include "stdafx.h" // common header files already dealt with

// only project specific headers here

using namespace std; // should never use using in header...

int main() {
    int val = 134652;

    do {
        cout << val << "\n";
        val /= 10; // i.e. val = val / 10
    } while (0 < val);

    return 0;
}


Last edited on
Oh Gosh! I see what I was doing wrong with the variables getting all switched up.
Well, I at least got it to print out correct this time. I really wish I could get it to loop, as i'm iffy on how that works still. Right now it will just do up to 2 digit base ten numbers (unless I keep repeating the process). But I have spent way too much time on it so if you don't have time to explain that's fine I can just move on.

If I can ask; how might I go about formatting this so that binary is lined up with decimal?



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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "stdafx.h"
#include <iostream>

using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	int low;
	int high;
	int a;



	cout << "Enter the low number: ";
	cin >> low;
	cout << "Enter the high number: ";
	cin >> high;

	cout << "Decimal" << endl;
	cout << "\tBinary" << endl;
	for (a = low; a <= high; a++)
	{
		cout << a << endl;
	} 

	int rem, con, aaa, bbb, ccc, ddd, eee;
	int sum, dec;
	dec = low;
	while (dec <= high)
	{

		sum = dec % 2;
		rem = dec / 2;
		con = rem % 2;
		aaa = rem / 2;
		bbb = aaa % 2;
		ccc = aaa / 2;
		ddd = ccc % 2;
		eee = ccc / 2;
		cout << "\t" << ddd << bbb << con << sum << endl;
		dec++;
	}

	return 0;
}


>.< I swear I am so slow at this everytime I look an hour goes by.
how might I go about formatting this so that binary is lined up with decimal?

Use std::setw
http://www.cplusplus.com/reference/iomanip/setw/

Andy

PS This could be written more succinctly

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
	int rem, con, aaa, bbb, ccc, ddd, eee;
	int sum, dec;
	dec = low;
	while (dec <= high)
	{

		sum = dec % 2;
		rem = dec / 2;
		con = rem % 2;
		aaa = rem / 2;
		bbb = aaa % 2;
		ccc = aaa / 2;
		ddd = ccc % 2;
		eee = ccc / 2;
		cout << "\t" << ddd << bbb << con << sum << endl;
		dec++;
	}
Last edited on
Thanks so much!

I'm sure it can be, but I just don't have time to figure it out.
I really appreciate all the help. I've already figured out how to convert it to octal as well, and I am still working on hexadecimal.
Topic archived. No new replies allowed.