473,320 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Replacing CString elements

5
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!
Apr 2 '07 #1
10 4561
Savage
1,764 Expert 1GB
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
Apr 2 '07 #2
Ganon11
3,652 Expert 2GB
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.
Apr 2 '07 #3
Savage
1,764 Expert 1GB
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
Apr 2 '07 #4
mecho
5
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.
Apr 2 '07 #5
Ganon11
3,652 Expert 2GB
When you were using a string, how did you find the comma?
Apr 3 '07 #6
mecho
5
I did it with
Expand|Select|Wrap|Line Numbers
  1. string str;
  2. for(int i=0; i<20; i++){
  3.   if(str[i]==',' ){ str[i]='.' ;}
  4. }
in a c++ source file, and it worked.
But when I tryed in MFC project with:
Expand|Select|Wrap|Line Numbers
  1. CString str="5433,54";
  2.     for(int i=0; i<20; i++){
  3.         if(str[i]==','){
  4.             str[i]='.'; //it finds is the error here
  5.         }
  6.     }
the error was:
error C2106: '=' : left operand must be l-value
Apr 4 '07 #7
Ganon11
3,652 Expert 2GB
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).
Apr 4 '07 #8
mecho
5
Thanks Ganon11.
Apr 5 '07 #9
weaknessforcats
9,208 Expert Mod 8TB
The CString::operator[] will only retreive a character.

Use the CString::SetAt() method to change a character.
Apr 5 '07 #10
mecho
5
Thanks weaknessforcats. At the moment I can't try it but I hope it'll work.
Apr 6 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

2
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 *...
8
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...
3
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...
2
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...
4
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++,...
4
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...
6
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...
9
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...
29
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...
0
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
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...
0
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...
1
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)...
0
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...
0
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....
0
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
0
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.