Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 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. string stringChoice;
  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. getline(cin, stringChoice);
  25. int choice = atoi(stringChoice.c_str);
  26. } while ((choice != 1) && (choice != 2) && (choice != 3));
  27.  
  28. if (choice == 1)
  29. {
  30. do{
  31. invalidinput = false;
  32. cout << "Please input a binary number, 8 bits or less, to convert to decimal: " << endl;
  33. cin >> binaryNum;
  34.  
  35. for (long i = 0; i < binaryNum.length(); i++)
  36. {
  37. char check = binaryNum[i];
  38.  
  39. if (check == '0' || check == '1'){
  40. //int val = atoi(binaryNum.c_str());
  41.  
  42. }
  43. else{
  44. cout << "Invalid input " << check << " is not a binary number" << endl;
  45. i = binaryNum.length();
  46. invalidinput = true;
  47. }
  48. }
  49. } while ((binaryNum.length() >= 8) || invalidinput == true);
  50. }
  51.  
  52. if (choice == 2)
  53. {
  54. do{
  55. cout << "Please input a decimal number to convert to binary: " << endl;
  56. getline(cin, num);
  57. converted = atoi(num.c_str());
  58. if (converted == 0){
  59. cout << "This is not a valid number, please re-enter: ";
  60. getline(cin, num);
  61. }
  62. }while (converted == 0); //if converted is 0, loop again
  63. }
  64.  
  65. if (choice == 3)
  66. return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement