Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,048 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,561 people online right now. Registration is fast and FREE... Join Now!




Display inputted double variable in Window App

 
Reply to this topicStart new topic

Display inputted double variable in Window App, WinMain program displays a double variable

rtokars
18 Jul, 2008 - 10:37 AM
Post #1

New D.I.C Head
*

Joined: 18 Jul, 2008
Posts: 18

Hello, I'm trying to display a double variable via a window. Basically all I could find is the TextOut function, but this requires a string variable, thus i converted the double to a string with the following code:

CODE
    HDC drawing_context = GetDC(hwnd);
    char Cont1Str[] = "Max stdev = ";
    char Cont2Str[] = "000.000";
    int stdevmaxL=stdevmax;
    int stdevmaxR=stdevmax*1000;
    stdevmaxR=stdevmaxR%1000;
    Cont2Str[0]+=stdevmaxL/100;
    Cont2Str[1]+=stdevmaxL%100/10;
    Cont2Str[2]+=stdevmaxL%100%10;
    Cont2Str[4]+=stdevmaxR/100;
    Cont2Str[5]+=stdevmaxR%100/10;
    Cont2Str[6]+=stdevmaxR%100%10;
    TextOut(drawing_context,275,77,Cont1Str,sizeof(Cont1Str)-1);
    TextOut(drawing_context,360,77,Cont2Str,sizeof(Cont2Str)-1);


This code is located in the WndProc function with the corresponding case (when the user hits a button it displays the inputted double variable on the window). I'm able to get the double input just fine. I'm hoping someone has some better ideas to outputting double variables to windows. It just seems my way is rather limited (for one thing the values are limited to positive values with max 999 and min inc of 0.001). I'm thinking I should use the itoa function, but I didnt get it to work as of the present. Anyone have any easy solutions? Thanks in advance.


User is offlineProfile CardPM
+Quote Post

Cerolobo
RE: Display Inputted Double Variable In Window App
18 Jul, 2008 - 10:41 AM
Post #2

D.I.C Regular
Group Icon

Joined: 5 Apr, 2008
Posts: 440



Thanked: 31 times
My Contributions
sprintf() is great for things like this. You have to worry about buffer overflows though.

CODE
char str[100];
sprintf(str, "%6.2f", 10.123456789);
printf("%s\n", str); // prints out "10.12"


Once you do that, you should be able to feed the string to TextOut()

Edit: If your using C++, you can use something like stringstream, which works just like cout. IE, you can do this

CODE
stringstream str;
str << setprecision(2) << 10.123456789 << endl;
// str contains "10.12"


which will remove the buffer overflow issue.

This post has been edited by Cerolobo: 18 Jul, 2008 - 10:50 AM
User is offlineProfile CardPM
+Quote Post

rtokars
RE: Display Inputted Double Variable In Window App
18 Jul, 2008 - 10:48 AM
Post #3

New D.I.C Head
*

Joined: 18 Jul, 2008
Posts: 18

brilliant, thanks =)
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 05:31PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month