Help! decimal to binary

I need use 1 if/else for repetitive use and can use more for input constraints. if you enter 345 and 2 bits obviously it wouldn't work (1 if) and in the while the pow(2,b)<=0. I know b=b-1 if it had to be setup like this.

n=513 and b=10
0 513<512 N/A
1 513-256=number
1 number<128 N/A

and ect. I need help setting this up. I am beginner

There are things built into c++ for dec to bin.
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <bitset>

int main()
{
    std::string binary = std::bitset<8>(128).to_string(); //to binary
    std::cout<<binary<<"\n";

    unsigned long decimal = std::bitset<8>(binary).to_ulong();
    std::cout<<decimal<<"\n";
    return 0;
}
it's for an assignment. All we can use are whiles, if/else. If you enter a number (say 332) and 2 bits the program shouldnt work. that is one if. then repitive in while (b==0) is the other if i believe but how do i setit up ?

#include <iostream>
#include <math.h>
#include <iomanip>
#include <windows.h>
using namespace std;

int main() {

char a='1';
int n, b ;
while(a=='1')
{
system("color f0");
cout<<"Enter a number, and an amount of bits. If it is possible this will\n determine the decimal to binary value\nEnter the number: ";
cin>>n;
cout<<endl<<"Enter the number of bits: ";
cin>>b;
cout<<endl<<endl;

b=b-1;

if
//while(b==0)



cout<<"Enter 1 to continue or anyother number to discontinue: ";
cin>>a;
cout<<endl;
}

system("PAUSE");
return 0;
}
bump!
As I understand it, this comment set describes what you want to accomplish:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
	// initialize necessary variables for a (user continue), number, bits, binary
	// while the user entry(a) =  0
	{
		//accept input for number
		//accept input for bits
		//if the number is greater than allowable for bits (pow(2,bits) < number) 
		//    reject the entry
		//else
		//    calculate the binary value of number
		//    print the binary value
		//end if

		//ask the user if he/she wants to continue, input b
	}
	return 0
}


Now, for each line in the text version, write a line or more as necessary to fulfill the statement:

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
// add all the #includes that need to go here
int main()
{
	// initialize necessary variables for: int b (user continue), number, bits, binary

	int a = 1; 

	//you only need to initialize them when you write the code that uses them.

	// while the user entry(a) =  0
	while (a == 1)
	{

		// accept input for number:

		std::cout << "Enter a number: ";
		std::cin >> number;

		//accept input for bits

		std::cout << "Enter bits:";
		std::cin>> bits;

		//if the number is greater than allowable for bits (pow(2,bits) < number) 

		if(pow(2,bits) < number)
		{
			//reject the entry
		}

		//else
		else
		{
			//calculate the binary value of number
			//print the binary value
		}
		//ask the user if he/she wants to continue, input b

		std::cout << "Enter 1 to continue, any other number to discontinue:"
		std::cin >> a;

	} // end of while (a == 1) loop

	std::cout << "Goodbye!\n";
	return 0;
}


As you add a block of code, try compiling and running it, even if you don't have anything to print out yet. Then add another block, try it. . .etc. That way if you do have a small error somewhere you 'll have a lot better chance to find it quickly.

I'm sure it is tempting to document your code (usually a class requirement) after you complete the assignment, but I assure you, if you use your documentation to create and complete a plan it can save a lot of pain and suffering.
Last edited on
Topic archived. No new replies allowed.