Thread: decimal to binary

  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    5

    decimal to binary

    I had an exercise to convert from decimal to binary with base 2, it was simple simple i did the following:
    Code:
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    
    
    using namespace std;
    void Conversion (int n);
    int main () {
    				int n; 
    				n=-1;
    				
    				
    				cout<< "Please enter a positiover integer";
    				do{
    				if(!(cin>>n)){
    					cin.clear();
                	cin.ignore(255, '\n');
            	}
    			if ( n<0 ){
    			cout<<"error, Please enter positive integer."<<endl;
    			}
    			}while ( n<0 );
    
    
    			cout << n<<
    			 " converted to binary is "<<endl;
    			Conversion(n);
    			
    
    
    }void Conversion(int n) {
    using namespace std;
    	float remainder;
    
    
    	if(n <= 1) {
    		cout << n;
    		return ;
    	}
    
    
    	remainder = n%2;
    	Conversion(n >> 1);    
    	cout << remainder;
    	}
    I then had an follow up exercise which was to replicate but for any base up to 10, i thought i would just have to replace 2 with a variable obtained from the user, however this did not work as i got an error saying too few arguments function but i cannot see why i am getting this.
    Code:
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    using namespace std;
    float Conversion (int n, int b);
    int main () {
    	int n;
    	n=-1;
    	int b;
    	b=0;
    	cout<< "Please enter a positiover integer";
    	do{
    		if (!(cin>>n)){
    			cin.clear();
    			cin.ignore(255, '\n');
    		}
    		if ( n<0 ){
    			cout<<"error, Please enter positive integer."<<endl;
    		}
    	}while ( n<0 );
    	cout <<"please enter the base";
    	do{
    		if (!(cin>>b)){
    			cin.clear();
    			cin.ignore(255, '\n');
    		}
    		if (b<2 || b>10){
    			cout<<"error please enter a number 1-10";
    		}
    	}while (b<2 ||  b>10);
    	cout << n<<
    	" converted to binary is "<<endl;
    	Conversion(n,b);
    }
    float Conversion(int  n,int b) {
    	using namespace std;
    	float remainder;
    	;
    	if (n <= 1) {
    		cout << n;
    		return 0 ;
    	}
    	remainder = n%b;
    	Conversion(n >> 1);
    	cout << remainder;
    }
    any help will be greatly appreciated, thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    For starters, you need to indent your code properly. For example:
    Code:
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    
    using namespace std;
    
    float Conversion(int n, int b);
    
    int main() {
        int n;
        n = -1;
        int b;
        b = 0;
    
        cout << "Please enter a positiover integer";
        do {
            if (!(cin >> n)) {
                cin.clear();
                cin.ignore(255, '\n');
            }
            if (n < 0) {
                cout << "error, Please enter positive integer." << endl;
            }
        } while (n < 0);
    
        cout << "please enter the base";
        do {
            if (!(cin >> b)) {
                cin.clear();
                cin.ignore(255, '\n');
            }
            if (b < 2 || b > 10) {
                cout << "error please enter a number 1-10";
            }
        } while (b < 2 || b > 10);
    
        cout << n << " converted to binary is " << endl;
        Conversion(n, b);
    }
    
    float Conversion(int n, int b) {
        using namespace std;
    
        float remainder;
        ;
        if (n <= 1) {
            cout << n;
            return 0;
        }
        remainder = n % b;
        Conversion(n >> 1);
        cout << remainder;
    }
    A few things to note:
    • Don't do this:
      Code:
      int n;
      n = -1;
      Rather, initialise:
      Code:
      int n = -1;
    • Instead of b, base would be a more descriptive name.
    • You already have the using directive using namespace std; earlier, so it should not be present in Conversion. Alternatively, you can move the previous using directive to the start of the main function.
    • This is an empty statement that should be removed:
      Code:
      ;
    • You forgot to change the call to Conversion here:
      Code:
      Conversion(n >> 1);
      This is the source of the problem that you are asking about.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2014
    Posts
    5
    Thanks for the reply, i can now run the program, however, it now just crashes after entering the base, is this a fundamental flaw in the idea i.e. i cannot just replace the 2 with a variable? as with 2 the remainder can only be 1 whereas with other numbers the remainder is more than 1

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Trace through your program. In particular, note that you check for n <= 1 in Conversion. Does this still apply when the base is not necessarily binary?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2014
    Posts
    5
    I've tried messing around with the code, and i tried n<base in the conversion as now it is not binary, but i still get a crash, i think it might be in the conversion(n,base>> _) part as i am unsure, I've tried numerous different approaches and still no success.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You need to learn how to use a debugger.
    Go install Visual Studio (there are free editions), then use this article to help you get started.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Decimal to Binary
    By Ducky in forum C++ Programming
    Replies: 18
    Last Post: 04-23-2009, 09:48 AM
  2. binary to decimal
    By rs07 in forum C Programming
    Replies: 3
    Last Post: 09-14-2008, 12:52 PM
  3. binary to decimal
    By owi_just in forum Tech Board
    Replies: 2
    Last Post: 04-02-2005, 10:24 AM
  4. decimal to binary
    By bugeye in forum C Programming
    Replies: 5
    Last Post: 03-13-2002, 11:50 AM
  5. decimal to binary, decimal to hexadecimal and vice versa
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 12-08-2001, 11:07 PM