CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Oct 2009
    Posts
    15

    Question 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;
    }

  2. #2
    Join Date
    Feb 2005
    Posts
    2,160

    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);

  3. #3
    Join Date
    Feb 2005
    Posts
    2,160

    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;
    }

  4. #4
    Join Date
    Oct 2009
    Posts
    15

    Unhappy 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.

  5. #5
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Issue with Binary to Decimal

    You didn't notice my change:

    Code:
    if(chUserInput[0] == 1) {
    if(chUserInput[0] == '1') {

  6. #6
    Join Date
    Oct 2009
    Posts
    15

    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.

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Issue with Binary to Decimal

    That checkValue() function could be significantly shortened with the use of a loop.

  8. #8
    Join Date
    Oct 2009
    Posts
    15

    Unhappy 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?

  9. #9
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Issue with Binary to Decimal

    Quote Originally Posted by Lindley View Post
    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.

  10. #10
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Issue with Binary to Decimal

    Quote Originally Posted by packetpirate View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured