Thread: Problem with program that uses a function to convert decimal values to binary values.

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    17

    Problem with program that uses a function to convert decimal values to binary values.

    The program works fine until I start using 6 digits or more, then I just get the first value of my binary number.
    I have the menu part in main because I'm planning on doing all the other conversions in different functions for example octal to hex or binary to hex.
    The switch statement is there to reverse the order of the numbers because the least value should be in the end not in the start like it would appear without the switch.
    I am grateful for any help I can get in advance ,thank you.



    Code:
    #include<iostream>
    
    
    using namespace std;
    
    
    int decimal_to_binary(int decb)
    {
         
        int counterb;  //The remainder varaible for the process  
        int round=0;  // To show the program where to put each number
        int h[5];
        int limit;  //To show when the loop should start or end so unused array slots won't be displayed
        int i;
        
    
    
    while(decb>0)
        {
            counterb=0;
            round++;
            
            if(decb%2!=0)  //Where the process of conversion to a binary number happens
            {
            counterb++;
            }
            else
            {
            counterb=0;
            }
    
    
            switch(round) //The process of putting each digit into it's coressponding memory slot
            {
            case 1:
                h[1]=counterb;
                limit=1;
                break;
            case 2:
                h[2]=counterb;
                limit=2;
                break;
            case 3:
                h[3]=counterb;
                limit=3;
                break;;
            case 4:
                h[4]=counterb;
                limit=4;
                break;
            case 5:
                h[5]=counterb;
                limit=5;
                break;
            case 6:
                h[6]=counterb;
                limit=6;
                break;
            case 7:
                h[7]=counterb;
                limit=7;
                break;
            case 8:
                h[8]=counterb;
                limit=8;
                break;
            case 9:
                h[9]=counterb;
                limit=9;
                break;
            case 10:
                h[10]=counterb;
                limit=10;
                break;
            default: cout<<"Error in array memory";
            }
        if(decb<=1) break;
            decb=decb/2;
    
    
    }
    for(i=limit;i>=1;i--)  //Displaying the digits form max value to least value e.g 100th 10th and so on
    cout<<h[i];
    
    
    
    
                
    
    
            return 1;
    }
    
    
    
    
    
    
        
    int main()
    {
        int choice; 
        cout<<"Welcome to the value converter"<<endl<<"1.Conversion from decimal to binary ."<<endl; //Menu for the other programs I'm working on
        cin>>choice;
    
    
        switch(choice)
        {
        case 1:
            {
                int dec;
                cout<<"Enter the number"<<endl;
                cin>>dec;
                decimal_to_binary(dec);
            }
    break;
        }
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    The program allocates space for 5 integers, but then uses 11 of them.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You need to allocate 10 ints for h.
    You need to index h from 0 to 9 (not from 1 to 10).
    And you can replace your switch with
    Code:
    h[round] = counterb;
    limit = round;
    (with some range-checking for round if necessary)

  4. #4
    Registered User
    Join Date
    Jun 2013
    Posts
    17
    Quote Originally Posted by oogabooga View Post
    You need to allocate 10 ints for h.
    You need to index h from 0 to 9 (not from 1 to 10).
    And you can replace your switch with
    Code:
    h[round] = counterb;
    limit = round;
    (with some range-checking for round if necessary)
    Thank you so much . I was actually looking for a way to do exactly what you just did, to have the index dependent on a variable but I didn't know how :S I'm still a beginner you know lol. I can't believe that I forgot that the indexing starts with 0! I adjusted the loops and some bits and it worked perfectly! Ok now I will try to readjust the code by removing the switch and using your way, I didn't know that it would be this simple,making the index dependent on a variable I mean, thank you again, I will post The new code when I'm done with it.

  5. #5
    Registered User
    Join Date
    Jun 2013
    Posts
    17
    Quote Originally Posted by rcgldr View Post
    The program allocates space for 5 integers, but then uses 11 of them.
    Thank you, I get it now .

  6. #6
    Registered User
    Join Date
    Jun 2013
    Posts
    17

    The new shorter code (thanks again guys)

    Code:
    #include<iostream>
    using namespace std;
    
    
    int decimal_to_binary(int decb)
    {
         
        int counterb;
        int round=0;
        int h[10];    //0-9 means 10 won't forget it again
        int limit=0;
        int i;
        
    
    
    while(1)
        {
            counterb=0;
            
            
            if(decb%2!=0)
            {
            counterb++;
            }
            else
            {
            counterb=0;
            }
    
    
            h[round] = counterb;   //Replaced the whole switch
            limit = round; //No range change was needed
    
    
        if(decb<=1) break;
            decb=decb/2;
    round++;
    
    
    }
    for(i=limit;i>=0;i--)
    cout<<h[i];
    
    
    
    
                
    
    
            return 1;
    }
    
    
    
    
    
    
        
    int main()
    {
        int choice;
        cout<<"Welcome to the value converter"<<endl<<"1.Conversion from decimal to binary ."<<endl;
        cin>>choice;
    
    
        switch(choice)
        {
        case 1:
            {
                int dec;
                cout<<"Enter the number"<<endl;
                cin>>dec;
                decimal_to_binary(dec);
            }
    break;
        }
    return 0;
    }

    It works perfectly now!

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    All that remains is to make it readable
    SourceForge.net: Indentation - cpwiki
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Check out this piece of art if you know some bit manipulations
    Code:
    #include <iostream>
    
    
     int  main()
     {
       int number;
     std :: cin >> number ;
     std :: cout << std :: endl;
     size_t no_of_bytes = sizeof( number );   
     size_t no_of_bits = 8 * no_of_bytes;
     std ::  cout <<" binary :  ";
     for(int k = no_of_bits - 1; k >= 0 ; --k) {
          if(number & (1 << k)) {
             std :: cout << "1";
          }
          else {
            std :: cout << "0";
          }
       }
       std :: cout << std :: endl;
    }

  9. #9
    Registered User
    Join Date
    Jun 2013
    Posts
    17
    Quote Originally Posted by Aslaville View Post
    Check out this piece of art if you know some bit manipulations
    Code:
    #include <iostream>
    
    
     int  main()
     {
       int number;
     std :: cin >> number ;
     std :: cout << std :: endl;
     size_t no_of_bytes = sizeof( number );   
     size_t no_of_bits = 8 * no_of_bytes;
     std ::  cout <<" binary :  ";
     for(int k = no_of_bits - 1; k >= 0 ; --k) {
          if(number & (1 << k)) {
             std :: cout << "1";
          }
          else {
            std :: cout << "0";
          }
       }
       std :: cout << std :: endl;
    }
    I barely got parts of that code . I'm still a beginner you know lol.

  10. #10
    Registered User
    Join Date
    Jun 2013
    Posts
    17
    Quote Originally Posted by Salem View Post
    All that remains is to make it readable
    SourceForge.net: Indentation - cpwiki
    I don't know what you mean. Would you please tell me what you mean by "readable"?

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Did you even click the link to read about what indentation means?

    Indentation means your code is arranged such that is represents the flow and scope of the code, like this.
    Code:
    #include<iostream>
    using namespace std;
    
    int decimal_to_binary(int decb)
    {
      int counterb;
      int round = 0;
      int h[10];                    //0-9 means 10 won't forget it again
      int limit = 0;
      int i;
    
      while (1) {
        counterb = 0;
    
        if (decb % 2 != 0) {
          counterb++;
        } else {
          counterb = 0;
        }
    
        h[round] = counterb;        //Replaced the whole switch
        limit = round;              //No range change was needed
    
        if (decb <= 1)
          break;
    
        decb = decb / 2;
        round++;
      }
    
      for (i = limit; i >= 0; i--)
        cout << h[i];
    
      return 1;
    }
    
    int main()
    {
      int choice;
    
      cout << "Welcome to the value converter" << endl 
           << "1.Conversion from decimal to binary ." << endl;
      cin >> choice;
    
      switch (choice) {
      case 1:
        {
          int dec;
          cout << "Enter the number" << endl;
          cin >> dec;
          decimal_to_binary(dec);
        }
        break;
      }
      return 0;
    }
    Take your decimal_to_binary() function as an example.
    In your poorly indented version, it's impossible to tell whether the for loop is inside or outside of the while loop, without careful counting of the braces.

    Whereas with good indentation by a programmer who cares about how the code looks to others, it's instantly obvious that the while loop and the for loop are separate.

    Not only does this benefit other readers, it also benefits you as well.

    Imagine now that your code was say 10x longer - how much harder would it be to understand what the code is doing?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    Jun 2013
    Posts
    17
    Quote Originally Posted by Salem View Post
    Did you even click the link to read about what indentation means?

    Indentation means your code is arranged such that is represents the flow and scope of the code, like this.
    Code:
    #include<iostream>
    using namespace std;
    
    int decimal_to_binary(int decb)
    {
      int counterb;
      int round = 0;
      int h[10];                    //0-9 means 10 won't forget it again
      int limit = 0;
      int i;
    
      while (1) {
        counterb = 0;
    
        if (decb % 2 != 0) {
          counterb++;
        } else {
          counterb = 0;
        }
    
        h[round] = counterb;        //Replaced the whole switch
        limit = round;              //No range change was needed
    
        if (decb <= 1)
          break;
    
        decb = decb / 2;
        round++;
      }
    
      for (i = limit; i >= 0; i--)
        cout << h[i];
    
      return 1;
    }
    
    int main()
    {
      int choice;
    
      cout << "Welcome to the value converter" << endl 
           << "1.Conversion from decimal to binary ." << endl;
      cin >> choice;
    
      switch (choice) {
      case 1:
        {
          int dec;
          cout << "Enter the number" << endl;
          cin >> dec;
          decimal_to_binary(dec);
        }
        break;
      }
      return 0;
    }
    Take your decimal_to_binary() function as an example.
    In your poorly indented version, it's impossible to tell whether the for loop is inside or outside of the while loop, without careful counting of the braces.

    Whereas with good indentation by a programmer who cares about how the code looks to others, it's instantly obvious that the while loop and the for loop are separate.

    Not only does this benefit other readers, it also benefits you as well.

    Imagine now that your code was say 10x longer - how much harder would it be to understand what the code is doing?


    Oh yes ok.I got it now. I thought the link was...never mind its just that a lot of members have like a link at the bottom of their comments that apparently aren't part of the comments ,give me a break man ,I'm new here lol. But I get it thank you for the information . Good Indentation = Great code .

  13. #13
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Think about this section more critically:
    Code:
        counterb = 0;
        if (decb % 2 != 0) {
          counterb++;
        } else {
          counterb = 0;
        }
    1) You set counterb to 0. Then, if false, what is the value of counterb? Do you need to set it to 0?
    2) What is the result of decb % 2? How can you use that knowledge?
    3) With the above, can you compact that code into something smaller?


    Next:
    Code:
      while(1)
      {
       ....
        if (decb <= 1)
          break;
      }
    Why an endless loop? Isn't the while loop essentially an if with a repeat? So why the explicit if? Let the while statement do it's job.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  14. #14
    Registered User
    Join Date
    Jun 2013
    Posts
    17
    Quote Originally Posted by WaltP View Post
    Think about this section more critically:
    Code:
        counterb = 0;
        if (decb % 2 != 0) {
          counterb++;
        } else {
          counterb = 0;
        }
    1) You set counterb to 0. Then, if false, what is the value of counterb? Do you need to set it to 0?
    2) What is the result of decb % 2? How can you use that knowledge?
    3) With the above, can you compact that code into something smaller?


    Next:
    Code:
      while(1)
      {
       ....
        if (decb <= 1)
          break;
      }
    Why an endless loop? Isn't the while loop essentially an if with a repeat? So why the explicit if? Let the while statement do it's job.

    Hmmm,you are absolutely right! Thank you . Normaly when I write the code I think of what will get the job done then I think maybe afterwards I work on making it compact and beautiful but I always seem to forget that last part :/. Anyways thank you a lot but if you may and I would understand if you couldn't but I have another post about a program that does the opposite of this program and I have hit a road-block,if you or anyone is able to help me I would be really grateful,any help what so ever on the problem would be amazing. Thanks again everyone for what you did already .


    The post's url: Problem with a binary to decimal conversion program. Help please.

    I have a 100 views but not even one single reply :/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c program-Return of two values from a global function.
    By mast3124mind in forum C Programming
    Replies: 4
    Last Post: 04-29-2013, 11:46 AM
  2. convert decimal to binary
    By tommytmh in forum C Programming
    Replies: 3
    Last Post: 11-24-2011, 06:38 AM
  3. C program to convert Hexadecimal values to Characters
    By ssurapaneni in forum C Programming
    Replies: 1
    Last Post: 08-06-2010, 04:32 PM
  4. Convert decimal to binary
    By planet_abhi in forum C Programming
    Replies: 1
    Last Post: 12-20-2002, 04:47 AM
  5. Replies: 10
    Last Post: 06-12-2002, 03:15 PM