-
October 23rd, 2009, 06:27 PM
#1
Issue with Binary to Decimal
I wrote a program to try and convert Binary strings to Decimal and output it.
For some reason, no matter what I type in, it comes up with "1".
For example, "01100001" should produce "96", since 96 is 01100001 in Decimal.
Can somebody tell me what is wrong with my code?
Code:
// Binary to Decimal Converter. Programmed by Darin Beaudreau.
#include <iostream> // Include input/output stream header.
#include <cstdlib> // Include the C standard library header.
using namespace std; // Use the standard namespace.
char chUserInput[8];
int decValue = 0;
int count;
void checkValue() {
if(chUserInput[1] == 1) {
decValue = decValue+128;
}
if(chUserInput[2] == 1) {
decValue = decValue+64;
}
if(chUserInput[3] == 1) {
decValue = decValue+32;
}
if(chUserInput[4] == 1) {
decValue = decValue+16;
}
if(chUserInput[5] == 1) {
decValue = decValue+8;
}
if(chUserInput[6] == 1) {
decValue = decValue+4;
}
if(chUserInput[7] == 1) {
decValue = decValue+2;
}
if(chUserInput[8] == 1) {
decValue = decValue+1;
}
}
void addInput() {
int place = 1;
for(count=1; count<=8; count++) {
checkValue();
place++;
count++;
}
}
int main() {
cout << "Welcome to Darin's Binary to Decimal Converter...\n";
cout << "Enter a binary string (8 digits) to convert to decimal...\n" << endl;
cout << "Binary: ";
cin >> chUserInput;
addInput();
cout << "\nDecimal: " << decValue << "\n\n";
system("PAUSE");
return 0;
}
-
October 23rd, 2009, 07:30 PM
#2
Re: Issue with Binary to Decimal
You're iterating from 1 when you should start from 0.
Also, C++ has a function for this:
Code:
int i=wcstol(L"01100001",NULL,2);
-
October 23rd, 2009, 07:43 PM
#3
Re: Issue with Binary to Decimal
Closer examination reveals that, your comparison should be like this:
Code:
if(chUserInput[0] == '1') { //char in single quotes (or L'1' for UNICODE)
and you don't need to loop. Just call CheckValue() once.
Here's your code modified:
Code:
#include <iostream> // Include input/output stream header.
#include <cstdlib> // Include the C standard library header.
using namespace std; // Use the standard namespace.
char chUserInput[8];
int decValue = 0;
int count;
void checkValue() {
if(chUserInput[0] == '1') {
decValue = decValue+128;
}
if(chUserInput[1] == '1') {
decValue = decValue+64;
}
if(chUserInput[2] == '1') {
decValue = decValue+32;
}
if(chUserInput[3] == '1') {
decValue = decValue+16;
}
if(chUserInput[4] == '1') {
decValue = decValue+8;
}
if(chUserInput[5] == '1') {
decValue = decValue+4;
}
if(chUserInput[6] == '1') {
decValue = decValue+2;
}
if(chUserInput[7] == '1') {
decValue = decValue+1;
}
}
int main() {
cout << "Welcome to Darin's Binary to Decimal Converter...\n";
cout << "Enter a binary string (8 digits) to convert to decimal...\n" << endl;
cout << "Binary: ";
cin >> chUserInput;
checkValue();
cout << "\nDecimal: " << decValue << "\n\n";
system("PAUSE");
return 0;
}
-
October 23rd, 2009, 08:38 PM
#4
Re: Issue with Binary to Decimal
Now it's coming up as 0... didn't fix anything, but I guess I didn't need that extra function in there. Anybody got any ideas now? Here's the current code.
Code:
// Binary to Decimal Converter by packetpirate.
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
char chUserInput[8];
int decValue;
void checkValue() {
if(chUserInput[0] == 1) {
decValue = decValue+128;
}
if(chUserInput[1] == 1) {
decValue = decValue+64;
}
if(chUserInput[2] == 1) {
decValue = decValue+32;
}
if(chUserInput[3] == 1) {
decValue = decValue+16;
}
if(chUserInput[4] == 1) {
decValue = decValue+8;
}
if(chUserInput[5] == 1) {
decValue = decValue+4;
}
if(chUserInput[6] == 1) {
decValue = decValue+2;
}
if(chUserInput[7] == 1) {
decValue = decValue+1;
}
}
int main() {
cout << "Welcome to packetpirate's Binary to Decimal Converter...\n" << endl;
cout << "Enter a binary string (8 digits) to convert to Decimal...\n\n";
cout << "Binary: ";
cin >> chUserInput;
checkValue();
cout << "Decimal: " << decValue << "\n\n";
system("PAUSE");
return 0;
}
EDIT: WOW I'M AN IDIOT... Forgot to call the function after taking the input...
EDIT 2: Still doesn't work
Last edited by packetpirate; October 23rd, 2009 at 08:46 PM.
-
October 23rd, 2009, 10:36 PM
#5
Re: Issue with Binary to Decimal
You didn't notice my change:
Code:
if(chUserInput[0] == 1) {
if(chUserInput[0] == '1') {
-
October 23rd, 2009, 11:15 PM
#6
Re: Issue with Binary to Decimal
Ah... let me try that now.
EDIT: AWESOME! It works now!
Last edited by packetpirate; October 23rd, 2009 at 11:18 PM.
-
October 24th, 2009, 12:44 AM
#7
Re: Issue with Binary to Decimal
That checkValue() function could be significantly shortened with the use of a loop.
-
October 24th, 2009, 10:01 AM
#8
Re: Issue with Binary to Decimal
I couldn't figure out a way how... I know I could probably use a counter, but how? Can you show me how you would do it?
-
October 24th, 2009, 10:41 AM
#9
Re: Issue with Binary to Decimal
Originally Posted by Lindley
That checkValue() function could be significantly shortened with the use of a loop.
As noted in my first reply, the whole program can be shortened to one line.
-
October 26th, 2009, 09:33 AM
#10
Re: Issue with Binary to Decimal
Originally Posted by packetpirate
I wrote a program to try and convert Binary strings to Decimal and output it.
If this is not a school assignment, the bitset class does this for you already.
Code:
#include <bitset>
#include <string>
#include <iostream>
using namespace std;
int main()
{
string inStr = "10010011";
cout << "Decimal value of " << inStr << " is " << bitset<64>(inStr).to_ulong();
}
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|