// Programmer: Simon Thougaard
// File: hw4.cpp
// Purpose: Convert numbers from different bases to base 10
using namespace std;
#include<iostream>
int main()
{
// Declarations
const int MAX_X = 99999;
const short MIN_X = 0;
const short MAX_N = 9;
const short MIN_N = 2;
const short MAX_NUM_TRIES = 3;
short option; // User choice
int x; // Value to convert
int n; // Base of the input value
short attemptCount;
// Used to check/convert x
int divisor;
int multiplier;
int sum;
int digit;
bool shouldContinue = true;
bool foundBadDigit = false;
// Used to remember if steps have been done
bool xSet = false;
bool nSet = false;
bool verified = false;
// Greeting
cout << "Welcome to the Thingamajig!" << endl;
// Begin a do-while loop
do
{
// Prompt the user with a menu
cout << "Please select one of the following options:" << endl
<< "1: enter x []" << endl
<< "2: enter base " << endl
<< "3: verify " << endl
<< "4: convert " << endl
<< "5: exit " << endl;
cin >> option;
// Begin a switch statement on the option variable
switch(option)
{
case 1:
// Enter x
xSet = false;
verified = false;
attemptCount = 0;
do
{
cout << "Enter a positive 5-digit number for x: " << endl;
cin >> x;
if(x >= MIN_X && x < MAX_X)
{
// Valid x is entered
xSet = true;
cout << "x is set to " << x << endl;
}
else
{
cout << "The input is not in the valid range" << endl;
cout << endl;
}
attemptCount++;
} while(!xSet && attemptCount < MAX_NUM_TRIES);
break;
case 2:
// Enter n
nSet = false;
verified = false;
attemptCount = 0;
do
{
cout << "Enter a number between 2 and 9 for n: " << endl;
cin >> n;
if(n >= MIN_N && n < MAX_N)
{
// Valid n is entered
nSet = true;
cout << "n is set to " << n << endl;
}
else
{
cout << "The input is not in the valid range" << endl;
cout << endl;
}
attemptCount++;
} while(!nSet && attemptCount < MAX_NUM_TRIES);
break;
case 3:
// Verify x is a valid base n number
if(xSet && nSet) //both x and n entered
{
cout << "Verifying that " << x
<< " is a valid base " << n << " number"<< endl;
divisor = 1;
foundBadDigit = false;
for(int i = 0; i < 5; i++) //there are 5 digits
{
// Check one digit at a time
digit = (x / divisor) % 10;
if(digit >= n)
{
foundBadDigit = true;
}
divisor *= 10;
}
// Report result and update "verified"
cout << "Result: "
<< (!foundBadDigit ? "valid" : "invalid")
<< endl;
verified = !foundBadDigit;
}
else //one of options 1 or 2 not chosen
{
cout << "Valid x and n must be entered first" << endl;
}
break;
case 4:
// Convert to base 10, if x and n are verified
if(verified)
{
cout << "Converting " << x << " to base " << n << endl;
sum = 0;
divisor = 1;
multiplier = 1;
for(int i = 0; i < 5; i++)
{
// Convert one digit at a time
digit = (x / divisor) % 10;
sum += digit * multiplier;
divisor *= 10;
multiplier *= n;
}
// Output the result
cout << "The input " << x
<< " in base " << n
<< " is: " << sum
<< " in base 10" << endl;
}
else
{
cout << "First verify that x is a valid base n number"
<< endl;
}
break;
case 5:
// Quit
shouldContinue = false;
break;
default:
// In case of an input other than 1-5
cout << "invalid options" << endl;
} // End of switch-case
cout << endl;
} while(shouldContinue); // End of the do-while loop
// Goodbye message
cout << "Thanks for using the Thingamajig! Goodbye!" << endl;
return 0;
}