binary calculator problem

i am writing a program for binary calculator. here is my code:
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<iostream>
#include<string>
using namespace std;
string decimalToBinary(unsigned char num)
{
	if(num==0) return "0";
	if(num==1) return "1";
	if(num%2==0)
		return decimalToBinary(num/2)+"0";
	else
		return decimalToBinary(num/2)+"1";
}

int main()
{
	unsigned char a=0,b=0;
	cout<<"enter decimal number a: "<<endl;
	cin>>a;
	cout<<"enter decimal number b:"<<endl;
	cin>>b;
	string s1= decimalToBinary(a);
	string s2= decimalToBinary(b);
	cout<<"a= "<<a<<"in binary is "<<s1<<endl;
	cout<<"b= "<<b<<"in binary is "<<s2<<endl;
	unsigned char c = a&b;
	unsigned char d=a||b;
	unsigned char e= ~a;
	unsigned char f=~b;
	unsigned char g = a<<2;
	unsigned char h= a>>3;
	unsigned char i=a^b;
	string s3= decimalToBinary(c);
	string s4= decimalToBinary(d);
	string s5= decimalToBinary(e);
	string s6= decimalToBinary(f);
	string s7= decimalToBinary(g);
	string s8= decimalToBinary(h);
	string s9= decimalToBinary(i);
	cout<<"a & b ="<<endl;
	cout<<"a|b= "<<s4<<endl;
	cout<<"~a ="<<s5<<endl;
	cout<<"~b ="<<s6<<endl;
	cout<<"a^b="<<s9<<endl;
	cout<<"a<<2 ="<<s7<<endl;
	cout<<"b>>3= "<<s8<<endl;
}

problem is program is not working correctly.
Last edited on
line 16: unsigned int a=0,b=0;
that made some improvement. I am however trying to achieve an output similar to this:
A = 5 in binary is 00000101
B = 8 in binary is 00001000

A & B = 00000000
A | B = 00001101
~A = 11111010
~B = 11110111
A ^ B = 00001101
A << 2 = 00010100
B >> 3 = 00000001
this is a matter of formatting. See the example

http://www.cplusplus.com/reference/ios/ios_base/width/


on line 39 the variable s3 (I guess) is missing
ok i read it and here is what i tried
line 39
1
2
3
4
cout<<"a & b=";
cout.fill(0);
cout.width(8);
cout<<right<< s3<<endl;

even bad output
Last edited on
hm, you need to learn the difference between char and int

line 1: cout.fill('0'); // Note the ''
my bad i missed ' '. code seemed to be working now. i however have one question. i now have to set width and pad '0' for every cout operation. is there any shorter way out.
by the thanks coder777 you have always helped me a lot!!
is there any shorter way out.
You can also use setw() and setfill() within the stream (don't forget to #include <iomanip> )

http://www.cplusplus.com/reference/iomanip/setfill/

it's shorter but also reset after first appearance (otherwise you would have output like 00000a=)
okay thanks a lot. here is my final code:
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
47
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
string decimalToBinary(unsigned char num)
{
	if(num==0) return "0";
	if(num==1) return "1";
	if(num%2==0)
		return decimalToBinary(num/2)+"0";
	else
		return decimalToBinary(num/2)+"1";
}

int main()
{
	unsigned int a=0,b=0;
	cout<<"enter decimal number a: ";
	cin>>a;
	cout<<endl<<"enter decimal number b:";
	cin>>b;
	string s1= decimalToBinary(a);
	string s2= decimalToBinary(b);
	cout<<"a= "<<a<<"in binary is "<<setfill('0')<<setw(8)<<s1<<endl;
	cout<<"b= "<<b<<"in binary is "<<setfill('0')<<setw(8)<<s2<<endl;
	unsigned char c= a&b;
	unsigned char d=a|b;
	unsigned char e= ~a;
	unsigned char f=~b;
	unsigned char g = a<<2;
	unsigned char h= b>>3;
	unsigned char i=a^b;
	string s3= decimalToBinary(c);
	string s4= decimalToBinary(d);
	string s5= decimalToBinary(e);
	string s6= decimalToBinary(f);
	string s7= decimalToBinary(g);
	string s8= decimalToBinary(h);
	string s9= decimalToBinary(i);
	cout<<"a & b="<<setfill('0')<<setw(8)<<s3<<endl;
	cout<<"a|b= "<<setfill('0')<<setw(8)<<s4<<endl;
	cout<<"~a ="<<setfill('0')<<setw(8)<<s5<<endl;
	cout<<"~b ="<<setfill('0')<<setw(8)<<s6<<endl;
	cout<<"a^b="<<setfill('0')<<setw(8)<<s9<<endl;
	cout<<"a<<2 ="<<setfill('0')<<setw(8)<<s7<<endl;
	cout<<"b>>3= "<<setfill('0')<<setw(8)<<s8<<endl;
}
Topic archived. No new replies allowed.