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 02:48 PM.
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.
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 05:08 PM.
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.
Bookmarks