Hey guys Im trying to create a code where:
input A: number
Input B: Base
Print: Decimal form

I am going to use algorithm where u do following:
NUMBER % BASE
but it would do it the number of times that the base is..
so if its 6 (base 2)
6%2 0
3%2 1
1%2 1
print 110
What I was thinking is this..do the % ammount of times=base..so if its 5 do it 5 times..and then check if the binary=input A

which should always work..i think
plz help I am very lost :s

tyy :))

#include <iostream>

using namespace std;

int main()
{
   int q, r, x;
	cout << "enter #: ";
	cin >> x;
	q=x;
		while (q != 1)
		{
			r = (q % 2);
			cout << r << endl;
			q = (q/2);
			if (q==1)

            cout << q;
				break;
			}
    return 0;
		}

I have that but it doesnt work :(

What does "it doesnt work" mean? Are we supposed to spend our time trying to figure out what it does wrong? Or could you simply explain what it does wrong so we know what to look for?

line 16-19 is the error place...

if ( condition )
{

}

Maybe?

What does "it doesnt work" mean? Are we supposed to spend our time trying to figure out what it does wrong? Or could you simply explain what it does wrong so we know what to look for?

line 16-19 is the error place...

And that is an accurate description of what the program does wrong, correct? Or is that simply where the problem lies?

Format your code properly and I'm guessing the error will probably smack you in the face. I still don't know what the error is, though.

hmm.. so also line 11?

The 'if' statement on line 16 only affects the cout << q. If you look carefully, you'll see that the 'break' is always executed and it will never loop.

However, it's still wrong if you fix that. From what I can tell by running the code in my head, you are attempting to convert the entered number to base 2. Your code will put the least significant digits first, which is backward. For example, if it were base 10 (if you replaced the '2' with '10'), then the number 264 would print

4
6
2

You need to reverse the order of the digits for it to be a proper binary number.
Why not do the endl outside the loop, after it has completed?

Why is it looping while q is not equal to 1? Can you describe why it shouldn't be looping while it is not equal to zero? Your program should print '1' if you enter 1, right?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.