Newbie questions about bloated binary to decimal converter
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Newbie questions about bloated binary to decimal converter

  1. #1
    Join Date
    Apr 2012
    Posts
    4

    Newbie questions about bloated binary to decimal converter

    Hi all, I'm new to programming and I'm having some minor difficulties with a console app I've made to convert a given binary number into decimal. This program is probably ugly and ultra bloated, so I would gladly accept advice on that front, but keep in mind that we're only using pretty basic stuff. We're not even allowed to use strings.

    Basically I have 2 questions.
    1. Because of how limited integers are in terms of storage, the largest binary number i can give seems to be 1 111 111 111. Anything larger breaks the program. Is there any way to increase the largest input I can give without completely rewriting the program? I tried changing the num/numCounter (and the typecasting) to long doubles in a blind hope that that would, help, but it just messed stuff up (or I did).

    2. I'd also like to make it so that if someone inputs a non-binary number my program tells them so and stops. I figured a switch statement within the while loop would work (for when numCounter/divisor is negative or greater than 1), but I was wondering if instead it was possible to use an if statement that could break the while loop? Any other suggestions?

    Anyway, here's the code:
    Code:
    #include <QtCore/QCoreApplication>
    #include <iostream>
    #include <cstdlib>
    #include <iomanip>
    #include <cmath>
    
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        char again;
        cout<<"This program will convert a binary number into decimal form."<<endl<<endl;
        do
        {
            int numDigits, divisor, ans=0;
            int num, numCounter;
            cout<<"Please enter the binary number."<<endl;
            cin>>num;
            numCounter = num;
            numDigits = static_cast<int>(1 + log10((static_cast<double>(num))));
            divisor = static_cast<int>(pow(10,(static_cast<double>(numDigits) - 1)));
            while(divisor >= 1)
            {
                if((numCounter/divisor)==1)
                {
                    ans = ans + pow(2,(static_cast<double>(numDigits)-1));
                    numCounter -= divisor;
                }
                divisor /= 10;
                numDigits -= 1;
            }
            cout<<endl<<num<<" in decimal form is "<<ans<<endl<<endl
                <<"Convert another number?  (Y)es or (N)o."<<endl;
            cin>>again;
            cout<<endl<<endl<<endl;
        }while(toupper(again) == 'Y');
        return a.exec();
    }
    Thanks to anyone who bothered to look at this. I'm sure it's an eyesore.
    Last edited by getajob92; April 18th, 2012 at 03:48 PM.

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,232

    Re: Newbie questions about bloated binary to decimal converter

    [] are used to wrap the code tags, not <>

    If you want a bigger binary number, read it into a char array or string.

    I'd just use an if statement to determine if each character was a 1 or a 0 and return an error if it isn't.
    Last edited by GCDEF; April 18th, 2012 at 03:01 PM.

  3. #3
    Join Date
    Apr 2012
    Posts
    4

    Re: Newbie questions about bloated binary to decimal converter

    I'm not allowed to use to use strings, and I've never used an array before.

    Thanks to whoever gave me editing power!
    Last edited by getajob92; April 18th, 2012 at 03:50 PM. Reason: Able to edit now

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,232

    Re: Newbie questions about bloated binary to decimal converter

    Can't you edit your post?

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,893

    Re: Newbie questions about bloated binary to decimal converter

    The only way to support a longer input without using some kind of string (either a string class or a char array) would be to use a larger integer type. Try "long long", which is 64 bits on most machines.

  6. #6
    Join Date
    Apr 2012
    Posts
    4

    Re: Newbie questions about bloated binary to decimal converter

    Can't you edit your post?
    Nope :-(. Even in my "Posting Rules" it says "You may not edit your posts."

    -Edit- Now I can! Dunno why I couldn't before, but all good now!

    The only way to support a longer input without using some kind of string (either a string class or a char array) would be to use a larger integer type. Try "long long", which is 64 bits on most machines.
    I'll try that and get back right away!
    Last edited by getajob92; April 18th, 2012 at 06:08 PM.

  7. #7
    Join Date
    Jan 2009
    Posts
    596

    Re: Newbie questions about bloated binary to decimal converter

    There is no need to read the entire input into either a string, array or int before processing it. You can simply treat the input as a 'stream' of characters, and do whatever processing you need to do upon reading each one.

    Outline code for this would be:
    Code:
    do {
    	const int c = getchar(); // Get the next character from the input
    
    	// Take the appropriate steps depending upon this character
    	// I.e. if it is EOF or newline, stop the loop
    
    	// If it is not either 0 or 1, report an error
    	// (or set an error code for later code to check)
    
    	// If it is 0 or 1, use it to build up the number
    } while (true);
    It would also be a good idea to check that the user isn't entering so many digits that the number would overflow whatever int type you use to store the number.

  8. #8
    Join Date
    Apr 2012
    Posts
    4

    Re: Newbie questions about bloated binary to decimal converter

    I tried long long, but anything past 10 1s would still break the program. It didn't, however, crash the console, so that was a start.

    I've never used getchar before, but I know it's incredibly useful so I'll play around with it and get back to ya.

    And yeah, I'll probably figure out a way to stop them from causing the overflow.
    Last edited by getajob92; April 18th, 2012 at 06:07 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center