Hello all, I am new to here, but not so new to programming. I'm no expert which is why I am here to seek help. I'm working on a number converting program which will convert a base 10 number to any other base that the user selects.
The way I initially went about this problem would be is every time the function would find the remainder it would cout that remainder, unfortunately this was giving me output in reverse order. Then a friend suggest I try using stacks. Here's my problem I think I've set up the stacks correctly, but every time I run the program it hangs or stalls at the point where it should display the converted number. To help I shall post snippets of my code.
#include <iostream>
#include <string>
#include <stack>
usingnamespace std;
void converter (longlong& number, int& base);
int main()
{
longlong number; //This is the number that the user wants to convert
int base; //This isthe base that the user wants to convert their number to
int choice; //This is the variable used in the opening menu
bool numbert; //This is the boolean variable used to say whether the number / base entered in by the user is valid
bool answer = true; //This is the boolean variable used to say whether the program should keep running or should stop.
while (answer == true) //This is to say while answer == true keep running the program
{
system ("cls");
cout << "Welcome to the decimal (base 10) number converter program by Cole Feeser & Jeff Anderson." << endl << endl;
cout << "What would you like to do?: " << endl << "1. Convert a decimal (base 10) number." << endl << "2. Get more info about the different number bases." << endl << "3. Get more info about this program and it's creators." << endl << "4. Exit the program." << endl << endl;
cout << "Answer: ";
cin >> choice; //This is where the user decides what they want to do
if (choice == 1)
{
numbert = true;
while (numbert == true)
{
system ("cls");
cout << "Please enter a whole decimal (base 10) number: ";
cin >> number; //The user enters their number here
cout << "Please enter a base between -36 and 36 (except -1 (For all numbers), 0 (For all numbers), or 1 (Only for numbers <= 0)) to have your number converted to: ";
cin >> base; //The user enters the base they want to convert to, here
numbert= false; //This makes the numbers appear valid to the program
if (base == 0 || base == -1 || (base == 1 && number <= 0))
{
numbert = true; //This changes the numbert variable to say that the number / base inputted by the user is invalid
cout << endl << "The number and/or base you entered is invalid. Please enter a new number and base." << endl << endl;
system ("pause");
}
}
cout << endl << "Your number " << number << " converted to base " << base << " is: ";
converter (number, base); //This is the line where the program shifts into the main functions below to make the necessary conversion.
}
Not sure on this one, but my guess would be that the stack is of char, so when you push the remainder (eg 3) the compiler pushes the number 3 not '3'.
The 'quick fix' for this would be to change convertBase.push(remainder);
to convertBase.push(remainder+48);
so that 3 becomes 51 which is the character code for '3'
You would also need to check for remainder > 9 if using a base >10, at which point you need to add 55 ('A' = 65).