Advertisement
Guest User

Untitled

a guest
Mar 31st, 2013
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. /*
  2. Program name: binarycalc.cpp
  3. Created: 3-30-13
  4. Created by: Dan Hogan
  5. Purpose: Takes 8 inputted bits of binary and converts it to a decimal
  6. */
  7.  
  8. #include<iostream>
  9. #include<string>
  10. #include<iomanip>
  11.  
  12. using namespace std;
  13. int main ()
  14. {
  15. // Welcome screen
  16. string binary;
  17. int answer;
  18. cout << "**************************************************" << endl;
  19. cout << "* Welcome to the Binary Calculator! *" << endl;
  20. cout << "* Made for people who don't want to do the math! *" << endl;
  21. cout << "* Created by Dan Hogan *" << endl;
  22. cout << "**************************************************" << endl << endl << endl;
  23.  
  24. cout << "Enter 8 bits for conversion. Entering anything other than a 1 or 0 entered will cause an incorrect answer." << endl;
  25. cin >> binary;
  26.  
  27. // Will check if the user inputs 8 numbers
  28. if(binary.length() != 8)
  29. {
  30. cout << "You did not enter 8 numbers, Invalid number." << endl;
  31. exit(0);
  32. }
  33. answer=0;
  34. // Begin adding
  35. if(binary[0]=='1')
  36. {
  37. answer= answer + 128;
  38. }
  39. if(binary[1]=='1')
  40. {
  41. answer= answer + 64;
  42. }
  43. if(binary[2]=='1')
  44. {
  45. answer= answer + 32;
  46. }
  47. if(binary[3]=='1')
  48. {
  49. answer= answer + 16;
  50. }
  51. if(binary[4]=='1')
  52. {
  53. answer= answer + 8;
  54. }
  55. if(binary[5]=='1')
  56. {
  57. answer= answer + 4;
  58. }
  59. if(binary[6]=='1')
  60. {
  61. answer= answer + 2;
  62. }
  63. if(binary[7]=='1')
  64. {
  65. answer= answer + 1;
  66. }
  67. // Begin output screen
  68. cout << endl;
  69. cout << endl;
  70. cout << endl;
  71. cout << setw(10) << "You entered: " << binary << endl;
  72. cout << setw(10) << "The decimal equivelant of this is: " << answer << endl;
  73. // Thank you message
  74. cout << setw(10) << "Thanks for using!" << endl;
  75. return 0;
  76. }
  77. // End program
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement