need help w/ number converter prog.

This is a discussion on need help w/ number converter prog. within the C++ Programming forums, part of the General Programming Boards category; This program is suppoded to get an input of any number in any number system with a base from 2(binary) ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    28

    need help w/ number converter prog.

    This program is suppoded to get an input of any number in any number system with a base from 2(binary) to 62, which includes all digits plus all capital and then all lower case letters.

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    string relet (int input)
    {
        string output;
        if (input == 0)
           output="0";
        if (input == 1)
           output="1";
    // ect, ect, ect.
        if (input == 61)
           output="z";
        return output;
    }
    
    int unlet (string input)
    {
        int output;
        if (input == "0")
           output=0;
        if (input == "1")
           output=1;
    //ect, ect, ect.
        if (input == "z")
           output=61;
        return output;
    }
    
    int main ()
    {
        string number;
        int base;
        int newbase;
    // (removed for space) This part gets user input for number, base, and newbase.
        const int length=number.length();
        int input[length];
        int * output;
        output[0]=0;
    // This loop puts the number into array "input", while reversing the numbers
    // and replaces letters with their decimal number equivilents
        int x=0;
        int y=length-1;
        string buffer;
        start1:
        buffer=number[y];
        input[x]=unlet (buffer);
        x++;
        y--;
        if (x != length)
           goto start1;
        y=0;
        x=0;
        
        
    //  -THE IMPORTANT PART-
        
    // subtracts 1 from the input
        substart:
        if (input[x] == 0)
        {
           input[x]=base-1;
           x++;
           if (x == length) // end if the input is 0
              goto finish;
           goto substart;
        }
        input[x]--;
        x=0;
        
    // adds 1 to the output
        addstart:
    // this adds another digit to the output when nessecary
        if (x > y)
        {
           output = new int;
           output[x]=0;
           y++;
        }
        if (output[x] == newbase-1)
        {
           output[x]=0;
           x++;
           goto addstart;
        }
        output[x]++;
        x=0;
        goto substart;
        finish:
        
    //(removed) outputs the "output" array starting from the top down and uses the
    //"relet" function.
    It works sometimes, but if the output is more than 2 digits it will sometimes add 1 to the second digit, and it crashes if the output is more than 4 digits long.

    I have gone through the code countless times and cannot figure out what is making it not work. I can post the full code it anyone wants.
    Last edited by Dash_Riprock; 08-14-2006 at 01:19 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> int * output;
    >> output[0]=0

    I'm not sure how you got the code to compile with that missing semicolon, but even with it there you are accessing memory that doesn't belong to you since output is uninitialized.

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    28
    The semicolon is there, I just accidentally deleted it when trimming down the code. I just put it back in.
    And I dont know what you mean by it isnt initialized- isnt it initialized in the line above? Plus the compiler would not let it compile if it wasnt initialized.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I don't think you should be doing this:
    Code:
    const int length=number.length();
        int input[length];
    length is not known at compile-time, therefore you should allocate memory dynamically.

    Code:
        int * output;
        output[0]=0;
    output is a pointer to a single integer, not an array.

    P.S If you are allowed to use std::string, could you also use a std::vector?

  5. #5
    Registered User
    Join Date
    Jul 2006
    Posts
    28
    The array always turns out to be the right length, so even if I do change it, it isnt the scource of the problem.

    I read a tutorial somewhere that said if you used the "new" operator with a pointer, you could access the locations like it was an array, or like *(output+1). Am I misunderstanding how that works?

    And yes, I can use vectors
    Last edited by Dash_Riprock; 08-14-2006 at 02:03 PM.

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Quote Originally Posted by Dash_Riprock
    The array always turns out to be the right length, so even if I do change it, it isnt the scource of the problem.

    I read a tutorial somewhere that said if you used the "new" operator with a pointer, you could access the locations like it was an array, or like *(output+1). Am I misunderstanding how that works?
    Yes, that is so. But in this line:
    Code:
    output = new int;
    you are just allocating a single integer again, and go on using it like an array. If you want an array, allocate an array:
    Code:
    output = new int[HowMany];
    However, if you are allocating memory with new, you should also free it with delete, before the pointer goes out of scope or you intend to point it to something else.

    And if you can use std::vector<int> instead, you could be almost sure none of your problems are caused by handling arrays and memory incorrectly. Which would let you concentrate on the task on hand, that is numeric conversions.

  7. #7
    Registered User
    Join Date
    Jul 2006
    Posts
    28
    I tried it with a vector and it works perfectly after the first try. Thanks alot.
    Last edited by Dash_Riprock; 08-14-2006 at 05:36 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  2. scanf oddities
    By robwhit in forum C Programming
    Replies: 5
    Last Post: 09-22-2007, 01:03 AM
  3. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  4. help with a source code..
    By venom424 in forum C++ Programming
    Replies: 8
    Last Post: 05-21-2004, 12:42 PM
  5. parsing a number
    By juancardenas in forum C Programming
    Replies: 1
    Last Post: 02-19-2003, 12:10 PM

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