Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <cstdlib>
- #include <cctype>
- #include <algorithm>
- #include <iomanip>
- using namespace std;
- //int binarytoDec_func(string);
- int main()
- {
- string stringChoice;
- string num;
- string binaryNum;
- int converted = 0;
- bool invalidinput;
- do{
- cout << "Which calculation would you like to perform?" << endl;
- cout << "Press 1 to convert a Binary Number to Decimal" << endl;
- cout << "Press 2 to convert a Decimal Number to Binary" << endl;
- cout << "Press 3 to exit" << endl;
- getline(cin, stringChoice);
- int choice = atoi(stringChoice.c_str);
- } while ((choice != 1) && (choice != 2) && (choice != 3));
- if (choice == 1)
- {
- do{
- invalidinput = false;
- cout << "Please input a binary number, 8 bits or less, to convert to decimal: " << endl;
- cin >> binaryNum;
- for (long i = 0; i < binaryNum.length(); i++)
- {
- char check = binaryNum[i];
- if (check == '0' || check == '1'){
- //int val = atoi(binaryNum.c_str());
- }
- else{
- cout << "Invalid input " << check << " is not a binary number" << endl;
- i = binaryNum.length();
- invalidinput = true;
- }
- }
- } while ((binaryNum.length() >= 8) || invalidinput == true);
- }
- if (choice == 2)
- {
- do{
- cout << "Please input a decimal number to convert to binary: " << endl;
- getline(cin, num);
- converted = atoi(num.c_str());
- if (converted == 0){
- cout << "This is not a valid number, please re-enter: ";
- getline(cin, num);
- }
- }while (converted == 0); //if converted is 0, loop again
- }
- if (choice == 3)
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement