main.cpp
// Programmer: Simon Thougaard
// File: main.cpp
// Purpose: This program converts numbers from different number systems,
// to base 10. After inputting a number and a base, the user may
// check if the number is of valid base, and if needed force either
// valid base or number. With a valid number and base, the user can
// convert the number to base 10.
using namespace std;
#include<iostream>
#include <cstdlib>
#include "conversion.h"
int main()
{
bool shouldContinue = true;
short choice;
int x;
bool xSet = false; // is x valid
short n;
bool nSet = false; // is n valid
srand(time(NULL));
cout << "Welcome to the Krusty Number Converter!" << endl;
do
{
displayMenu();
cin >> choice;
switch (choice)
{
case 1:
promptForX(x, xSet);
break;
case 2:
promptForN(n, nSet);
break;
case 3:
if (xSet && nSet) // Only check, if both n and x are valid
{
cout << x << " is a";
cout << (checkValidPair(x, n) ? " valid" : "n invalid");
cout << " base " << n << " number" << endl;
}
else
{
cout << "You must first enter valid x and n" << endl;
}
break;
case 4:
if (xSet && nSet) // Only check, if both n and x are valid
{
if (!checkValidPair(x, n)) //... and x and n are an invalid pair
{
n = forceN(x);
cout << "n has been set to " << n << endl;
}
else
{
cout << "x and n must be an invalid pair" << endl;
}
}
else
{
cout << "You must first enter valid x and n" << endl;
}
break;
case 5:
if (xSet && nSet) // Only check, if both n and x are valid
{
if (!checkValidPair(x, n))//... and x and n are an invalid pair
{
x = forceX(x, n);
couts << "x has been set to " << x << endl;
}
else
{
cout << "x and n must be an invalid pair" << endl;
}
}
else
{
cout << "You must first enter valid x and n" << endl;
}
break;
case 6:
if (xSet && nSet) // Only check, if both n and x are valid
{
if (checkValidPair(x, n))//... and x and n are a valid pair
{
cout << x << " converted to base " << n << " is:";
cout << convert(x, n) << endl;
}
else
{
cout << "x and n must be an invalid pair" << endl;
}
}
else
{
cout << "You must first enter valid x and n" << endl;
}
break;
case 7:
shouldContinue = false; // quit the program
break;
default: // bad user input
cout << endl;
cout << "Please enter a valid option (1-7)" << endl;
}
cout << endl;
} while (shouldContinue);
cout << "Goodbye!" << endl;
return 0;
}