473,398 Members | 2,212 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,398 software developers and data experts.

How to change the following string into the floating point value?(urgent)

Hi friend,
I am in deep trouble,
I need to change the string in following format " 1.55576+2" in the
floating point notation.
that is i should have 155.576 as my output.
Please suggest some efficient way.
Looking forward to get good solution to my problem.
Raj

Mar 2 '06 #1
6 1903

mahesh wrote:
Hi friend,
I am in deep trouble,
I need to change the string in following format " 1.55576+2" in the
floating point notation.
that is i should have 155.576 as my output.
Please suggest some efficient way.
Looking forward to get good solution to my problem.
Raj


Did you try solving the problem ?

Divide the string into two parts. Lets say str1 is string before the
sign. ch1 is the charcter after the sign. Remember to have a null
character at end of str1.

Do a switch or use if-else on ch1 and convert the str1 to int using
atoi and multiply by the sign.

This is not the most efficient solution though. Just to get you
started.

You could also use std::string for more efficient means.

Mar 2 '06 #2
mahesh wrote:
Hi friend,
I am in deep trouble,
I need to change the string in following format " 1.55576+2" in the
floating point notation.
that is i should have 155.576 as my output.
Please suggest some efficient way.
Looking forward to get good solution to my problem.
Raj


std::cout << 155.576 << std::endl;

Alternatively, use
cout.setf(ios::fixed,ios::floatfield);

Mar 2 '06 #3
Hi,
Thanks for the reply.
But I am confused as you gave me vague idea.
As I was out of the programming for long time and I am recalling my C++
expertise. But I know that atoi function doesnot work under certain
condition.
Could you please more specific?
I think the solution you provided was not optimal.T
Thanks
Raj

Mar 2 '06 #4
mahesh wrote:
Hi friend,
I am in deep trouble,
I need to change the string in following format " 1.55576+2" in the
floating point notation.
that is i should have 155.576 as my output.
Please suggest some efficient way.
Looking forward to get good solution to my problem.
Raj


use stringstream

ben
Mar 2 '06 #5
Hi thanks for the help,
Here is another problem:
I used following code to read a string in 1.00016+4 to 10000016 format:

////MY CODE////
int main ()
{

std::string temp1 = "1.00016+4";

int len1 = temp1.length();
cout<<"The length is:\n"<<len1<<endl;
int pos = temp1.find('+',0);
cout<<"the position of + is :\n"<<pos<<endl;
// int pos3 = temp1.find('-',0);
// if temp1[pos] =
string s1 = temp1.substr(pos+1,len1);
cout<<"The position after the decimal s1 is:\n"<<s1<<endl;
string s2 = temp1.substr(0,pos);
cout<<"The position before the decimal s2 is:\n"<<s2<<endl;
double j;
float i;
i = atoi(s2.c_str());
cout<<"The changed s2\n"<<i<<endl;
char* pc;

cout<<"The changed s2\n"<<i<<endl;
j = strtod(s1,&pc,10);
//char* pc1;
cout<<"The changed s1\n"<<j<<endl;
//cout<<i;
}

I am having problem with strtod function.
its gives me following error:
38 C:\Documents and Settings\Mahesh Neupane\My Documents\temp1.cpp
cannot convert `std::string' to `const char*' for argument `1' to
`double strtod(const char*, char**)'
Can anyone help me to debug this program and convert the file
correctly?
Raj

Mar 2 '06 #6

mahesh wrote:
Hi thanks for the help,
Here is another problem:
I used following code to read a string in 1.00016+4 to 10000016 format:

////MY CODE////
int main ()
{

std::string temp1 = "1.00016+4";

int len1 = temp1.length();
cout<<"The length is:\n"<<len1<<endl;
int pos = temp1.find('+',0);
cout<<"the position of + is :\n"<<pos<<endl;
// int pos3 = temp1.find('-',0);
// if temp1[pos] =
string s1 = temp1.substr(pos+1,len1);
cout<<"The position after the decimal s1 is:\n"<<s1<<endl;
string s2 = temp1.substr(0,pos);
cout<<"The position before the decimal s2 is:\n"<<s2<<endl;
double j;
float i;
i = atoi(s2.c_str());
If you know what the line above does (and presumably you do if you
wrote it) ...
cout<<"The changed s2\n"<<i<<endl;
char* pc;

cout<<"The changed s2\n"<<i<<endl;
j = strtod(s1,&pc,10);
.... then you know how to fix the problem you are having on this line.
//char* pc1;
cout<<"The changed s1\n"<<j<<endl;
//cout<<i;
}

I am having problem with strtod function.
its gives me following error:
38 C:\Documents and Settings\Mahesh Neupane\My Documents\temp1.cpp
cannot convert `std::string' to `const char*' for argument `1' to
`double strtod(const char*, char**)'
Can anyone help me to debug this program and convert the file
correctly?


Read these
http://www.parashift.com/c++-faq-lit....html#faq-39.1
http://www.parashift.com/c++-faq-lit....html#faq-39.2

Gavin Deane

Mar 3 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: ken | last post by:
I've been looking for a solution to a string to long conversion problem that I've run into >>> x = 'e10ea210' >>> print x e10ea210 >>> y=long(x) Traceback (most recent call last): File...
4
by: cindy liu | last post by:
Hi, In .Net, how to convert a string to a double? Thanks in advance! Cindy
5
by: Bryan R. Meyer | last post by:
I am a relatively new C++ programmer and am attempting to write a function that takes a number of type float and adds commas to it in the appropriate places. In order to manipulate the number to...
21
by: Allin Cottrell | last post by:
OK, I realize that what I am asking here is not likely to have a answer within the C standard. Nonetheless, it is not specific to any particular platform, so I'll hazard the question anyway. A...
13
by: tings | last post by:
An article states: "In floating point maths, where if you divide by a sufficiently large number sufficiently often, you will always be able to reach a value too small to distinguish from zero,...
21
by: oksuresh | last post by:
Hi talents, I have noticed that atof() function approximates the string I pass to it. when I use atof() , as atof(" 184.64") and it returns 184.63999999999 But I would like to have...
19
by: VK | last post by:
http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/ b495b4898808fde0> is more than one month old - this may pose problem for posting over some news servers. This is why I'm...
28
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
6
by: Cezary Noweta | last post by:
Hello, In Control Panel, Regional Settings, set decimal point to something other then dot ,,.'' (ASCII 0x2E). Let it be comma (ASCII 0x2C) for example. Compile and run the following code ===...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.