Let the dust settle.
Here's some minor revisions to make it (sort of) work. Please go on to the text afterwards ... and grit your teeth: I'm going to acknowledge the errors that @tpb found.
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
bool checkbinary(unsigned long long);
int convertBtoD(unsigned long long);
void drawline(char);
void header ();
void body(unsigned long long);
int main()
{
int n;
unsigned long long A[20], bin; // <===== Gives slightly more margin .. but ultimately
// binary representations are so long that you need a string
cout << "Please enter the number of binary values desired: ";
cin >> n;
while ( (n < 0) || (n > 20) )
{
cout << "ERROR: THE NUMBER ENTERED IS IN-VALID; PLEASE RE-ENTER: ";
cin >> n;
cout << endl;
}
for ( int k = 0; k < n; k++)
{
cout << "Please enter the Binary number: ";
cin >> A[k]; // <===== You want the kth value, not the nth
while (!checkbinary(A[k]) ) // <===== Same again ... also, change to a while loop (may repeat the error)
{
cout << "ERROR: NUMBER NOT A BINARY NUMBER; PLEASE RE-ENTER: ";
cin >> A[k]; // <===== And again
}
}
drawline ('#');
header();
drawline ('-');
for (int c = 0; c < n; c++) // <===== Should be < n, not <= n
{
bin = A[c]; // <===== You want the cth value, not the nth
body(bin);
}
drawline ('#');
system ("pause"); // <===== Not ideal
}
void drawline(char SYM) // <====== Don't use $
{
for (int c = 1; c<=30; c++) cout << SYM;
cout << '\n';
}
void header()
{
cout << setw(15) << "Binary" << setw(15) << "Decimal" << endl;
}
void body(unsigned long long bin)
{
cout << setw(15) << bin << setw(15) << convertBtoD(bin) << endl;
return;
}
bool checkbinary(unsigned long long bin)
{
int d;
cout << "testing " << bin << endl;
while (bin > 0)
{
d = bin % 10;
if ( (d != 1) && (d !=0) ) // <===== Should be &&, not ||
return false;
bin /= 10;
}
return true;
}
int convertBtoD (unsigned long long num)
{
int d = 0, w = 1;
while (num > 0)
{
d = d + (num % 10) * w;
num /= 10;
w = w * 2;
}
return d;
}
| |
Please enter the number of binary values desired: 3
Please enter the Binary number: 112
testing 112
ERROR: NUMBER NOT A BINARY NUMBER; PLEASE RE-ENTER: 1100
testing 1100
Please enter the Binary number: 1010
testing 1010
Please enter the Binary number: 111111
testing 111111
##############################
Binary Decimal
------------------------------
1100 12
1010 10
111111 63
############################## |
Outstanding issues.
(1) Binary representations are VERY long. I've given you a bit more margin with an unsigned long long above (a long is probably the same size as an int). Ultimately, you do need to replace this with a
string (as @tpb tried to persuade you right at the beginning).
(2) A general comment on how you write code. You have an int main() and
five separate functions ... before you start testing. Isolate them and check
one at a time, building your program gradually.
Short test driver + convertBtoD()
Short test driver + checkBinary()
Also, avoid the fancy formatting until last of all - it's distracting from the hard numerical parts.
Get some sleep and revise it in the morning.