1

This is a pretty simple question; first time poster and long time looker.

Here is my binary to decimal converter I wrote:

#include <iostream>
#include <cmath>
using namespace std;
const int MAX = 6;
int conv(int z[MAX], int l[6], int MAX);

int main()
{
    int zelda[MAX];
    const int d = 6;
    int link[d];

    cout << "Enter a binary number: \n";  
    int i = 0;
    while (i < MAX && (cin >> zelda[i]).get())  //input loop
    {
        ++i;
    }   

    cout << conv(zelda, link, MAX);

    cin.get();
    return  0;
}

int conv(int zelda[MAX], int link[6], int MAX)
{   
    int sum = 0;
    for (int t = 0; t < MAX; t++)
    {
        long int h, i;
        for (int h = 5, i = 0; h >= 0; --h, ++i)
            if (zelda[t] == 1)
                link[h] = pow(2.0, i);
            else
                link[h] = 0;
            sum += link[t]; 
    }
    return sum;
}

With the way the input loop is being handled, I have to press enter after each input of a number. I haven't added any error correction yet either (and some of my variables are vague), but would like to enter a binary say 111111 instead of 1 enter, 1 enter, 1 enter, etc to fill the array. I am open to any technique and other suggestions. Maybe input it as a string and convert it to an int?

I will keep researching. Thanks.

5
  • 7
    Your variable naming is most intriguing. Commented Nov 25, 2011 at 1:29
  • 1
    Your entire input logic is very obscure. Why don't you simply read one string, expected to consist of 1s and 0s only, and convert that? Commented Nov 25, 2011 at 1:32
  • 1
    Best variable nomenclature, EVAR :) (Had to post this comment :)) Commented Nov 25, 2011 at 2:04
  • Thanks fellow coders. I honestly didn't give it much thought -_- Commented Nov 25, 2011 at 2:29
  • You should be returning your sum in masterSword silly. Commented Nov 26, 2011 at 17:59

2 Answers 2

2

To read data, see this related question (and replace the file stream by std::cin).

To convert, you can do something simple:

unsigned int convert(const std::string & s)
{
  // maybe check that s.size() <= CHAR_BIT * sizeof(unsigned int)

  unsigned int result = 0;

  for (std::string::const_reverse_iterator rit = s.rbegin(), rend = s.rend(); rit != rend; ++rit)
  {
    result *= 2;

    if (*rit == '1') ++result;

    else if (*rit != '0') { /*error!*/ return -1; }
  }

  return result;
}
Sign up to request clarification or add additional context in comments.

1 Comment

I will read that link and get another angle on how to tackle this. It appears that entering as a string is the best way.
1

You could read an int and parse it this way:

int number = 0;

cin >> number;
int i = 0;
while(i < MAX)
{
    if(number > 0)
    {
        zelda[i] = number % 10; // or you can switch to zelda[MAX-(i+1)]
        number = number/10;
    else
    {
        zelda[i] = 0;
    }
    i++;
}

EDIT:
Note that this conversion is in little endian format, meaning that if you type the int '100', zelday will be filled with '001'.

EDIT2:
If you want to get it from a string instead, do this, assuming zelda has the same size of str:

string str ("111000");
int i;
for (i=0; i < str.length(); i++)
{
    zelda[i] = (str[i] - '0');
}

Reason why this works:
The numbers represented in a char list are sequential (int this case ASCII), that is, the number zero is represented as 48, the number one is represented by 49, and so on. So when you subtract the representation of the '0', you get the actual number.

10 Comments

I like this method, but I might have to work around the endian format. Definitely some ideas.
Switch the commented version zelda[MAX-(i+1)] instead of zelda[i], and you switch around.
This is all great. How do I boost your reputation? I don't know if I can at my low rank.
Just click the up arrow to upvote the answer and then click the V mark to accept it, all users can cast upvotes and accept answers.
Ah I tried that just now, but I need 15 rep. Figuring the inner workings of this forum still.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.