Hello, I am new to these great forums here and new to C++ too
Anyway, I am coding a binary to decimal converter and I have the math right and everything but C++ does not want to play nice with the math.
here is the output:
Code:
Enter a binary number: 11110000
1 = 98
1 = (2 ^ 0) * 1
1 = 147
1 = (2 ^ 1) * 1
1 = 0
1 = (2 ^ 2) * 1
1 = 49
1 = (2 ^ 3) * 1
0 = 288
0 = (2 ^ 4) * 0
0 = 336
0 = (2 ^ 5) * 0
0 = 192
0 = (2 ^ 6) * 0
0 = 240
0 = (2 ^ 7) * 0
the decimal number shows the output C++ gave for the digit, the tabbed line under it shows the math it did to get that number, if you do the math manually the output should be different.
Here is my code:
Code:
char inText[32];
int final;
cout << "\n Enter a binary number: ";
cin >> inText;
cout << "\n Decimal Result: \n";
for(int length = 0; length < strlen(inText); length++)
{
// The forumla
final += 2 ^ length * inText[length];
// Shows 1 = $output
cout << inText[length] << " = " << ((2 ^ length) * inText[length]) << "\n\t";
// Shows 1 = (2 ^ $len) * $digit
cout << inText[length] << " = " << " (2 ^ " << length << ") * " << inText[length] << "\n";
}
cout << final;
cout << "\n\n";
Thanks in advance