string differences; max irritation

J

John Brawley

Please what is the functional C++ difference between a string of characters
typed manually and included inside the main() function of a program...

vis:
string str = "123,4.56,7.8"; ,

....and _exactly the same string_, read into a string variable from a file
via getline()
vis:
getline(fileobject, str, ','); ?
(I'm parsing this line with three getline()s; output via cout is "123" then
"4.56" then "7.8")

I can't get getline()'s string to work with strtod() in the same program,
but the manually typed string works perfectly with strtod() in a program all
by itself, and the getline()-read-and-parse works perfectly in a program all
by itself.
This is making no sense to me.

The file contains double-precision numbers (as characters), the three you
see above (they vary, file to file), and many other lines (\n=terminated) of
three doubles each.
The manually typed and-inside-the-main() string is also numbers (as
characters).

So: _what's different_ ??
I'd think a string of characters is a string of characters, regardless of
where it comes from, the only difference I've been able to find being that
between C++ strings (which these have to be), and C strings (which have a \0
null character at the end).
No null characters.
WHY can't I read these characters in from the file, then get my decimal
floats back with strtod() ? I can do either _independently_, just not
together.

I'm stumped, and I _have_ done my 'homework' --days of it--
searching/reading on the web, trying various code snippets found (which is
why strtod() works with the manually written numbers), asking e-friends,
etc.
This is nuts.
It should be _easy_ and simple, to get a number from a file and have it BE a
number when I need it in the program. (It was easy in Python; I did all
this before. It's crazy in C++.)

Thanks
(*whimper*)
 
A

Alf P. Steinbach

* John Brawley:
Please what is the functional C++ difference between a string of characters
typed manually and included inside the main() function of a program...

vis:
string str = "123,4.56,7.8"; ,

...and _exactly the same string_, read into a string variable from a file
via getline()
vis:
getline(fileobject, str, ','); ?
(I'm parsing this line with three getline()s; output via cout is "123" then
"4.56" then "7.8")

I can't get getline()'s string to work with strtod() in the same program,
but the manually typed string works perfectly with strtod() in a program all
by itself, and the getline()-read-and-parse works perfectly in a program all
by itself.
This is making no sense to me.

The file contains double-precision numbers (as characters), the three you
see above (they vary, file to file), and many other lines (\n=terminated) of
three doubles each.
The manually typed and-inside-the-main() string is also numbers (as
characters).

So: _what's different_ ??
I'd think a string of characters is a string of characters, regardless of
where it comes from, the only difference I've been able to find being that
between C++ strings (which these have to be), and C strings (which have a \0
null character at the end).
No null characters.
WHY can't I read these characters in from the file, then get my decimal
floats back with strtod() ? I can do either _independently_, just not
together.

I'm stumped, and I _have_ done my 'homework' --days of it--
searching/reading on the web, trying various code snippets found (which is
why strtod() works with the manually written numbers), asking e-friends,
etc.
This is nuts.
It should be _easy_ and simple, to get a number from a file and have it BE a
number when I need it in the program. (It was easy in Python; I did all
this before. It's crazy in C++.)

Thanks
(*whimper*)

See the FAQ item titled "How do I post a question about code that
doesn't work correctly?", which can be found at e.g. <url:
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8>.

Then try re-posting your question, following the guidelines in the FAQ.

This will hopefully make it possible to answer the question.


Cheers, & hth.,

- Alf
 
K

kasthurirangan.balaji

* John Brawley:













See the FAQ item titled "How do I post a question about code that
doesn't work correctly?", which can be found at e.g. <url:http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8>.

Then try re-posting your question, following the guidelines in the FAQ.

This will hopefully make it possible to answer the question.

Cheers, & hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?- Hide quoted text -

- Show quoted text -

Pls find the sample program, from what i understand your problem would
be.

#include <fstream>
#include <iostream>
#include <string>
#include <cstdlib>

main()
{
std::ifstream ifstr("new");
std::string txt("");

while( getline(ifstr,txt) )
{
char *tmp;
const double dd(strtod(txt.c_str(),&tmp));
std::cout << dd << '\n';
}
}

output is 16.16

contents of file 'new' is
16.16

Thanks,
Balaji.
 
J

John Brawley

To Alf:
My apologies.
I read the FAQ B4 the post.
I'm trying to comply, by doing as much as I can externally, and by asking
for hints, leads, pointings-in-the-right-direction B4 posting loads of
working / nonworking code.

To Kasthurirangan Balaji:
Your example code is alien to me (newbie; Python handyman/toolbox graduate),
but I will try to grasp it and study it and use it to try to figure out why
mine isn't working.
And, _thank_you_!

If this doesn't help, I'll try again, this time with the runnable (and non-)
code inline.
 
A

Alf P. Steinbach

* John Brawley:
To Alf:
My apologies.
I read the FAQ B4 the post.
I'm trying to comply, by doing as much as I can externally, and by asking
for hints, leads, pointings-in-the-right-direction B4 posting loads of
working / nonworking code.

Do /simplest possible/ demonstration program.

Start with

int main()
{
}

Add the smallest amount of code that demonstrates the problem.

Post complete program, actual results, and what you expected instead.


Cheers, & hth.,

- Alf
 
J

John Brawley

Update (minutes later):
To Balaji:
THANK YOU.
It compiles, runs, and I understand it now.
Your grasp of my problem was perfect.
Your code was perfect: clean, brief, functional, elegant, _exactly_ what I
needed to see.
I am in your debt.

(My first post may have been improper in some way (maybe this one is also),
but Balaji, you got the essence of the problem from it.
Sorry, Alf.)
 
A

Alf P. Steinbach

* John Brawley:
Update (minutes later):
To Balaji:
THANK YOU.
It compiles, runs, and I understand it now.
Your grasp of my problem was perfect.
Your code was perfect: clean, brief, functional, elegant, _exactly_ what I
needed to see.
I am in your debt.

(My first post may have been improper in some way (maybe this one is also),
but Balaji, you got the essence of the problem from it.
Sorry, Alf.)


T'is all right. But consider that advice about posting actual code, as
per FAQ 5.8, for next question. It generally works, whereas guessing
about the problem generally doesn't work (although it did this time :)).

Cheers,

- Alf
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top