Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Program name: binarycalc.cpp
- Created: 3-30-13
- Created by: Dan Hogan
- Purpose: Takes 8 inputted bits of binary and converts it to a decimal
- */
- #include<iostream>
- #include<string>
- #include<iomanip>
- using namespace std;
- int main ()
- {
- // Welcome screen
- string binary;
- int answer;
- cout << "**************************************************" << endl;
- cout << "* Welcome to the Binary Calculator! *" << endl;
- cout << "* Made for people who don't want to do the math! *" << endl;
- cout << "* Created by Dan Hogan *" << endl;
- cout << "**************************************************" << endl << endl << endl;
- cout << "Enter 8 bits for conversion. Entering anything other than a 1 or 0 entered will cause an incorrect answer." << endl;
- cin >> binary;
- // Will check if the user inputs 8 numbers
- if(binary.length() != 8)
- {
- cout << "You did not enter 8 numbers, Invalid number." << endl;
- exit(0);
- }
- answer=0;
- // Begin adding
- if(binary[0]=='1')
- {
- answer= answer + 128;
- }
- if(binary[1]=='1')
- {
- answer= answer + 64;
- }
- if(binary[2]=='1')
- {
- answer= answer + 32;
- }
- if(binary[3]=='1')
- {
- answer= answer + 16;
- }
- if(binary[4]=='1')
- {
- answer= answer + 8;
- }
- if(binary[5]=='1')
- {
- answer= answer + 4;
- }
- if(binary[6]=='1')
- {
- answer= answer + 2;
- }
- if(binary[7]=='1')
- {
- answer= answer + 1;
- }
- // Begin output screen
- cout << endl;
- cout << endl;
- cout << endl;
- cout << setw(10) << "You entered: " << binary << endl;
- cout << setw(10) << "The decimal equivelant of this is: " << answer << endl;
- // Thank you message
- cout << setw(10) << "Thanks for using!" << endl;
- return 0;
- }
- // End program
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement