Latest and greatest Problem

This is a discussion on Latest and greatest Problem within the C++ Programming forums, part of the General Programming Boards category; I have a function that takes funct_name(char *s); It displays the string, uses string functions on it. BUT i need ...

  1. #1
    Unregistered
    Guest

    Latest and greatest Problem

    I have a function that takes funct_name(char *s);
    It displays the string, uses string functions on it.

    BUT i need to send a LONG INT to it for display.

    - cannot convert parameter 1 from 'long' to 'char *' -
    if i simply cast it, it compiles but gets errors when running it :

    at this statement on debug --> strcpy((char*)datum,s);
    (datum is void *)


    any way around this problem ?

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Unregistered
    Guest
    i need the opposite

  4. #4
    Unregistered
    Guest

    Thumbs up

    gcvt

    did it


    Thats, cool

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    to pass a type long to the function you need to change type long into type char *, that is into a string. atol() converts type char * to type long, but that isn't what you want. I think conio.h has a conversion from long to char *, probably ltof() if their function naming pattern holds. But it will be non-standard, meaning your compiler might not have it.

    You can write your own ANSI C++ standard function to do this. It will probably go something like this:

    //char array to hold digits one by as they are generated
    char temp[30];

    //number to convert
    long num = 123;

    //peel off digits one at a time
    int holder;
    int i = 0;
    while (num >= 0)
    {
    //get first digit on the left of num
    holder = num %10;

    //cast holder to char and store int temp
    temp[i++] = (char)holder;

    //divide num by 10 using integer math to peel of least significant
    //digit
    num = num/10;
    }

    //now convert temp to string by adding null terminating char
    temp[i] = 0;

    //now reverse temp so digits are in correct order
    strrev(temp);

    //now send temp to your function

    Well, that almost works, but strrev() isn't ANSI C++ standard either so you should probably write your own string reversal function to get the whole thing ANSI C++ standard. The reversal function might look something like this;

    int i;
    char ch;
    int length = strlen(temp);
    for(i = 0; i < length/2; i++)
    {
    ch = temp[i];
    temp[i] = temp[length - 1 - i];
    temp[length - 1 - i] = ch;
    }

    now with a little tweaking you should be burning premium.

  6. #6
    Ted
    Ted is offline
    Registered User
    Join Date
    Aug 2001
    Posts
    52
    The last time I looked there was a ltoa() function that converts a long int value to a string.

    Check out stdlib.h

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. loop to get greatest and least number, its not working please help
    By stressedstudent in forum C++ Programming
    Replies: 27
    Last Post: 09-27-2007, 04:45 AM

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