Hi,
I want to excuse myself if my question may be lame, I am still a student,
So I am making a calculator and the problem appears when a
float value is entered in the dialog box (as CString) with ","
How can I replace it with "." and covert it to double?
My CString is testStr("123,54") I want to make it double d=123.54
I am using VS 6.0
Thanks in advance!
10 4561
Hi,
I want to excuse myself if my question may be lame, I am still a student,
So I am making a calculator and the problem appears when a
float value is entered in the dialog box (as CString) with ","
How can I replace it with "." and covert it to double?
My CString is testStr("123,54") I want to make it double d=123.54
I am using VS 6.0
Thanks in advance!
Because strings are in fact arrays of chars,you need to serach for "," apperance
and then replace it with "."
Savage
Converting it to a double may be harder, though - there may be a function defined in some obscure library. I know the function atoi() converts a char (or char array?) to an integer - maybe there is an atof() or atod() function for floating point numbers.
Yes like the Gannon 11 said there is a function atof(); and it has the following syntax: double atof( const char *s);
and there is also a function long double _atold(const char *(s));,which converts to long double;
You could also use strtod.
strtod is similiar to atof,but it provides better error detection than atof();
Here is the syntax: double strtod(const char *s,char **endptr);
Note:If endtpr is not null,this function set *endtpr to point to the char that stoped scan.(This is usefull for error detection)
Savage
Thank you for the help, it worked. But as I convert it to double I still have problems with replacing the "," with "."
I've done it with string but I couldn't do it with CString.
When you were using a string, how did you find the comma?
I did it with - string str;
-
for(int i=0; i<20; i++){
-
if(str[i]==',' ){ str[i]='.' ;}
-
}
in a c++ source file, and it worked.
But when I tryed in MFC project with: - CString str="5433,54";
-
for(int i=0; i<20; i++){
-
if(str[i]==','){
-
str[i]='.'; //it finds is the error here
-
}
-
}
the error was:
error C2106: '=' : left operand must be l-value
CStrings are most likely unchangeable, then - one you set it to a value you cannot change the comma. Maybe you could copy the original CString into a new CString character for character, until you get to the comma (when you add a period instead of a comma).
The CString::operator[] will only retreive a character.
Use the CString::SetAt() method to change a character.
Thanks weaknessforcats. At the moment I can't try it but I hope it'll work.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: sqwirx |
last post by:
I am using a String that has a member function c_str(). Because I
think c_str() reduces the readability of the code and I don't want
implicit conversions, I'm concidering to add a "char const *...
|
by: Oskar |
last post by:
Hi.
I`m new in cpp and i have a litlle problem.
i have a CString from Edit Box (eg."aaa bbb ccc 7327373 d feaf 323 dvjiv
234") and i want to put the data (space separated) into an array.It shuld...
|
by: nsyforce |
last post by:
What is the correct way to convert a const char* to a CString? I'm
somewhat of a newbie and have tried several ways. While they all
convert ok, I'm using a profiler that shows a memory leak for...
|
by: thehuby |
last post by:
Isn't inserting good data and getting it out of a db a pain in the a$$?
I am going to be using the Markdown text to HTML parser
(http://daringfireball.net/projects/markdown/dingus) for creating...
|
by: huguogang |
last post by:
Just curious, any one know what the 3 part parameter "class CString
filename" would mean.
The code:
int TestFunc(class CString filename)
{
fopen(filename, "w");
}
Compile using Visaul C++,...
|
by: Susan Rice |
last post by:
I'm new to using CString. Why won't the following compile?
I'm using Microsoft Visual C++ 6.0
Line 37 which it complains about is the function:
37 CString ConvertFile(char *szFileName)
I...
|
by: meisterbartsch |
last post by:
Hi NG,
I have got a starting pointer of a char series (mxArrayToString(pa)).
I also know the length (number of elements by mxGetNumberOfElements) I
want to read.
How do I get a Cstring from...
|
by: Donos |
last post by:
I have a CString with 100 characters. Now i want to make that to 2
lines.
For example,
CString str = "This is just a test to find out how to break a
cstring";
I want this CString in the...
|
by: Nick |
last post by:
I've seen a few frameworks use the following:
function $(id) { return document.getElementById(id); }
Then to use:
$('something').innerHTML = 'blah';
I'm just trying to roll this out to my...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |