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 6884395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:30:09+00:00 2026-05-27T05:30:09+00:00

To practice using pointers and arrays i’m trying to do a simple program capable

  • 0

To practice using pointers and arrays i’m trying to do a simple program capable of converting a binary input to denary.. i think i have a good idea for the logic but i haven’t even got round to trying to implement it because im struggling to get my for loop running!

It seems silly but i know the code inside the for loop works fine outside of it, so it must be something wrong with the condition..? im trying to start at the back of the char array (navigating using pointers) and output each char(as an int) up to the first element.

So the desired output is “0 – 1 – 0 – 1 -“

#include <iostream>

using std::cout;
using std::endl;

//prototypes
void binaryToDenary(const char* input, int& inputLength);

int main(){

    const char binaryInput[] = {1,0,1,0};
    int inputLength = sizeof(binaryInput)/sizeof(binaryInput[0]);

    binaryToDenary(binaryInput, inputLength);

    return 0;
}
void binaryToDenary(const char* input, int& inputLength){
    //testing some stuff--- this all works as expected
    //cout << input[2] << " " << (int)*(input+2) << " " << inputLength <<endl;

    int i;
    for(i = inputLength; i < 0; i--){
        cout << (int)*(input+i) << " - ";
    }

}
  • 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-27T05:30:10+00:00Added an answer on May 27, 2026 at 5:30 am

    Your for loop should be this:

    for(i = inputLength -1 ; i  >= 0; i--)
    {
        cout << (int)*(input+i) << " - ";
    }
    

    There are two problems in your code:

    • i = inputLength which should be i = inputLength -1
    • i < 0 which should be i >= 0

    Also, change the second parameter type from int & to int:

    void binaryToDenary(const char* input, int inputLength) //now its better!
    

    The type int& reduces the use cases, and benefits almost nothing. If you use int &, then all of these would give compilation error:

    const int inputLength = sizeof(binaryInput)/sizeof(binaryInput[0]);
    ^^^^ note this
    
    binaryToDenary(binaryInput, inputLength); //compilation error
    binaryToDenary(binaryInput, sizeof(binaryInput)/sizeof(binaryInput[0])); //error
    binaryToDenary(binaryInput, 4); ////compilation error
    

    So use int, and all of the above would compile fine!

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

Sidebar

Related Questions

I've written a simple string tokenizing program using pointers for a recent school project.
I'm trying to do something simple to practice my Javascript (which I learned some
Just trying to figure out what's the best practice: when using method that takes
I wanted to create simple plugin using jquery. Also suggest me standard practice while
What's the best practice for using a switch statement vs using an if statement
What's the best practice for using ASP.NET expressions inside an external Javascript file? I'm
What is the best practice for using C# as an embedded internal scripting application
I would like to know what is the best practice between using bind tag
I'm new to C#, and am curious about best practice for using namespaces. I
what is the preferred practice when linq2sql using (in asp.net mvc applications): to create

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.