Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6877825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:38:51+00:00 2026-05-27T04:38:51+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:38:51+00:00Added an answer on May 27, 2026 at 4:38 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm pretty sure this is a simple question in regards to formatting but here's
This is a pretty simple question but I'm somewhat stumped. I am capturing sections
I think this is a pretty simple question. How do you an apache rewrite
This is a pretty simple question.. lets say I have the following. wordArray WORD
pretty simple question I have here, but I couldn't find the answer: Assume I
This is pretty simple, I come from a swing/awt background. I'm just wondering what
This is pretty simple: I'm using NetBeans on Linux with Android emulator 1.6. I
Im sure this is pretty simple but I just cant seem to find the
this is probably pretty simple, but I've got this text file containing a bunch
This should be pretty simple, but I am new at LINQ. I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.