This was my solution:
#include <iostream>
int main(void);
typedef unsigned long int DWORD;
typedef unsigned char BYTE;
using namespace std;
int main()
{
DWORD numBits, curBit, inBit;
DWORD val;
char empty;
numBits = 0;
curBit = 0;
val = 0;
do
{
cout << "Enter the number of bits (1 to 32) that you will be entering, from MSB to LSB." << endl;
cin >> numBits;
} while (numBits < 1 || numBits > 32);
cout << "Accepting " << numBits << " bits:" << endl;
for (curBit = numBits; curBit > 0; curBit--)
{
inBit = 2;
while (inBit > 1)
{
cin >> inBit;
if (inBit < 2)
break;
cout << "Error: invalid bit (specify 0 or 1)." << endl;
}
if (inBit)
val |= (inBit << (curBit - 1));
}
cout << "You entered: " << val << "." << endl;
cin >> empty;
return 0;
}
Enter the number of bits (1 to 32) that you will be entering, from MSB to LSB.
20
Accepting 20 bits:
0
1
1
0
0
1
1
1
1
0
0
0
1
0
1
1
0
1
0
0
You entered: 424116.
This was the decimal to binary converter:
#include <iostream>
int main(void);
typedef unsigned long int DWORD;
using namespace std;
int main()
{
DWORD val;
char empty;
int i;
cout << "Enter a value from 0 to 4294967295:" << endl;
cin >> val;
cout << "You entered (in binary): ";
for (i = 31; i >= 0; i--)
{
if (val & (1 << i))
cout << "1";
else
cout << "0";
if (i % 8 == 0)
cout << " ";
}
cout << "b" << endl;
cin >> empty;
}
Enter a value from 0 to 4294967295:
884376993
You entered (in binary): 00110100 10110110 10000101 10100001 b
Both compile with Visual C++ 2003.
Joe, some comments (Ergot.... you need more help than comments
):
1.) Don't #include <iostream.h>, #include <iostream>. That's standard C++.
2.) Why are you using ASCII literals (0x30, 0x31) as characters? '0' and '1' work fine.
3.) Why are you using printf() in a C++ program? Use cout. (Unless you're formatting strings).
4.) accum = accum - sub; == accum -= sub;
5.) You shouldn't put programming statements on the same line as I/O (IMO), for example: cin >> accum; if (accum) dosomething(); -- I think that's ugly.
6.) You should have come up with a better way to do what you did in the first program. What if your project manager wanted you to convert 32-bit numbers? Can you imagine typing out all those literals? Or even macros? [edit]Hrm, your third go was better, but you still could have done better with it. Why not just cin to an int and test for 1 or 0? (Note that my program tested for illegal values also -- what would yours do if I put in a 2 or something?)[/edit]
And DON'T make the standard I'm-Joe-I'm-Lazy argument. Part of what likely discourages you from completing your projects is that you get frustrated when you have to go back and correct things. Be un-lazy and do it right the first time and you don't need to spend time being consistent (like upgrading your code to take a 32-bit number).
Ergot, I wasn't trying to pick on you. It seems like you just need to experience better what the computer is capable of doing, because it looks like you're taking the long way to get where you're going, but only because you don't know any better.