Base converter libary

This is a discussion on Base converter libary within the C++ Programming forums, part of the General Programming Boards category; I've been able to modify it so it doesn't ask for direct user input, but I'm trying to find a ...

  1. #16
    Registered User
    Join Date
    Apr 2005
    Posts
    53
    I've been able to modify it so it doesn't ask for direct user input, but I'm trying to find a way to modify the argv[2] so that it only takes in 1 argument which is the value to be represented from base 2 up to base 30. I'm not sure how to change the loop so that base starts at 2 and increments up to 30.

    Code:
    #include <iostream>
    #include <string.h>
    
    using namespace std;
    
    int main(int argc, char **argv)
    {
            if(argc == 3)
            {
                    input = atoi(argv[1]);
                    base = atoi(argv[2]);
            }
    
            while (input != 0)
            {
                    switch (input % base)
                    {
                    case 0:
                            output += '0';
                            break;
                    case 1:
                            output += '1';
                            break;
                    case 2:
                            output += '2';
                            break;
                    case 3:
                            output += '3';
                            break;
                    case 4:
                            output += '4';
                            break;
                    case 5:
                            output += '5';
                            break;
                    case 6:
                            output += '6';
                            break;
                    case 7:
                            output += '7';
                            break;
                    case 8:
                            output += '8';
                            break;
                    case 9:
                            output += '9';
                            break;
                    case 10:
                            output += 'A';
                            break;
                    case 11:
                            output += 'B';
                            break;
                    case 12:
                            output += 'C';
                            break;
                    case 13:
                            output += 'D';
                            break;
                    case 14:
                            output += 'E';
                            break;
                    case 15:
                            output += 'F';
                            break;
                    case 16:
                            output += 'G';
                            break;
                    case 17:
                            output += 'H';
                            break;
                    case 18:
                            output += 'I';
                            break;
                    case 19:
                            output += 'J';
                            break;
                    case 20:
                            output += 'J';
                            break;
                    case 21:
                            output += 'K';
                            break;
                    case 22:
                            output += 'L';
                            break;
                    case 23:
                            output += 'M';
                            break;
                    case 24:
                            output += 'N';
                            break;
                    case 25:
                            output += 'O';
                            break;
                    case 26:
                            output += 'P';
                            break;
                    case 27:
                            output += 'Q';
                            break;
                    case 28:
                            output += 'R';
                            break;
                    case 29:
                            output += 'S';
                            break;
                    case 30:
                            output += 'T';
                            break;
                    }
                    input = input/base;
                    counter++;
            }
            cout << "The result of the conversion is ";
            while (counter > -1)
            {
                    cout << output[counter];
                    counter--;
            }
            cout << "\n";
            return 0;
    }

  2. #17
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,832
    Hint
    Code:
    char table[] = "0123456789ABCDEF";
    output += table[input % base];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #18
    Registered User
    Join Date
    Apr 2005
    Posts
    53
    I modified the code to this and I get a "Floating exception (core dumped)"


    Code:
    #include <iostream>
    #include <string.h>
    
    using namespace std;
    
    int main(int argc, char **argv)
    {
            char table[] = "0123456789ABCDEF";
            int input, base, counter = 0;
    
            if(argc == 2)
            {
                    input = atoi(argv[1]);
            }
    
             base = 0;
    
            while (input != 0)
            {
                    switch (input % base)
                    {
                    case 0:
                            output += '0';
                            break;
                    case 1:
                            output += '1';
                            break;
                    case 2:
                            output += '2';
                            break;
                    case 3:
                            output += '3';
                            break;
                    case 4:
                            output += '4';
                            break;
                    case 5:
                            output += '5';
                            break;
                    case 6:
                            output += '6';
                            break;
                    case 7:
                            output += '7';
                            break;
                    case 8:
                            output += '8';
                            break;
                    case 9:
                            output += '9';
                            break;
                    case 10:
                            output += 'A';
                            break;
                    case 11:
                            output += 'B';
                            break;
                    case 12:
                            output += 'C';
                            break;
                    case 13:
                            output += 'D';
                            break;
                    case 14:
                            output += 'E';
                            break;
                    case 15:
                            output += 'F';
                            break;
                    case 16:
                            output += 'G';
                            break;
                    case 17:
                            output += 'H';
                            break;
                    case 18:
                            output += 'I';
                            break;
                    case 19:
                            output += 'J';
                            break;
                    case 20:
                            output += 'J';
                            break;
                    case 21:
                            output += 'K';
                            break;
                    case 22:
                            output += 'L';
                            break;
                    case 23:
                            output += 'M';
                            break;
                    case 24:
                            output += 'N';
                            break;
                    case 25:
                            output += 'O';
                            break;
                    case 26:
                            output += 'P';
                            break;
                    case 27:
                            output += 'Q';
                            break;
                    case 28:
                            output += 'R';
                            break;
                    case 29:
                            output += 'S';
                            break;
                    case 30:
                            output += 'T';
                            break;
                    }
                    input = input/base;
                    counter++;
            }
            cout << "The result of the conversion is ";
            while (counter > -1)
            {
                    cout << output += table[input % base];
                    counter--;
            }
            cout << "\n";
            return 0;
    }

  4. #19
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,832
    I guess replacing a 90 line switch / case statement with a one-line statement was a bit much?

    > cout << output += table[input % base];
    Is in the wrong place.

    Just how did you declare output anyway?
    We can't help you if you don't post the ACTUAL code you compiled. This does NOT compile.


    > char table[] = "0123456789ABCDEF";
    I was kinda hoping you'd add G to T yourself.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #20
    Registered User
    Join Date
    Apr 2005
    Posts
    53
    Sorry about that. This should compile now. Though I still get the floating core dumped process. Not sure if it is because base is init to 0. Thank you for your time.

    Code:
    #include <iostream>
    #include <string.h>
    
    using namespace std;
    
    int main(int argc, char **argv)
    {
            char table[] = "0123456789ABCDEFGHIJKLMNOPQRST";
            int input, base = 2, counter = 0;
            string output;
    
            if(argc == 2)
            {
                    input = atoi(argv[1]);
            }
            else
            {
                    cout << "Not Enough Params" << "\n";
                    exit(-1);
            }
    
            while (input != 0)
            {
                    switch (input % base)
                    {
                    case 0:
                            output += '0';
                            break;
                    case 1:
                            output += '1';
                            break;
                    case 2:
                            output += '2';
                            break;
                    case 3:
                            output += '3';
                            break;
                    case 4:
                            output += '4';
                            break;
                    case 5:
                            output += '5';
                            break;
                    case 6:
                            output += '6';
                            break;
                    case 7:
                            output += '7';
                            break;
                    case 8:
                            output += '8';
                            break;
                    case 9:
                            output += '9';
                            break;
                    case 10:
                            output += 'A';
                            break;
                    case 11:
                            output += 'B';
                            break;
                    case 12:
                            output += 'C';
                            break;
                    case 13:
                            output += 'D';
                            break;
                    case 14:
                            output += 'E';
                            break;
                    case 15:
                            output += 'F';
                            break;
                    case 16:
                            output += 'G';
                            break;
                    case 17:
                            output += 'H';
                            break;
                    case 18:
                            output += 'I';
                            break;
                    case 19:
                            output += 'J';
                            break;
                    case 20:
                            output += 'J';
                            break;
                    case 21:
                            output += 'K';
                            break;
                    case 22:
                            output += 'L';
                            break;
                    case 23:
                            output += 'M';
                            break;
                    case 24:
                            output += 'N';
                            break;
                    case 25:
                            output += 'O';
                            break;
                    case 26:
                            output += 'P';
                            break;
                    case 27:
                            output += 'Q';
                            break;
                    case 28:
                            output += 'R';
                            break;
                    case 29:
                            output += 'S';
                            break;
                    case 30:
                            output += 'T';
                            break;
                    }
                    input = input/base;
                    counter++;
            }
            cout << "The result of the conversion is ";
            while (counter > -1)
            {
                    cout << output[counter];
                    //not exactly sure where this line should go
                    //cout << output += table[input % base];
                    counter--;
            }
            cout << "\n";
            return 0;
    }
    Last edited by cisokay; 05-14-2005 at 02:35 PM.

  6. #21
    Registered User
    Join Date
    Apr 2005
    Posts
    53
    How would I modify it to make it print bases 0 - 30?

    I can see that:

    Code:
    char table[] = "0123456789ABCDEFGHIJKLMNOPQRST";
    Can be modified for the switch statment, but I'm unsure how the loop would work so instead of printing a specific base. It will print bases 2 through 30. If anyone could help me, I would greatly apperciate it.
    Last edited by cisokay; 05-15-2005 at 12:19 AM.

  7. #22
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,832
    Like this!
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main(int argc, char **argv)
    {
        char table[] = "0123456789ABCDEFGHIJKLMNOPQRST";
        int input, base = 2, counter = 0;
        string output;
    
        if (argc == 2) {
            input = atoi(argv[1]);
        } else {
            cout << "Not Enough Params" << "\n";
            exit(-1);
        }
    
        while (input != 0) {
            output += table[input % base];
            input = input / base;
            counter++;
        }
        
        cout << "The result of the conversion is ";
        while (counter > -1) {
            cout << output[counter];
            counter--;
        }
        cout << "\n";
        return 0;
    }
    Your switch / case is a glorified array - you have cases from 0 to some number, and you do exactly the same thing in each case.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #23
    Registered User
    Join Date
    Apr 2005
    Posts
    53
    Thanks, I actually figured out how to loop through a certain amount of bases to print out. Just threw another while loop outside of the input check and as long as the val < valtostopat. Thanks for this shortcut, it def. limited the lines of code and did the exact thing. =)

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 10:42 PM
  2. Base Converter Part 2
    By encyclopedia23 in forum C Programming
    Replies: 2
    Last Post: 12-30-2006, 02:42 PM
  3. Base Converter
    By encyclopedia23 in forum C Programming
    Replies: 3
    Last Post: 12-29-2006, 08:55 AM
  4. Base10 to Base n converter
    By P4r4digm in forum C++ Programming
    Replies: 1
    Last Post: 10-04-2006, 10:34 PM
  5. Help With Libary Data Base.
    By Dangerous Dave in forum C Programming
    Replies: 5
    Last Post: 04-23-2002, 07:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21