Double variable problem...

This is a discussion on Double variable problem... within the C++ Programming forums, part of the General Programming Boards category; I need to send the variable (double) into the function as a char* Code: void changecolor(char* word, int color){ textcolor(color); ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    12

    Double variable problem...

    I need to send the variable (double) into the function as a char*

    Code:
    void changecolor(char* word, int color){
    	textcolor(color); 		 //changes text to the color given through the argument
    	cprintf("%s", word);	   	    //outputs the string in the given color
    	textcolor(LIGHTGRAY);	 //change color back to the default light gray
    } //end void changecolor
    tried this:

    Code:
      char* mon;
      mon = (char)monthly_interest;
    	changecolor(mon, color);
    monthly_interest is a double?? why? what did I go wrong? thanks!

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    i dont know why you would want to cast a double to a char *, but heres how you do it...
    (char *)&monthly_interest
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

  3. #3
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    >> (char)monthly_interest;
    Typecasting your double to a char will do no good. You need to convert it to a string first.

    >> (char *)&monthly_interest
    That will not do it either. In the function he passed it to, he's printing it as a string. So he'll need to convert the float to a string.

    EDIT - if you're still having trouble, I suggest you look at the sprintf function
    Last edited by Cshot; 10-05-2002 at 06:21 PM.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    12
    Originally posted by *ClownPimp*
    i dont know why you would want to cast a double to a char *, but heres how you do it...
    (char *)&monthly_interest
    I tried and it s garbled if I use that way.

    Basically, I just need a way to input a variable, and send it to that function and have it output in the color I choose..

    Thanks for response

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    void changecolor(char* word, int color){
    textcolor(color); //changes text to the color given through the argument
    cprintf("%s", word); //outputs the string in the given color
    textcolor(LIGHTGRAY); //change color back to the default light gray
    } //end void changecolor --------------------------------------------------------------------------------


    tried this:


    code:------------------------------------------------------------------------ char* mon;
    mon = (char)monthly_interest;
    changecolor(mon, color);



    ++++++++++++++++++++

    I believe the person is using that function to output both text and variables if sent in as arguments, like if he went changecolor("Word", 1); or changecolor(variable, 1), it'd still do the same.

    BTW how WOULD you do that?

  6. #6
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    >> BTW how WOULD you do that?
    Actually the changecolor function's first parameter is char* which points to a string of characters. So you just convert all your variables to a string before passing it to the function.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    How would you change a given datatype into a string before passing it?

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    12
    Originally posted by Nakeerb
    How would you change a given datatype into a string before passing it?
    Thats one I want to know! lol..

  9. #9
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    You shoud've read my suggestions then and tried them out. But since you can't seem to do that here's a sample:

    Code:
    #include <stdio.h>
    
    void printString(char *blah)
    {
       printf("%s\n", blah);
    }
    
    int main()
    {
       double x = 6.925;
       char bleh[20];
    
       sprintf(bleh, "%f", x);
       printString(bleh);
    
       return 0;
    }
    Now that wasn't too hard
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  10. #10
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    Nm this post
    Last edited by Nakeerb; 10-05-2002 at 07:34 PM.

  11. #11
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    I should say, it does work, but

    Code:
    	cout.setf(ios::showpoint); 
    	cout.setf(ios::fixed); 
    	cout.precision(2);
    something that that won't work on it

  12. #12
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    Actually i think it is impossible to do that because it is a string, you can't "round a string", lol.

  13. #13
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Not sure what you're saying. But you can't mix those statements with printf. Use cout instead. You now know how to convert a variable to a string so I don't see what the problem is.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  14. #14
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    with cout you can use precision, for instance if you had a double n = 4.553535 and used the cout.precision(2), outputting that variable would make it 4.55, but you CANNOT do this with a string

  15. #15
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    There's no functions to set precision for a string. However you CAN still do it manually. Instead of printing the string, print character by character until:

    1) you've reached the end of the string '\0'
    2) or until you've reached a '.' then print 2 more characters after that if you want a precision of 2
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

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

Similar Threads

  1. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  2. C++ to C Conversion
    By dicon in forum C Programming
    Replies: 7
    Last Post: 06-11-2007, 08:38 PM
  3. Another inheritance problem
    By patricio2626 in forum C++ Programming
    Replies: 4
    Last Post: 04-08-2007, 05:26 PM
  4. Class problem
    By Lord JoNil in forum C++ Programming
    Replies: 4
    Last Post: 09-22-2006, 05:18 PM
  5. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 05:54 PM

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