1

Thanks for any help in advance.

I was given an assignment that takes in the following inputs:

init_base which is the base we are converting to. 2<=init_base and 36>= init_base
in_num which is the decimal number that is to be converted
in_num gets written to multiple times for each decimal number
the input is terminated with a -1

I was told to use the capital letters of the alphabet for when the number is larger than 9.

I have written code for the solution. The code runs but I constantly get a weird output. I am not to sure why this is. I think it has something to do with my data type conversions. However I am not too sure what is going wrong.

#include <iostream>
#include <cmath>
#include <string>
#include <stdlib.h>
#include <algorithm> 

using namespace std;
string ans;
int counter = 0;
bool Alpha_check(int val){
    if(val>9){
        return true;
    }
    else{
        return false;
    }
}
char Al_conv(int val){
    if(Alpha_check(val)){
        return char(val+55);
    }
    else {
         return char((val-48)+'0'); 
    }
}
void add_On(int c){
    ans.append(Al_conv(c));
    counter++;
}

int div_loop(int num, int base){    
    int temp = int(num/base);
    int temp2 = int(num%base);
    add_On(temp2);
    return temp;
}

void add_line(int number){
    ans[number] = '\n';
}

int main(){
    int init_base, in_num = 0;
    cin >> init_base;
    cin >> in_num;
    do{
        string rem;
        int init_count = counter;
        while(in_num!=0){
            in_num = div_loop(in_num,init_base);
        }
        int helper = int(floor((counter-init_count)/2));
        for(int y = 0; y < helper; y++){
            int temp = ans[y+init_count];
            ans[y+init_count] = ans[(counter-1)-y];
            ans[(counter-1)-y] = temp;
        }
        add_line(counter);
        counter++; 
        cin >> in_num;
    }while(in_num!=-1);
    ans[counter] = '\0';
    for(int gh = 0; gh < ans.length(); gh++){
        cout << ans[gh];
    }
    cout << endl;


     return 0;
}

Here is also a link for you to follow http://ideone.com/qFOtyl to check out the output.

  • 2
    It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: **[How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)** – NathanOliver Mar 21 '17 at 13:28
  • Thanks @NathanOliver . I generally don't use a debugger. I use output statements with variables details etc. But do you know of any good debuggers out there for c++ for windows 10? I've currently been using a CLI on Ubuntu but I have yet to get around to compartementalizing my drive on my laptop. – Johann Muller Mar 21 '17 at 13:57
  • I really like MSVS for it's debugger. It's C++ support isn't the best but version 2015 and 2017 do pretty good. – NathanOliver Mar 21 '17 at 14:45
  • @NathanOliver Alright Thanks. Will give it a try. Thanks – Johann Muller Mar 21 '17 at 15:10

1 Answers1

0

Splitting the problem in 2 parts, the solution is simple. First part, split the input number in the digits using the base and the modulo operator. Then, convert them into a character suitable for output.

///Given a maximum base of 36, the input of this function is in the range [0..35]
char toAlphaNumericSingleDigit(int input) {
    if (input < 10) {
         return '0' + input;
    } else {
         return 'A' + (input - 10);
    }
}

void print_conversion(int input, int base) {
    while (input > 0) {
         std::cout << toAlphaNumericSingleDigit(input % base);
         input /= base; ///Proceeds to the next digit
    }
    std::cout << std::endl;
}

Note that this will print the output characters in reversed order, from least significant to most significant.

Tested:

int main() {
    print_conversion(1294,36);
    return 0;
}

Prints:

YZ

Antonio
  • 19,451
  • 13
  • 99
  • 197
  • Thanks @Antonio. This helps quite a bit. Can you possibly explain the **input /= base;** operation for me? I'm assuming it divides input by base and then stores back into input. But does this not then forget to take into account that the **input** variable should be rounded down **( rather : floor(input)? )** – Johann Muller Mar 21 '17 at 14:23
  • @JohannMuller In integer division, the `/` operator always "rounds" down for positive numbers ("truncation toward zero"). See http://stackoverflow.com/q/3602827/2436175 – Antonio Mar 21 '17 at 14:26