inserting into a string.

I'm learning to program windows API and would like some advice or perhaps code example on how to reinsert the decimal point from _fcvt. Any alternatives to _fcvt?

I previously have written code to insert characters by shifting an array but surely there is a better way.

I have tried the example from http://www.cplusplus.com/reference/string/string/insert.html and was unable to get it to work. It would compile but beyond that it was a no go.

If it matters I'm on DevC++ 4.9.9.2.

A portion of my code is as follows.

1
2
3
4
5
6
GetDlgItemText(hwnd, 2, szEditbox,10);
holder = atof( szEditbox );
holder = holder; //maybe do some math here
char *number;
number = _fcvt( holder, 2, &decimal, &sign );
SetDlgItemText(hwnd, 3, number);



cheers,
Shane
well... i wouldn't even bother with those functions if i could help it. stringstreams work great for conversions between strings and numbers. assuming that 'szEditBox' holds valid data after your call to 'GetDlgItemText()' ( error checking is always a good idea with win32 ) i would do something like this:

1
2
3
4
5
6
7
float N;
stringstream sstr;
sstr << szEditBox;
sstr >> N;
// do your math
sstr << N;
SetDlgItemText( hwnd, 3, sstr.str().c_str() );


hope this helped! regards.
I will give it a try tomorrow morning and post the results. It certainly is much more graceful than the code I was working on.
A different approach is always welcome.

much thanks!
I have been working on the code and have a couple questions.

Trying to understand the details of the code.
I added the includes and namespace etc. and plugged in the code as follows:

1
2
3
4
5
6
7
8
GetDlgItemText(hwnd, 2, szEditbox,10);
float N;
stringstream sstr;
sstr << 'szEditBox';
sstr >> N;
N = N+100;// do your 
sstr << N;
SetDlgItemText( hwnd, 3, sstr.str().c_str() );


I added single quotes on the line : sstr << szEditBox; It will compile at this point however it throws a warning: character constant too long for it's type. The output is a integer of ten digits in length. No matter what is in szEditbox it will still place the same ten digit number in the child window.

Is this a unicode or wchar type of problem?

As a experiment in understanding I revised the code to do the following:

1
2
3
4
N = N+100;// do your math
sstr << N;
sstr << szEditbox;
SetDlgItemText( hwnd, 3, szEditbox);


The output here is the original number from child window not the expected N+100.

Ideas?


Last edited on
in your first code segment there, you have sstr << 'szEditBox';. by putting single quotes around it, you are telling the compiler that you want to insert a character... however, szEditBox, if i'm not mistaken, is a char*. a char is 8 bits, but a char* is 32 if i remember correctly, so you are trying to cast a 32 bit data type down to 8 bits. if you remove the single quotes so that you have sstr << szEditBox; it should work just fine.

in your second code snippet, your problem is that when you call SetDlgItemText(), look at what you are passing to it as the text to set... szEditbox. what does szEditbox contain? your original value from your call to GetDlgItemText()! so when you call SetDlgItemText(), make sure you actually give it the correct text to display.

here is a page on the insertion operator '<<':
http://www.cplusplus.com/reference/iostream/ostream/operator%3C%3C.html

and if you want, here is the page on stringstreams:
http://www.cplusplus.com/reference/iostream/stringstream/
I have been tinkering with the code and reading on the site for a couple hours this afternoon and have been looking at the pages in the reference. I originally added the single quotes to sstr << 'szEditBox'; due that it refused to compile otherwise. I just tried it again without the quotes and it compiles. Not sure why it did not compile at this point since I have changed the code experimenting with various things from the pages on this site.

in the second snippet I omitted the write back to fill szEditbox with the updated number... Opps my bad. :-)

Now that it compiles I do have a question.

1
2
3
4
5
6
7
8
GetDlgItemText(hwnd, 2, szEditbox,10);
float N;
stringstream sstr;
sstr << szEditbox;
sstr << N;
N = N*2;// do your math
sstr >> N;
SetDlgItemText( hwnd, 3, sstr.str().c_str() );


If the number entered is 25 the number is 250.
The lines N = N*2; and N = N+2; do the same thing.
Why is this so?


Much thanks for all the help.
glad i could help :)

another problem of tracing back through your code i think. look at lines 5, and 7 of your last post. instead of inserting then extracting, you need to extract to 'N' after inserting szEditbox first, then insert it back into the stream after the math. just a simple mistake :)
Last edited on
The reason that the << and >> from lines 5 and 7 were the other way around is that I swapped them to check to see if that was the original cause of my problem. There is some underlying problem that I have not sussed out yet. The output with the << >> swapped back proper is just a copy of the original numbers and ignores the math except when you enter something like 3.3. then the output will be 3.3.6.6 ??
I have set the precision with sstr.precision(10);
and the change is input -- 3.3. output -- 3.3.6.599999905 ??

I'm learning that windows is more complicated than the embedded programming. I plan to devote about six hours tomorrow toward the string library and hope that will get me better acquainted.

cheers
Last edited on
aha, i believe this is caused by line 4... what i forgot myself, is that initially, we put 'szEditbox' into the stream, so after we extract and then insert the number again, we have not cleared 'szEditbox' from the string, thus, it is still in there, when you render it to the control! sorry, that was my fault. I think that a simple call to sstr.flush() should clear the content before you insert your new value back into the stream, and fix the problem.
I tried the sstr.flush() and it's not working for me. Not sure why yet, but it seems to me that .flush() is for file stream? It did give me a few ideas to try though. This is what I have done so far. Below I have left commented in the ideas I have tried. Each in turn had an input/output as described below. Could this be a problem with how the cast is being done? It seems to me that an extra "." acts as a delimiter of sorts and is not needed for clear.

1
2
3
4
5
6
7
8
9
10
11
12
13
GetDlgItemText(hwnd, 2, szEditbox,10);
float num;
stringstream sstr;
//sstr.precision(10);
sstr << szEditbox;
sstr >> num;
    //sstr.flush();
    //sstr << flush;
    //sstr.clear();
    //sstr << "T";
num = num * 2;// do your math
sstr << num;
SetDlgItemText( hwnd, 3, sstr.str().c_str() );


For lines 7 and 8 sstr.flush(); and sstr << flush;
The input/output is the same as if the lines 7 or 8 were not there. No change.
Input: 1.. Output: 1..2
Input: 1.1 Output: 1.1
Input: 1.1. Output: 1.1.2.2
Input: 1.1.1 Output: 1.1.12.2

For line 9
Input: 1 Output: 12
Input: 1. Output: 1.2
Input: 1.1 Output: 1.12.2
Input: .001 Output: .0010.002

For line 10 I experimented to get a look at it with a non numeric input.
I also tried sstr << NULL; with a similar result.
Input: 1 Output: 1
Input: 1. Output: 1.
Input: 1.. Output: 1..T2
Input: 1..1 Output: 1..1T2
Okay, I have found a fix.

1
2
3
4
5
6
7
8
9
10
11
GetDlgItemText(hwnd, 2, szEditbox,10);
float num;
stringstream sstr;
//sstr.precision(10);
sstr << szEditbox;
sstr >> num;
sstr.clear();
num = num * 2;// do your math
sstr << num;
sstr >> szEditbox;
SetDlgItemText( hwnd, 3, szEditbox);


I don't know yet why this works and there is a little issue if you put in an extra "." which I can fix with an validating function.

Any ideas?
Topic archived. No new replies allowed.