#include #include #include /* * Created by Drake Vidkjer * Created on 10/8/15 * Modified on 10/9/15 * Version .1 * Take in a number and convert it into another base */ using namespace std; int main() { //define variables string input; string base; bool inGood = false; bool cont = true; int num; while(cont == true) { //reset all variables used inGood = false; num = -1; base = ""; input = ""; //loop until th number entered is valid while(inGood == false) { //ask user for input and take in cout << dec << "Enter a number 0-15: "; cin >> input; //check that input if(input.size() == 2) { if(input.at(0) < 48 || input.at(0) > 57) { //tell user bad input cout << "Bad input, try again" << endl; } else if(input.at(1) < 48 || input.at(1) > 57) { //tell user bad input cout << "Bad input, try again" << endl; } else { //cast number to int and check num = atoi(input.c_str()); if(num >= 0 && num <= 15) { inGood = true; } } } else if(input.size() == 1) { if(input.at(0) < 48 || input.at(0) > 57) { //tell user bad input cout << "Bad input, try again" << endl; } else { //cast number to int and check num = atoi(input.c_str()); if(num >= 0 && num <= 15) { inGood = true; } } } else if(input.size() >= 3) { //tell user bad input cout << "Bad input, try again" << endl; } } //loop until user tells us what base to put number into while(base != "b" && base != "h" && base != "o") { //ask user what to do cout << "What base do you want to convert into? Binary (b), Hexadecimal (h), or Octal (o) "; cin >> base; //tell user if input was bad //if(base != "B" || base != "H" || base != "O") //{ // cout << "Invalid input, try again" << endl; //} } //convert number if(base == "b") { //cout << num << " in binary is " << bin << num << endl; cout << num/8; num = num - (num/8) * 8; cout << num/4; num = num - (num/4) * 4; cout << num/2; num = num - (num/2) * 2; cout << num/1 << endl; num = num - (num/1) * 1; } else if(base == "h") { cout << num << " in hexadecimal is " << hex << num << endl; } else if(base == "o") { cout << num << " in octal is " << oct << num << endl; } else { cout << "something went wrong :(" << endl; return 0; } //check if user wants to enter new number while((input != "y") && (input != "n")) { cout << "Would you like to convert another number? (y/n): "; cin >> input; } if(input == "y") { cont = true; } else if (input == "n") { cont = false; } else { cout << "something went wrong :(" << endl; return 0; } } }