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

M

mahesh

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
 
J

Jaspreet

mahesh said:
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.
 
N

Neelesh Bodas

mahesh said:
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);
 
M

mahesh

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
 
B

benben

mahesh said:
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
 
M

mahesh

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
 
G

Gavin Deane

mahesh said:
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-lite/misc-technical-issues.html#faq-39.1
http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2

Gavin Deane
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top