I am trying to write a program that will convert from one base to another and I am running into a problem with it and I have no idea what is happening.
The problem is in convert(...), somehow, and I do not know how..., my second while loop is bugging on me. It would be much easier to have you see the ouput from my program than to explain what is happening.
So, let us say the inputs to this function are: 10, 3, 556
What is wrong, is what is outputted after you press enter to enter 556.
What newTotal should be is displayed above what it actually is.
In my example, newTotal should be 70 but it is 69.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
#include <iostream>
#include <math.h>
using namespace std;
string convert(int input, int output, int num);
char getNum(int a);
int main()
{
int inputBase = 0;
do
{
cout << "Base of numbers inputted: ";
cin >> inputBase;
} while(inputBase <= 1);
int outputBase = 0;
do
{
cout << "Base of numbers ouputted: ";
cin >> outputBase;
} while(outputBase <= 1);
//TODO Check for invalid inputs. If any number is greater than output base
int numToConvert = -1;
do
{
cout << "Number to convert: ";
cin >> numToConvert;
} while(numToConvert < 0);
cout << endl;
cout << convert(inputBase, outputBase, numToConvert);
cin.get();
return 0;
}
string convert(int inputBase, int outputBase, int num)
{
int startingExponent = 0;
while(pow(outputBase, startingExponent) <= num)
{
startingExponent++;
}
startingExponent--;
int tempNum;
int newTotal = num;
string newNum = "";
do
{
tempNum = 0;
while(newTotal >= pow(outputBase, startingExponent)) {
tempNum++;
cout << "power (" << outputBase << "^" << startingExponent << "): " << pow(outputBase, startingExponent) << endl << "tempNum: " << tempNum << endl << "newTotal should be (" << newTotal << " - " << pow(outputBase, startingExponent) << "): " << newTotal - pow(outputBase, startingExponent) << endl;
newTotal -= pow(outputBase, startingExponent);
cout << "Acutal newTotal: " << newTotal << endl << endl;
cin.get();
}
startingExponent--;
newNum += getNum(tempNum);
} while(newTotal > 0 || startingExponent != -1);
return newNum;
}
char getNum(int a) {
return 48 + a;
}
| |
** I know that if I input a inputBase higher than outputBase or if inputBase > 9, it does not work. Leave that alone for now ^.^
Edit: The problem is at line 62. The value isn't what is should be... It would make a lot more sense if it was compiled.