need help here..

This is a discussion on need help here.. within the C++ Programming forums, part of the General Programming Boards category; hi i just finished my program but it won't do the input as required by me.. some digits are missing ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    61

    need help here..

    hi i just finished my program but it won't do the input as required by me..
    some digits are missing and i can't find it out
    basically it is a program that ask user's for input integer and base number
    here's the coding

    Code:
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    void convert(int , int, int, int*); //convert(number, base, length, result)
    
    int len=0, i;
    
    int main()
    {
    int num, base, *r;
    
    cout<<"\nEnter a number: ";
    cin>>num;
    cout<<"\nEnter base: ";
    cin>>base;
    
    len=(int)log10(num)/log10(base)+1; //Number of digits of the converted number
    
    r=new int [len];
    
    convert(num, base, len, r);
    
    cout<<endl;
    
    for(i=0; i<len; i++)
    cout<<*(r+i);
    
    delete [] r;
    
    return 0;
    }
    
    void convert(int n, int b, int len, int* res)
    {
    for(i=0; i<len; i++)
    {
    *(res+len-i-1)=n%b;
    n/=b;
    }
    }
    like this input i put on
    Enter an integer: 89
    actual answer||||||||||||| My input answer!!!
    89 in Base 2 : 1011001>>1001

    89 in Base 3 : 10022>>>>022

    89 in Base 4 : 1121>>>>>21

    89 in Base 5 : 324>>>>>>24
    how to solve this problem???
    any ideas??

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,462
    Quote Originally Posted by MyRedz
    here's the coding
    Good to see that you posted code in code tags, but please indent your code properly.

    Quote Originally Posted by MyRedz
    how to solve this problem???
    any ideas??
    Use a debugger. In particular, examine the value of len. You will find that it is smaller than expected because of a mistake in casting.

    Also, both len and i should be local variables. In fact, the scope of i should be limited to the for loops.

    EDIT:
    Consider:
    • Using std::string or std::vector instead of manual memory management
    • What happens when the base is greater than 10?
    • Turning on compiler warnings to a higher level (the casting problem could have been detected with a warning)
    • Using static_cast instead of C-style casts (it would also have helped prevent the problem)
    • Using array notation instead of pointer notation when you use array indices (e.g., r[i] instead of *(r+i))
    Last edited by laserlight; 01-20-2009 at 11:48 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,457
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    a newbie :p
    Join Date
    Aug 2008
    Location
    Zurich, Switzerland, Switzerland
    Posts
    90
    Quote Originally Posted by MyRedz View Post
    Code:
    len=(int)log10(num)/log10(base)+1; //Number of digits of the converted number
    this won't work!
    should be:

    Code:
    len=(int)(log10(num)/log10(base)+1); //Number of digits of the converted number
    calculate first, later convert to int.

    I tried in VC++, its output is just good.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,462
    auralius, that is what I meant by a mistake in casting. Apparently you were less confident than I was of MyRedz's debugging skills
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    a newbie :p
    Join Date
    Aug 2008
    Location
    Zurich, Switzerland, Switzerland
    Posts
    90
    I'm speechless...

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    61
    question here..
    how do i toget it put text before the answer like the answer is..
    i tried
    for(i=0; i<len; i++)
    cout<<"the answer is"<<*(r+i);
    but the output is meessed up then..
    how to make it into separate functions i mean for.
    base 2 base 4 base 6.
    this coding only allow more to do for one base and input at a time ..

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,457
    You do it right, but don't forget to put a space! They aren't automatically put in there, you know...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    a newbie :p
    Join Date
    Aug 2008
    Location
    Zurich, Switzerland, Switzerland
    Posts
    90
    Quote Originally Posted by MyRedz View Post
    i tried
    Code:
    for(i=0; i<len; i++)
         cout<<"the answer is"<<*(r+i);
    but the output is meessed up then..
    can't be like that...
    string "the answer is" will be reprinted several times...

    should be:

    Code:
    cout << "the answer is  " ;
    for(i=0; i<len; i++)
    	cout << *(r+i);

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    61
    ok looks good here well.
    then my question ask for a function prototype to receive two arguments only that is the user inputed base and integer value.
    how to do this.
    must the value and calculation of len and rbe defined as global variable or compute it separately in each function
    well here's my coding but one error..
    am i right here
    Code:
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    void convert(int , int, int, int*); //convert(number, base, length, result)
    
    int len=0, i,*r;
    
    int main()
    {
    int num, base ;
    
    cout<<"\nEnter a number: ";
    cin>>num;
    cout<<"\nEnter base: ";
    cin>>base;
    convert(num, base );
    len=(int)(log10(num)/log10(base)+1); //Number of digits of the converted number
    r=new int [len];
    
    
    
    cout<<endl;
    
    for(i=0; i<len; i++)
    cout<<*(r+i);
    
    delete [] r;
    
    return 0;
    }
    
    void convert(int n, int b)
    {
    	
    for(i=0; i<len; i++)
    {
    *(r+len-i-1)=n%b;
    n/=b;
    }
    }

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,462
    Quote Originally Posted by MyRedz
    then my question ask for a function prototype to receive two arguments only that is the user inputed base and integer value.
    What is the rationale for such restrictions on the number of parameters?

    Quote Originally Posted by MyRedz
    must the value and calculation of len and rbe defined as global variable or compute it separately in each function
    No, do not use global variables. #include <vector> and use a std::vector<int>, e.g.,
    Code:
    std::vector<int> convert(int number, int base);
    
    // ...
    
    cout << "\nEnter a number: ";
    cin >> num;
    cout << "\nEnter base: ";
    cin >> base;
    cout << endl;
    
    std::vector<int> result = convert(num, base);
    for (std::vector<int>::size_type i = 0, len = result.size(); i < len; ++i)
        cout << result[i];
    Note that you no longer need to use delete[] since std::vector performs the memory management for you.

    If for some reason you cannot use std::vector, then the next simplest alternative is to perform the dynamic memory allocation in the convert function and then return a pointer. You would then compute the length twice: once in the convert function and once in the main function.

    If you happen to be taking a class with a teacher who insists that convert must take two arguments of type int and have a void return type, then go ahead and use global variables, but know also that your teacher would be teaching you poor programming practices.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

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