Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cstdlib>
  4. #include <cctype>
  5. #include <algorithm>
  6. #include <iomanip>
  7. using namespace std;
  8.  
  9. //int binarytoDec_func(string);
  10.  
  11. int main()
  12. {
  13. int choice;
  14. string num;
  15. string binaryNum;
  16. int converted = 0;
  17. bool invalidinput;
  18.  
  19. do{
  20. cout << "Which calculation would you like to perform?" << endl;
  21. cout << "Press 1 to convert a Binary Number to Decimal" << endl;
  22. cout << "Press 2 to convert a Decimal Number to Binary" << endl;
  23. cout << "Press 3 to exit" << endl;
  24. cin >> choice;
  25. } while ((choice != 1) && (choice != 2) && (choice != 3));
  26.  
  27. if (choice == 1)
  28. {
  29. do{
  30. invalidinput = false;
  31. cout << "Please input a binary number, 8 bits or less, to convert to decimal: " << endl;
  32. cin >> binaryNum;
  33.  
  34. for (long i = 0; i < binaryNum.length(); i++)
  35. {
  36. char check = binaryNum[i];
  37.  
  38. if (check == '0' || check == '1'){
  39. //int val = atoi(binaryNum.c_str());
  40.  
  41. }
  42. else{
  43. cout << "Invalid input " << check << " is not a binary number" << endl;
  44. i = binaryNum.length();
  45. invalidinput = true;
  46. }
  47. }
  48. } while ((binaryNum.length() >= 8) || invalidinput == true);
  49. }
  50.  
  51. if (choice == 2)
  52. {
  53. do{
  54. cout << "Please input a decimal number to convert to binary: " << endl;
  55. getline(cin, num);
  56. converted = atoi(num.c_str());
  57. if (converted == 0){
  58. cout << "This is not a valid number, please re-enter: ";
  59. getline(cin, num);
  60. }
  61. }while (converted == 0); //if converted is 0, loop again
  62. }
  63.  
  64. if (choice == 3)
  65. return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement