Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Use this program to convert numbers of one base to another base.
- // For example, decimal to hexadecimal, or binary to base 36 (0-Z).
- // Base can be anything between 2 and 36.
- // Convert.cpp https://pastebin.com/c6HBu4rp
- // Convert.h https://pastebin.com/39AvSGaj
- #include <cstdio>
- #include <iostream>
- #include <string>
- #include "Convert.h"
- using namespace std;
- void invalidInput();
- void checkValue(string &input, int base);
- void checkBase(int &base);
- int main(){
- Convert convert;
- string initNum;
- string newNum;
- int initBase;
- int newBase;
- int bufferNum;
- printf("Enter initial base (2-36): ");
- checkBase(initBase);
- printf("\nValid numbers: %s\n", convert.getBase(initBase).c_str());
- printf("Enter initial number: ");
- checkValue(initNum, initBase);
- printf("\nEnter new base (2-36): ");
- checkBase(newBase);
- printf("\nBase %d: %s\n", initBase, initNum.c_str());
- if(newBase != initBase){
- bufferNum = convert.tonumber(initNum, initBase);
- printf("Base 10: %d\n", bufferNum);
- if(newBase != 10){
- newNum = convert.tobase(bufferNum, newBase);
- printf("Base: %d: %s\n", newBase, newNum.c_str());
- }
- }
- system("PAUSE");
- return 0;
- }
- void invalidInput(){
- printf("Invalid input. Try again: ");
- cin.clear();
- cin.ignore(10000, '\n');
- }
- bool checkBase(string input, int base){
- for(int i = 0; i < input.length(); i++){
- bool isBase = false;
- for(int b = 0; b < base; b++){
- if(input[i] == Convert().getBase(base)[b]){
- isBase = true;
- break;
- }
- }
- if(!isBase){
- return false;
- }
- }
- return true;
- }
- void checkValue(string &input, int base){
- while(!(cin >> input && checkBase(input, base))){
- invalidInput();
- }
- }
- void checkBase(int &base){
- while(!(cin >> base && base > 1 && base < 37)){
- invalidInput();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement