Doubles to Strings for Edit Box Output
-
I'm writing a graphing program that draws x- and y- axis in a picture box and I want to label some of the hash marks. I cheated a little and used read only edit controls for the labels. My problem is this - I need to convert the array of labels (doubles) to an array of strings so that I can send them to the edit controls for display in the picture box. I've tried the
CString.Format
function andToString
function and even_gcvt
but I can't seem to get any of them to do what I want. Can anyone help? Here my button click event that is supposed to handle this:void CGraphView::OnBnClickedBtnplot() { double* Labels; GetDlgItem(IDC_PICT)->GetWindowRect(pictRect); ScreenToClient(pictRect); //Labels [1 - 26] (one for each hash mark) //Don't use all of them though Labels = GetLabelVals(); SetDlgItemText(IDC_EDITINTERSECT, "INT"); SetDlgItemText(IDC_EDITX1, str); SetDlgItemText(IDC_EDITX2, "x2"); SetDlgItemText(IDC_EDITX3, "x3"); SetDlgItemText(IDC_EDITX4, "x4"); SetDlgItemText(IDC_EDITX5, "x5"); SetDlgItemText(IDC_EDITY1, "y1"); SetDlgItemText(IDC_EDITY2, "y2"); SetDlgItemText(IDC_EDITY3, "y3"); InvalidateRect(pictRect); }
Instead of the "x1...y3" I want the label strings to go in. Any help would be appreciated! "It has become appallingly obvious that our technology has exceeded our humanity." - Albert Einstein (1879-1955) "I think there is a world market for maybe five computers." - Thomas Watson (1874-1956), Chairman of IBM, 1943 "640K ought to be enough for anybody." - Bill Gates (1955-), in 1981 "Half this game is ninety percent mental." - Yogi Berra -
I'm writing a graphing program that draws x- and y- axis in a picture box and I want to label some of the hash marks. I cheated a little and used read only edit controls for the labels. My problem is this - I need to convert the array of labels (doubles) to an array of strings so that I can send them to the edit controls for display in the picture box. I've tried the
CString.Format
function andToString
function and even_gcvt
but I can't seem to get any of them to do what I want. Can anyone help? Here my button click event that is supposed to handle this:void CGraphView::OnBnClickedBtnplot() { double* Labels; GetDlgItem(IDC_PICT)->GetWindowRect(pictRect); ScreenToClient(pictRect); //Labels [1 - 26] (one for each hash mark) //Don't use all of them though Labels = GetLabelVals(); SetDlgItemText(IDC_EDITINTERSECT, "INT"); SetDlgItemText(IDC_EDITX1, str); SetDlgItemText(IDC_EDITX2, "x2"); SetDlgItemText(IDC_EDITX3, "x3"); SetDlgItemText(IDC_EDITX4, "x4"); SetDlgItemText(IDC_EDITX5, "x5"); SetDlgItemText(IDC_EDITY1, "y1"); SetDlgItemText(IDC_EDITY2, "y2"); SetDlgItemText(IDC_EDITY3, "y3"); InvalidateRect(pictRect); }
Instead of the "x1...y3" I want the label strings to go in. Any help would be appreciated! "It has become appallingly obvious that our technology has exceeded our humanity." - Albert Einstein (1879-1955) "I think there is a world market for maybe five computers." - Thomas Watson (1874-1956), Chairman of IBM, 1943 "640K ought to be enough for anybody." - Bill Gates (1955-), in 1981 "Half this game is ninety percent mental." - Yogi BerraWhy doesn't CString.Format do what you want...it will give you a formatted string. If you want an array of strings then check out CStringArray. Gary Kirkham A working Program is one that has only unobserved bugs I thought I wanted a career, turns out I just wanted paychecks
-
Why doesn't CString.Format do what you want...it will give you a formatted string. If you want an array of strings then check out CStringArray. Gary Kirkham A working Program is one that has only unobserved bugs I thought I wanted a career, turns out I just wanted paychecks
Would it then look like this?
void CGraphView::OnBnClickedBtnplot() { CStringArray strings; strings.SetSize(26); double* Labels; GetDlgItem(IDC_PICT)->GetWindowRect(pictRect); ScreenToClient(pictRect); //Labels [1 - 26] (one for each hash mark) //Don't use all of them though Labels = GetLabelVals(); strings[1].Format("%.3d\n", Labels[1]); SetDlgItemText(IDC_EDITINTERSECT, "INT"); SetDlgItemText(IDC_EDITX1, strings[1]); SetDlgItemText(IDC_EDITX2, "x2"); SetDlgItemText(IDC_EDITX3, "x3"); SetDlgItemText(IDC_EDITX4, "x4"); SetDlgItemText(IDC_EDITX5, "x5"); SetDlgItemText(IDC_EDITY1, "y1"); SetDlgItemText(IDC_EDITY2, "y2"); SetDlgItemText(IDC_EDITY3, "y3"); InvalidateRect(pictRect); }
Am I using the .Format correctly? The output on the screen is not correct. If Labels[1] is 10.26356897 then I get something like -85478 or something like that in the edit control when i run it. Could you give me an example of what it would take for my output to look like this: 10.3 I don't have a lot of space in the picture box so the output needs to be rounded to have only one number after the decimal place. "It has become appallingly obvious that our technology has exceeded our humanity." - Albert Einstein (1879-1955) "I think there is a world market for maybe five computers." - Thomas Watson (1874-1956), Chairman of IBM, 1943 "640K ought to be enough for anybody." - Bill Gates (1955-), in 1981 "Half this game is ninety percent mental." - Yogi Berra -
Would it then look like this?
void CGraphView::OnBnClickedBtnplot() { CStringArray strings; strings.SetSize(26); double* Labels; GetDlgItem(IDC_PICT)->GetWindowRect(pictRect); ScreenToClient(pictRect); //Labels [1 - 26] (one for each hash mark) //Don't use all of them though Labels = GetLabelVals(); strings[1].Format("%.3d\n", Labels[1]); SetDlgItemText(IDC_EDITINTERSECT, "INT"); SetDlgItemText(IDC_EDITX1, strings[1]); SetDlgItemText(IDC_EDITX2, "x2"); SetDlgItemText(IDC_EDITX3, "x3"); SetDlgItemText(IDC_EDITX4, "x4"); SetDlgItemText(IDC_EDITX5, "x5"); SetDlgItemText(IDC_EDITY1, "y1"); SetDlgItemText(IDC_EDITY2, "y2"); SetDlgItemText(IDC_EDITY3, "y3"); InvalidateRect(pictRect); }
Am I using the .Format correctly? The output on the screen is not correct. If Labels[1] is 10.26356897 then I get something like -85478 or something like that in the edit control when i run it. Could you give me an example of what it would take for my output to look like this: 10.3 I don't have a lot of space in the picture box so the output needs to be rounded to have only one number after the decimal place. "It has become appallingly obvious that our technology has exceeded our humanity." - Albert Einstein (1879-1955) "I think there is a world market for maybe five computers." - Thomas Watson (1874-1956), Chairman of IBM, 1943 "640K ought to be enough for anybody." - Bill Gates (1955-), in 1981 "Half this game is ninety percent mental." - Yogi Berrastudent() wrote: Would it then look like this? Yes, however keep in mind that C arrays are 0 based...when you add a string to the CStringArray it will start at the 0 index. You can either handle it with the index or add a dummy string to the 0th location. student() wrote: Am I using the .Format correctly? No, change the d to an f Gary Kirkham A working Program is one that has only unobserved bugs I thought I wanted a career, turns out I just wanted paychecks
-
Would it then look like this?
void CGraphView::OnBnClickedBtnplot() { CStringArray strings; strings.SetSize(26); double* Labels; GetDlgItem(IDC_PICT)->GetWindowRect(pictRect); ScreenToClient(pictRect); //Labels [1 - 26] (one for each hash mark) //Don't use all of them though Labels = GetLabelVals(); strings[1].Format("%.3d\n", Labels[1]); SetDlgItemText(IDC_EDITINTERSECT, "INT"); SetDlgItemText(IDC_EDITX1, strings[1]); SetDlgItemText(IDC_EDITX2, "x2"); SetDlgItemText(IDC_EDITX3, "x3"); SetDlgItemText(IDC_EDITX4, "x4"); SetDlgItemText(IDC_EDITX5, "x5"); SetDlgItemText(IDC_EDITY1, "y1"); SetDlgItemText(IDC_EDITY2, "y2"); SetDlgItemText(IDC_EDITY3, "y3"); InvalidateRect(pictRect); }
Am I using the .Format correctly? The output on the screen is not correct. If Labels[1] is 10.26356897 then I get something like -85478 or something like that in the edit control when i run it. Could you give me an example of what it would take for my output to look like this: 10.3 I don't have a lot of space in the picture box so the output needs to be rounded to have only one number after the decimal place. "It has become appallingly obvious that our technology has exceeded our humanity." - Albert Einstein (1879-1955) "I think there is a world market for maybe five computers." - Thomas Watson (1874-1956), Chairman of IBM, 1943 "640K ought to be enough for anybody." - Bill Gates (1955-), in 1981 "Half this game is ninety percent mental." - Yogi BerraDisregard the first part of my last post...I didn't see that you had used SetSize. Since the array is zero based, in order to start at an index of 1 then you will need to make the array size one larger than the number of items you are storing. Gary Kirkham A working Program is one that has only unobserved bugs I thought I wanted a career, turns out I just wanted paychecks
-
student() wrote: Would it then look like this? Yes, however keep in mind that C arrays are 0 based...when you add a string to the CStringArray it will start at the 0 index. You can either handle it with the index or add a dummy string to the 0th location. student() wrote: Am I using the .Format correctly? No, change the d to an f Gary Kirkham A working Program is one that has only unobserved bugs I thought I wanted a career, turns out I just wanted paychecks
Alright! That worked perfectly! I REALLY appreciate the help. "It has become appallingly obvious that our technology has exceeded our humanity." - Albert Einstein (1879-1955) "I think there is a world market for maybe five computers." - Thomas Watson (1874-1956), Chairman of IBM, 1943 "640K ought to be enough for anybody." - Bill Gates (1955-), in 1981 "Half this game is ninety percent mental." - Yogi Berra
-
Disregard the first part of my last post...I didn't see that you had used SetSize. Since the array is zero based, in order to start at an index of 1 then you will need to make the array size one larger than the number of items you are storing. Gary Kirkham A working Program is one that has only unobserved bugs I thought I wanted a career, turns out I just wanted paychecks
One last question.. Why will this work...
strLabels[3].Format("%.2f\n", dblLabels[3]);
But this won't?:wtf:for (int i = 1; i < 26; i++) strLabels[i].Format("%.2f\n", dblLabels[i]);
"It has become appallingly obvious that our technology has exceeded our humanity." - Albert Einstein (1879-1955) "I think there is a world market for maybe five computers." - Thomas Watson (1874-1956), Chairman of IBM, 1943 "640K ought to be enough for anybody." - Bill Gates (1955-), in 1981 "Half this game is ninety percent mental." - Yogi Berra -
One last question.. Why will this work...
strLabels[3].Format("%.2f\n", dblLabels[3]);
But this won't?:wtf:for (int i = 1; i < 26; i++) strLabels[i].Format("%.2f\n", dblLabels[i]);
"It has become appallingly obvious that our technology has exceeded our humanity." - Albert Einstein (1879-1955) "I think there is a world market for maybe five computers." - Thomas Watson (1874-1956), Chairman of IBM, 1943 "640K ought to be enough for anybody." - Bill Gates (1955-), in 1981 "Half this game is ninety percent mental." - Yogi BerraI don't see anything wrong with what you posted. What does it do? Gary Kirkham A working Program is one that has only unobserved bugs I thought I wanted a career, turns out I just wanted paychecks
-
I don't see anything wrong with what you posted. What does it do? Gary Kirkham A working Program is one that has only unobserved bugs I thought I wanted a career, turns out I just wanted paychecks
The output on the screen shows -9255 for every string no matter what is in my dblLabels array when I use a for loop. If I put the literal index in it works but that would be too many lines of code. Heres the whole thing again just in case:
void CGraphView::OnBnClickedBtnplot() { GetDlgItem(IDC_PICT)->GetWindowRect(pictRect); ScreenToClient(pictRect); //Array of label strings CStringArray strLabels; strLabels.SetSize(26); //Array of double labels [1-26] one for each has mark //Don't use all of them though double* dblLabels = GetLabelVals(); for (int i = 1; i < 26; i++) strLabels[i].Format("%.2f\n", dblLabels[i]); SetDlgItemText(IDC_EDITINTERSECT, "INT"); SetDlgItemText(IDC_EDITY1, strLabels[3]); SetDlgItemText(IDC_EDITY2, strLabels[6]); SetDlgItemText(IDC_EDITY3, strLabels[9]); SetDlgItemText(IDC_EDITX1, strLabels[12]); SetDlgItemText(IDC_EDITX2, strLabels[15]); SetDlgItemText(IDC_EDITX3, strLabels[18]); SetDlgItemText(IDC_EDITX4, strLabels[21]); SetDlgItemText(IDC_EDITX5, strLabels[24]); InvalidateRect(pictRect); }
"It has become appallingly obvious that our technology has exceeded our humanity." - Albert Einstein (1879-1955) "I think there is a world market for maybe five computers." - Thomas Watson (1874-1956), Chairman of IBM, 1943 "640K ought to be enough for anybody." - Bill Gates (1955-), in 1981 "Half this game is ninety percent mental." - Yogi Berra -
The output on the screen shows -9255 for every string no matter what is in my dblLabels array when I use a for loop. If I put the literal index in it works but that would be too many lines of code. Heres the whole thing again just in case:
void CGraphView::OnBnClickedBtnplot() { GetDlgItem(IDC_PICT)->GetWindowRect(pictRect); ScreenToClient(pictRect); //Array of label strings CStringArray strLabels; strLabels.SetSize(26); //Array of double labels [1-26] one for each has mark //Don't use all of them though double* dblLabels = GetLabelVals(); for (int i = 1; i < 26; i++) strLabels[i].Format("%.2f\n", dblLabels[i]); SetDlgItemText(IDC_EDITINTERSECT, "INT"); SetDlgItemText(IDC_EDITY1, strLabels[3]); SetDlgItemText(IDC_EDITY2, strLabels[6]); SetDlgItemText(IDC_EDITY3, strLabels[9]); SetDlgItemText(IDC_EDITX1, strLabels[12]); SetDlgItemText(IDC_EDITX2, strLabels[15]); SetDlgItemText(IDC_EDITX3, strLabels[18]); SetDlgItemText(IDC_EDITX4, strLabels[21]); SetDlgItemText(IDC_EDITX5, strLabels[24]); InvalidateRect(pictRect); }
"It has become appallingly obvious that our technology has exceeded our humanity." - Albert Einstein (1879-1955) "I think there is a world market for maybe five computers." - Thomas Watson (1874-1956), Chairman of IBM, 1943 "640K ought to be enough for anybody." - Bill Gates (1955-), in 1981 "Half this game is ninety percent mental." - Yogi BerraShould work... Run it in debug mode and check each value set by this call student() wrote: double* dblLabels = GetLabelVals(); make sure each value in dblLabels is set properly. I ran the following on my machine at it worked perfectly
CStringArray strLabels; strLabels.SetSize(26); double arr\[\] = { 0 , 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 18, 20, 21, 22, 23, 26, 25 }; for (int i = 1; i < 26; i++) { strLabels\[i\].Format("%.2f\\n", arr\[i\]); AfxMessageBox(strLabels\[i\]); }
Other than that I don't know what to tell you Gary Kirkham A working Program is one that has only unobserved bugs I thought I wanted a career, turns out I just wanted paychecks
-
Should work... Run it in debug mode and check each value set by this call student() wrote: double* dblLabels = GetLabelVals(); make sure each value in dblLabels is set properly. I ran the following on my machine at it worked perfectly
CStringArray strLabels; strLabels.SetSize(26); double arr\[\] = { 0 , 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 18, 20, 21, 22, 23, 26, 25 }; for (int i = 1; i < 26; i++) { strLabels\[i\].Format("%.2f\\n", arr\[i\]); AfxMessageBox(strLabels\[i\]); }
Other than that I don't know what to tell you Gary Kirkham A working Program is one that has only unobserved bugs I thought I wanted a career, turns out I just wanted paychecks
Gary... Yeah something weird is going on. I ran it in the debugger and my dbl array is fine. The weird part is that after only one iteration of the for loop, all 26 of the values in the string array are set to -9255 plus about ten other numbers after that. I don't understand how one iteration could change all the values in the array. Anyway..Thanks for your help..I really appreciate it. "It has become appallingly obvious that our technology has exceeded our humanity." - Albert Einstein (1879-1955) "I think there is a world market for maybe five computers." - Thomas Watson (1874-1956), Chairman of IBM, 1943 "640K ought to be enough for anybody." - Bill Gates (1955-), in 1981 "Half this game is ninety percent mental." - Yogi Berra
-
Gary... Yeah something weird is going on. I ran it in the debugger and my dbl array is fine. The weird part is that after only one iteration of the for loop, all 26 of the values in the string array are set to -9255 plus about ten other numbers after that. I don't understand how one iteration could change all the values in the array. Anyway..Thanks for your help..I really appreciate it. "It has become appallingly obvious that our technology has exceeded our humanity." - Albert Einstein (1879-1955) "I think there is a world market for maybe five computers." - Thomas Watson (1874-1956), Chairman of IBM, 1943 "640K ought to be enough for anybody." - Bill Gates (1955-), in 1981 "Half this game is ninety percent mental." - Yogi Berra
Just for kicks, try using the CStringArray GetAt and SetAt functions instead of the [] operator. Gary Kirkham A working Program is one that has only unobserved bugs I thought I wanted a career, turns out I just wanted paychecks
-
The output on the screen shows -9255 for every string no matter what is in my dblLabels array when I use a for loop. If I put the literal index in it works but that would be too many lines of code. Heres the whole thing again just in case:
void CGraphView::OnBnClickedBtnplot() { GetDlgItem(IDC_PICT)->GetWindowRect(pictRect); ScreenToClient(pictRect); //Array of label strings CStringArray strLabels; strLabels.SetSize(26); //Array of double labels [1-26] one for each has mark //Don't use all of them though double* dblLabels = GetLabelVals(); for (int i = 1; i < 26; i++) strLabels[i].Format("%.2f\n", dblLabels[i]); SetDlgItemText(IDC_EDITINTERSECT, "INT"); SetDlgItemText(IDC_EDITY1, strLabels[3]); SetDlgItemText(IDC_EDITY2, strLabels[6]); SetDlgItemText(IDC_EDITY3, strLabels[9]); SetDlgItemText(IDC_EDITX1, strLabels[12]); SetDlgItemText(IDC_EDITX2, strLabels[15]); SetDlgItemText(IDC_EDITX3, strLabels[18]); SetDlgItemText(IDC_EDITX4, strLabels[21]); SetDlgItemText(IDC_EDITX5, strLabels[24]); InvalidateRect(pictRect); }
"It has become appallingly obvious that our technology has exceeded our humanity." - Albert Einstein (1879-1955) "I think there is a world market for maybe five computers." - Thomas Watson (1874-1956), Chairman of IBM, 1943 "640K ought to be enough for anybody." - Bill Gates (1955-), in 1981 "Half this game is ninety percent mental." - Yogi BerraYou can also us a vector. Here is an example
vector strLabels; strLabels.reserve(26); CString string; strLabels.push\_back(""); // this takes care of index 0 for (int i = 1; i < 26; i++) { string.Format("%.2f\\n", dblLabels\[i\]); strLabels.push\_back(string); }
put this at the top of you cpp file #include using std::vector; Gary Kirkham A working Program is one that has only unobserved bugs I thought I wanted a career, turns out I just wanted paychecks
-
The output on the screen shows -9255 for every string no matter what is in my dblLabels array when I use a for loop. If I put the literal index in it works but that would be too many lines of code. Heres the whole thing again just in case:
void CGraphView::OnBnClickedBtnplot() { GetDlgItem(IDC_PICT)->GetWindowRect(pictRect); ScreenToClient(pictRect); //Array of label strings CStringArray strLabels; strLabels.SetSize(26); //Array of double labels [1-26] one for each has mark //Don't use all of them though double* dblLabels = GetLabelVals(); for (int i = 1; i < 26; i++) strLabels[i].Format("%.2f\n", dblLabels[i]); SetDlgItemText(IDC_EDITINTERSECT, "INT"); SetDlgItemText(IDC_EDITY1, strLabels[3]); SetDlgItemText(IDC_EDITY2, strLabels[6]); SetDlgItemText(IDC_EDITY3, strLabels[9]); SetDlgItemText(IDC_EDITX1, strLabels[12]); SetDlgItemText(IDC_EDITX2, strLabels[15]); SetDlgItemText(IDC_EDITX3, strLabels[18]); SetDlgItemText(IDC_EDITX4, strLabels[21]); SetDlgItemText(IDC_EDITX5, strLabels[24]); InvalidateRect(pictRect); }
"It has become appallingly obvious that our technology has exceeded our humanity." - Albert Einstein (1879-1955) "I think there is a world market for maybe five computers." - Thomas Watson (1874-1956), Chairman of IBM, 1943 "640K ought to be enough for anybody." - Bill Gates (1955-), in 1981 "Half this game is ninety percent mental." - Yogi Berrathe first line of code sould look like this vector<CString>strLabels; I forgot the message software doesn't like the <> symbols Gary Kirkham A working Program is one that has only unobserved bugs I thought I wanted a career, turns out I just wanted paychecks
-
I'm writing a graphing program that draws x- and y- axis in a picture box and I want to label some of the hash marks. I cheated a little and used read only edit controls for the labels. My problem is this - I need to convert the array of labels (doubles) to an array of strings so that I can send them to the edit controls for display in the picture box. I've tried the
CString.Format
function andToString
function and even_gcvt
but I can't seem to get any of them to do what I want. Can anyone help? Here my button click event that is supposed to handle this:void CGraphView::OnBnClickedBtnplot() { double* Labels; GetDlgItem(IDC_PICT)->GetWindowRect(pictRect); ScreenToClient(pictRect); //Labels [1 - 26] (one for each hash mark) //Don't use all of them though Labels = GetLabelVals(); SetDlgItemText(IDC_EDITINTERSECT, "INT"); SetDlgItemText(IDC_EDITX1, str); SetDlgItemText(IDC_EDITX2, "x2"); SetDlgItemText(IDC_EDITX3, "x3"); SetDlgItemText(IDC_EDITX4, "x4"); SetDlgItemText(IDC_EDITX5, "x5"); SetDlgItemText(IDC_EDITY1, "y1"); SetDlgItemText(IDC_EDITY2, "y2"); SetDlgItemText(IDC_EDITY3, "y3"); InvalidateRect(pictRect); }
Instead of the "x1...y3" I want the label strings to go in. Any help would be appreciated! "It has become appallingly obvious that our technology has exceeded our humanity." - Albert Einstein (1879-1955) "I think there is a world market for maybe five computers." - Thomas Watson (1874-1956), Chairman of IBM, 1943 "640K ought to be enough for anybody." - Bill Gates (1955-), in 1981 "Half this game is ninety percent mental." - Yogi Berra#include #include char buf1[50]="",buf2[50]=""; DWORD x1=1923; float x2=0.5; sprintf(buf1,"%d",x1); sprintf(buf2,"%f",x2); x1=atol(buf1); x2=atof(buf2); quite simple ? right ! //be cool I am the mighty keeper of the book on knowledge . Contact me to get your copy .
-
the first line of code sould look like this vector<CString>strLabels; I forgot the message software doesn't like the <> symbols Gary Kirkham A working Program is one that has only unobserved bugs I thought I wanted a career, turns out I just wanted paychecks
Just thought I would let you know since you helped me so much... I didn't even need to convert to strings. I add some code to my
GetLabelVals
function and changed the name of it toSetLabels().
I didn't realize it before but you can attach a double variable to the edit controls then, I just set the var attached to each box using the corresponding element in my double array. Then to have it appear on the screen -- I callUpdateData(false).
I knew I could get the value in the box with an attached variable but I just figured out that I can set it using a variable also, using theUpdateData(false)
call. So much work just for that!! ...Brent "It has become appallingly obvious that our technology has exceeded our humanity." - Albert Einstein (1879-1955) "I think there is a world market for maybe five computers." - Thomas Watson (1874-1956), Chairman of IBM, 1943 "640K ought to be enough for anybody." - Bill Gates (1955-), in 1981 "Half this game is ninety percent mental." - Yogi Berra