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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:11:52+00:00 2026-05-27T03:11:52+00:00

#include <iostream> using namespace std; void generCad(int n, char* cad){ int longi = 1,

  • 0
#include <iostream>
using namespace std;

void generCad(int n, char* cad){

int longi = 1, lastchar, m = n; // calculating lenght of binary string
char actual;
do{
    longi++;
    n /= 2;
}while(n/2 != 0);
cad = new char[longi];
lastchar = longi - 1;
do{
    actual = m % 2;
    cad[lastchar] = actual;
    m /= 2;
    lastchar--;
}while(m/2 != 0);
cout << "Cadena = " << cad;

}

Hi! I’m having a problem here because I need a function that creates a binary string for a number n. I think the process is “good” but cout doesn’t print anything, I don’t know how to fill the string I’ve created using the new operator

  • 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-27T03:11:53+00:00Added an answer on May 27, 2026 at 3:11 am

    The code should look like this:

    void generCad(int n, char** cad)
    {
        int m = n, c = 1;
    
        while (m >>= 1) // this divides the m by 2, but by shifting which is faster
            c++; // here you counts the bits
        *cad = new char[c + 1];
        (*cad)[c] = 0; // here you end the string by 0 character
    
        while (n)
        {
            (*cad)[--c] = n % 2 + '0';
            n /= 2;
        }
                cout << "Cadena = " << *cad;
    }
    

    Note that cad is now char ** and not char *. If it is just char * then you do not get the pointer as you expect outside the function. If you do not need the string outside this function, then it may be passed as char *, but then do not forget to delete the cad before you leave the function (good habit ;-))

    EDIT:

    This code will probably be more readable and do the same:

    char * toBin(int n)
    {
        int m = n, c = 1;
    
        while (m >>= 1) // this divides the m by 2, but by shifting which is faster
            c++; // here you counts the bits
        char *cad = new char[c + 1];
        cad[c] = 0; // here you end the string by 0 character
    
        while (n)
        {
            cad[--c] = n % 2 + '0';
            n /= 2;
        }
        cout << "Cadena = " << cad;
        return cad;
    }
    
    int main()
    {
        char *buff;
        buff = toBin(16);
    
        delete [] buff;
    
        return 1;
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include<iostream> using namespace std; class A { int a; int b; public: void eat()
#include <iostream> #include <vector> using namespace std; int main(void) { int i, s, g;
This very simple code: #include <iostream> using namespace std; void exec(char* option) { cout
#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; int main(void) {
i have following code in c++ #include <iostream> using namespace std; void qsort5(int a[],int
#include <iostream> using namespace std; struct Node { char item; Node *next; }; void
#include <iostream> using namespace std; void initializeMap(int mapSizeX, int mapSizeY, int map[][10]) { //
Given the following program, #include <iostream> using namespace std; void foo( char a[100] )
#include <iostream> using namespace std; int main (void) { cout << 1\t2\t3\t4\t5\t6\t7\t8\t9 << endl
#include <iostream> using namespace std; void main() { int i = 0; while (i

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.