|
Hello, everyone!
How to use C (not C++) to check whether a character buffer (char*) contains a double or float number? I want to know how to check it with standard C (not C++) method and not VC++ specific method.
For example,
----------
char buf1 [5] = "123.4";
char buf2 [5] = "123.m";
----------
What the result I want to get is, when I input buf1, I can get TRUE and when I input buf2, I can get FALSE.
I find the function atof is not working properly.
Thanks in advance,
Geo
|
|
|
|
|
TCHAR * end = NULL;
double x = strtod(buf, &end);
// end points to the character that stopped the scan - this should be the 0 term
if (*end != '\0' || !_finite(x))
{
// not a valid number
}
"Vierteile den, der sie Hure schimpft mit einem türkischen Säbel."
sighist | Agile Programming | doxygen
|
|
|
|
|
#include <stdio.h>
#include <stdlib.h>
Thanks, peterchen buddy!
I found your code is not working properly, Here is a piece of sample code, which should return 0, but it returns 1 instead.
Source Code:
--------
int main (int argc, char** argv)
{
char* end = NULL;
char buf [5] = "123.4";
double x = strtod (buf, &end);
if ((*end != '\0') || (!finite (x)))
{
// not valid
return 1;
}
//valid
return 0;
}
--------
Is there something wrong?
regards,
Geo
|
|
|
|
|
Try replacing char buf [5] = "123.4"; by
char *buf = "123.4";
Dominik
_outp(0x64, 0xAD);
and
__asm mov al, 0xAD __asm out 0x64, al
do the same... but what do they do??
(doesn't work on NT)
|
|
|
|
|
char* and char[] are the same according to Bjarne Stroustrup.
Thank You
Bo Hunter
|
|
|
|
|
Thanks, Bo buddy!
But I find his method is really working, when change char[] to char*. But I do not understand the reason. Can you tell me why?
regards,
Geo
|
|
|
|
|
Bo Hunter wrote:
char* and char[] are the same according to Bjarne Stroustrup.
Thats correct! But char[5] is something different!
_outp(0x64, 0xAD);
and
__asm mov al, 0xAD __asm out 0x64, al
do the same... but what do they do??
(doesn't work on NT)
|
|
|
|
|
Thanks, Dominik buddy!
Now I have understood the problem.
Geo
|
|
|
|
|
Thanks, Dominik buddy!
I find your method is working. But I do not understand the reason, can you tell me why?
regards,
Geo
|
|
|
|
|
char[5]="..." simply isn't correct C. You can either use char[] or char*.
Even if it would be correct C, char[5]="123.4" would possibly result in a memory read violation because you allocate 5 characters and put 5 characters in it, but where do you put the ending NULL character? So, this causes a read violation, because the strtod function cannot find the NULL character.
I wonder why your compiler accepted char[5]="...". My compiler doesn't compile this.
_outp(0x64, 0xAD);
and
__asm mov al, 0xAD __asm out 0x64, al
do the same... but what do they do??
(doesn't work on NT)
|
|
|
|
|
Thanks, Dominik buddy!
How silly I am.
Geo
|
|
|
|
|
What's the difference between C's printf and C++'s cout?
I have heard that C does not support String, so is it
much difficult to output data in C than in C++?
|
|
|
|
|
printf is not typesafe or extensible, whereas cout is. This means that C++ output is easier to get right (once you get over the odd syntax), and the output of complex objects can be deferred into those objects, thus improving encapsulation. However, printf can be handy for quick apps, and most programmers seem to prefer "format string" based IO (such as printf) to "stream oriented" IO (such as cout). Hence why everyone uses sprintf to construct strings, even though stringstream objects are safer
--
Ian Darling
"The moral of the story is that with a contrived example, you can prove anything." - Joel Spolsky
|
|
|
|
|
Added to that, positional format strings, as supported by Windows' FormatMessage (and .NET's String.Format ) are easier to translate for localized versions of the software.
cout doesn't have an answer to this.
I'll admit that since I mostly write C++ software for Pocket PCs, I don't often have the chance to use the C++ runtime library, because most of it's missing.
|
|
|
|
|
|
Ian Darling wrote:
Hence why everyone uses sprintf to construct strings, even though stringstream objects are safer
sprintf is convenient but we really ought to shun it for the reason you say. In the past I've run into nasty memory bugs in maintenance because of developers' fondness for sprintf.
Kevin
|
|
|
|
|
Guys,
I have noticed - CHtmlView sometimes does not process correctly if form does not have submit button, Enter key does not submit.
This form will work perfectly - submit on Enter key:
<br />
<form name="myform" onSubmit="return cmd();" ><br />
<input type="text" class="form-say" name="say"><br />
<input type="submit" class="form-say" name="say" ><br />
</form>
And this one will just ignore Enter key (but in IE works fine):
<br />
<form name="myform" onSubmit="return cmd();" ><br />
<input type="text" class="form-say" name="say" ><br />
</form>
Do you know the way to work around it?
Thanks
PS - I'm using MFC with good old VC++ 6.0 . Maybe is it fixed in newer versions ?
|
|
|
|
|
Maybe some JavaScript that does the Submit, or a hidden Submit button, assuming that is possible.
PS. I don't thing this is a CHtmlView issue.
Neville Franks, Author of ED for Windows. Free Trial at www.getsoft.com
|
|
|
|
|
Neville Franks wrote:
Maybe some JavaScript that does the Submit, or a hidden Submit button, assuming that is possible.
PS. I don't thing this is a CHtmlView issue.
The problem is - I'm writing multy-window browser, so I have to process pages "as is", and this particular example - part of IRC:CGI, so I have to work with "submit-less" forms correctly.
I believe it is CHtmlView issue (not mine), because I've created simple Document/View CHtmlView-based project by wizard, and opened test "submit-less" page without any code modification - same result - it does not sumbit.
|
|
|
|
|
trof wrote:
I believe it is CHtmlView issue (not mine), because I've created simple Document/View CHtmlView-based project by wizard, and opened test "submit-less" page without any code modification - same result - it does not sumbit.
This doesn't surprise me. Why not modify the HTML after it is loaded. ie. in xxx::OnDocumentComplete().
Neville Franks, Author of ED for Windows. Free Trial at www.getsoft.com
|
|
|
|
|
Hi to all. I'm trying to programm Macromedia's Flash ocx under Visual C++. It is known that this control cannot support transparent background. But there are in the market third party programms that enable transparency in the control. Can give me a help how to make transparent the background of the control?
Thank's bros.
|
|
|
|
|
hi...
can u pls tell me how to get the handle of DLG from a totally different class???
i have a class that does some function and then at tat point i need to access my main dialog....actually i need to access a static control of that dialog...
can anyone pls tell me a solution???
tks a lot
Have a Super Blessed Day!
-------------------------
For God has not given us a spirit of fear, but of power and of love and of a sound mind.
2 Timothy 1:7
"For God so loved the world that He gave His only begotten Son, that whoever believes in Him should not perish but have everlasting life."
John 3:16
"Therefore you also be ready, for the Son of Man is coming at an hour you do not expet."
Luke 12:40
|
|
|
|
|
can anyone gimme an answer??
Have a Super Blessed Day!
-------------------------
For God has not given us a spirit of fear, but of power and of love and of a sound mind.
2 Timothy 1:7
"For God so loved the world that He gave His only begotten Son, that whoever believes in Him should not perish but have everlasting life."
John 3:16
"Therefore you also be ready, for the Son of Man is coming at an hour you do not expet."
Luke 12:40
|
|
|
|
|
When your dialog is created, store its class instance in a static member variable in the dialog class. Create a static function that returns that instance.
So, if your dialog class was named CDlg, then to get the dialog, you could do:
CDlg* dialog;
dialog=Dlg.GetMainDialog();
HTH
"To know what is right and not do it is the worst cowardice." -- Confucius
FLUID UI Toolkit
|
|
|
|
|
my dialog and my class are far far away...
my dlg calls a class by returning ans int to ir(this class is not a dlg)
then this class has to call another class..
this another class needs to access the dilog...
now can i pass the dlg to the class as the class is created by returning a valu..
and C++ only allows returning one param...
I HAVE TO USE RETURNING to to create the class...
so i cant substitute it with anything else
Have a Super Blessed Day!
-------------------------
For God has not given us a spirit of fear, but of power and of love and of a sound mind.
2 Timothy 1:7
"For God so loved the world that He gave His only begotten Son, that whoever believes in Him should not perish but have everlasting life."
John 3:16
"Therefore you also be ready, for the Son of Man is coming at an hour you do not expet."
Luke 12:40
|
|
|
|