Need help converting bases with C++

Discussion in 'Programming and Homework Questions' started by Mastershroom, Jun 28, 2011.

Thread Status:
Not open for further replies.
  1. Mastershroom

    Mastershroom wat

    Reputations:
    3,833
    Messages:
    8,212
    Likes Received:
    16
    Trophy Points:
    206
    Hi guys. I'm a CS major at my university, and I'm taking my first programming course, in C++. Until now I've had a decent idea of what I'm doing, but now I have a lab assignment that I'm totally lost on. I have to write a program that converts a user input integer value to and from specified bases, from base 2 through base 10. I'm supposed to write one function to convert from base 10 to the specified base, and another to convert from the specified base to base 10. I have no idea where to even begin, and my professor and textbook are less than helpful.

    Obviously I'm not asking anyone to do my work for me, but I really need some help. The only things I've found through Google involved converting up to base 16, which involves characters and strings, which are not relevant here.

    If it helps, here are the full instructions for the assignment:
    If it makes any difference, I'm using Eclipse to write and compile the code, and it must be submitted to an online repository by midnight on Friday night.

    +rep to anyone who can help, of course, and I wouldn't even mind sending a bit of Paypal money your way. I'm a little desperate here -_-
     
  2. redrazor11

    redrazor11 Formerly waterwizard11

    Reputations:
    771
    Messages:
    1,315
    Likes Received:
    0
    Trophy Points:
    55
    *I don't know C++ syntax, but I do know C#, Java, C, Python, MIPS, and V.B*

    Your first function is a simple "get input from user" function. It will assign user input to the 3 input variables you need. Your argument for the function will either be (number) or (base). If you're asking user for (number) then you must check if it's between 1 and 2147483647.

    If the argument is (base) then you must check if it's between 2 and 10.

    PSUDO-CODE*************

    //IN MAIN called 3 times to get user input
    int oldNum = getUserInput(false);
    int oldBase = getUserInput(true); //true for base check
    int newBase = getUserInput(true);
    //IN MAIN

    //Input function
    int getUserInput (bool base) //this is a flag to check for base or number
    {
    int temp = userInput.get(keyboard.in) //idk however you get user input

    if (base==true) //it must be a base then
    ...if(temp <=10 || temp >=2) // so we check between 2 and 10
    ......return temp;
    ...else
    ......print (out of range)
    else //it must not be a base then
    ...if (temp <=2147483647 || temp >= 1)
    ......return temp;
    ...else
    ......print (out of range)
    }


    After calling this 3 times from main (number, base, new base), you will have your input parameters for the next function...

    The first thing you need to do is figure out the base-converting equation. You have 3 input variables (original number, original base, new base) and 1 output variable (new number).

    It's possibly a logarithmic equation. You can probably google it.

    //IN MAIN
    int newNum = calcNewNum(oldNum, oldBase, newBase); //call function with the inputs

    print ("your nums are: " + oldNum + oldBase + newNum + newBase);

    //IN MAIN

    //function to calculate new num
    int calcNewNum(int oN, int oB, int nB)
    {

    //use log equasion here

    //return solution here

    }



    Obviously, you'll need to put some other stuff in there...lol (such as loop around main, in order to keep asking user for numbers until they enter 0 to quit).

    PM me if you run into trouble with the logic. I'm not up on C++ syntax, but I can probably help you. I'm 3rd year into my CS major.
     
  3. redrazor11

    redrazor11 Formerly waterwizard11

    Reputations:
    771
    Messages:
    1,315
    Likes Received:
    0
    Trophy Points:
    55
    Did that help at all?

    DP intended as a bump.
     
  4. Kyle

    Kyle JVC SZ2000 Dual-Driver Headphones

    Reputations:
    1,700
    Messages:
    810
    Likes Received:
    286
    Trophy Points:
    76
    Forget about programming for the moment --- do you know how you would do it mathematically? Say on a math test?
     
  5. Mastershroom

    Mastershroom wat

    Reputations:
    3,833
    Messages:
    8,212
    Likes Received:
    16
    Trophy Points:
    206
    Sorry, I just got around to checking this thread. I'll have to fiddle around with that code and try to implement it into C++. Thank you very much for the help, I'll give you rep as soon as mine recharges. :)
    Not a clue. :eek:
     
  6. Mastershroom

    Mastershroom wat

    Reputations:
    3,833
    Messages:
    8,212
    Likes Received:
    16
    Trophy Points:
    206
    Okay, so I'm working on the input function now, and this is what I have at this point:
    Code:
    #include <iostream><iostream>
    
    using namespace std;
    
    int getUserInput(bool base) //determines which range to check
    {
        int input;
        cin >> input;
        if(base == true)
        {
            if(input <= 10 && input >=2)
            {
                return input;
            }
            else cout << "Out of range.";
        }
        else
            if (input <= 2147483647 && input >= 1)
            {
                return input;
            }
            else cout << "Out of range.";
    }
    
    int main()
    {
        int oldNumber = getUserInput(false);
        int oldBase = getUserInput(true);
        int newBase = getUserInput(true);
    
    }
    
    <iostream>
    I used && instead of || to check if the input is in a valid range because the value has to be greater than the minimum value and less than the maximum value, otherwise it could still be out of range but still pass the test since one of the conditions would be true.

    Obviously it's not finished yet, but it compiled without error...it just doesn't do anything at this point. :p

    *sigh* I'm really starting to reconsider my choice of major >_<</iostream></iostream></iostream>
     
  7. Kyle

    Kyle JVC SZ2000 Dual-Driver Headphones

    Reputations:
    1,700
    Messages:
    810
    Likes Received:
    286
    Trophy Points:
    76
    Figure that out first. Lots of links on converting bases on googling.
    Coding is just a way to automate the process.

    To give an analogy, dabbling in c++ now is like trying to assemble a factory for making cakes, before you know how to bake a cake by hand. Doesnt make sense. Get the recipe for baking the cake, and bake a few cakes yourself by hand to see how the recipes work.

    THEN assemble the factory.
     
  8. masterchef341

    masterchef341 The guy from The Notebook

    Reputations:
    3,047
    Messages:
    8,658
    Likes Received:
    4
    Trophy Points:
    206
    precisely.
     
  9. Mastershroom

    Mastershroom wat

    Reputations:
    3,833
    Messages:
    8,212
    Likes Received:
    16
    Trophy Points:
    206
    Not sure it was worth bumping the thread three months later for that, masterchef. :p The class has been done for over a month lol.
     
  10. hockeymass

    hockeymass that one guy

    Reputations:
    1,450
    Messages:
    3,683
    Likes Received:
    85
    Trophy Points:
    116
    And how did you do?
     
Loading...
Thread Status:
Not open for further replies.

Share This Page