Hello all,
I've got a question regarding a programming assignment that is due by midnight. The objective is to create a program that converts a binary number (up to 32 bits long) to decimal. The problems I have run up against thus far are:
1) Without the error checking for an entry being 1 or 0, I have a problem getting the original binary number to be displayed on the "____ in binary is ____ in decimal." line.
2) With error checking, it keeps automatically passing by the binary number entry line and saying that it is not a valid binary number.
Here is my current code:
Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char entry[32];
char decision, exit_option;
int place = 0, digit, decimal = 0;
int n = 0, power = 1;
cout << "This program converts positive integer binary numbers"
<< " (up to 32 bits) into a decimal number.\n\n"
<< "Would you like to convert a binary number? (Y or y): ";
cin >> decision;
exit_option = 'Y';
while((exit_option == 'Y')||(exit_option == 'y'))
{
if((decision == 'Y')||(decision == 'y'))
{
cout << "\n\nPlease enter your binary number:\n\n";
cin >> entry[0];
while (entry[place] != '\n')
{
place ++;
cin >> noskipws >> entry[place];
}
if((entry[place] != 1)||(entry[place] != 0))
{
cout << "\n\nThis is not a binary number. Try again? ";
cin >> exit_option;
}
else
{
for(digit = place-1; digit >= 0; digit --)
{
decimal += (entry[digit] - '0')*power;
power *= 2;
}
cout << entry[place] << "in binary is " << decimal << " in decimal.\n\n";
cout << "Continue? (Y or y): ";
cin >> exit_option;
}
}
else
{
cout << "Thanks anyway!";
exit_option = 'n';
}
}
cout << "\n\nThank you for using the Binary Number Convertor!\n\n";
return 0;
}
Thanks for any help/advice given.