Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- #include <cmath>
- using namespace std;
- int strToDecimal(int base, string num);
- string decimalToBase(int number, int base);
- int main()
- {
- int menuChoice;
- int startBase;
- int numberInDecimal;
- string number;
- cout << "NUMBER BASE CONVERTER 2019" << endl;
- cout << "By: Suraj Joshi" << endl << endl;
- cout << "Enter number to convert: ";
- cin >> number;
- cout << "Choose your starting base: " << endl
- << "1. Binary" << endl
- << "2. Octal" << endl
- << "3. Decimal" << endl
- << "4. Hex" << endl;
- cin >> menuChoice;
- switch(menuChoice)
- {
- case 1:
- {
- startBase = 2;
- break;
- }
- case 2:
- {
- startBase = 8;
- break;
- }
- case 3:
- {
- startBase = 10;
- break;
- }
- case 4:
- {
- startBase = 16;
- break;
- }
- default:
- {
- startBase = 0;
- cout << "Invalid Choice..." << endl;
- break;
- }
- }
- numberInDecimal = strToDecimal(startBase, number);
- cout << "Binary: " << decimalToBase(numberInDecimal, 2) << endl;
- cout << "Decimal: " << numberInDecimal << endl;
- cout << "Octal: " << decimalToBase(numberInDecimal, 8) << endl;
- cout << "Hex: " << decimalToBase(numberInDecimal, 16) << endl;
- return 0;
- }
- // This is where the problem starts(I Believe) I never experianced the problem when this wasnt here
- int strToDecimal(int base, string num)
- {
- int sum = 0;
- for (int i = 0; num.length() - 1; ++i) {
- if(num[i] > 64)
- sum += (num[i] - 55) * pow(base, num.length() - 1 - i);
- else
- sum += (num[i] - 48) * pow(base, num.length() - 1 - i);
- }
- return sum;
- }
- // this can be ingored, This isnt what is causing the problem but feel free to look at it, it isnt complete yet
- string decimalToBase(int number, int base )
- {
- int rem;
- string tempStr (1, number % base + 48);
- while(number != 0){
- rem = number % base;
- number = number / base;
- // str.insert(0, string(1, num % base + 48))
- // or string tempStr (1, num % base + 48);
- //str.insert(0, tempStr);
- switch(rem)
- {
- } // switch
- } // while
- return " ";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement