strtod(*iter) + double

G

Gary Wessle

Hi

I have a
vector<string> which holds numbers, I need to loop and printout those
numbers + a value as doubles .

typedef vector<string>::const_iterator vs_itr;
for(vs_itr i=vect.begin(); i!=vect.end(); ++i){
cout << *i << '\t' << strtod(*i)+val << '\n';

isn't de-referencing the iterator puts out its string value?

I am getting

gen_data.cpp:62: error: cannot convert 'const std::basic_string<char,
std::char_traits<char>, std::allocator<char> >' to 'const char*' for
argument '1' to 'double strtod(const char*, char**)'

thanks
 
I

Ian Collins

Gary said:
Hi

I have a
vector<string> which holds numbers, I need to loop and printout those
numbers + a value as doubles .

typedef vector<string>::const_iterator vs_itr;
for(vs_itr i=vect.begin(); i!=vect.end(); ++i){
cout << *i << '\t' << strtod(*i)+val << '\n';

isn't de-referencing the iterator puts out its string value?

I am getting

gen_data.cpp:62: error: cannot convert 'const std::basic_string<char,
std::char_traits<char>, std::allocator<char> >' to 'const char*' for
argument '1' to 'double strtod(const char*, char**)'
The error is telling exactly what's wrong, strtod takes a const char*,
not a std::string. Look up string.c_str()
 
G

Gary Wessle

Ian Collins said:
The error is telling exactly what's wrong, strtod takes a const char*,
not a std::string. Look up string.c_str()

I actually was trying to fix it using

out<< setw(10) << *i << strtod(*i.c_str(),0)+val << '\n';
for no avail, here the *i puts out a string, c_str() converts it to a
char*, why is it complaining still?
saying vs_itr' has no member named 'c_str', then what am going to
apply c_str() upon? should I cast *i into a string?
 
R

red floyd

Gary said:
I actually was trying to fix it using

out<< setw(10) << *i << strtod(*i.c_str(),0)+val << '\n';
for no avail, here the *i puts out a string, c_str() converts it to a
char*, why is it complaining still?
saying vs_itr' has no member named 'c_str', then what am going to
apply c_str() upon? should I cast *i into a string?

Try strtod( (*i).c_str(), 0).

the . operator binds tighter that '*', so you had the equivalent of
*(i.c_str()), which won't work.
 
T

Thomas J. Gritzan

Gary said:
out<< setw(10) << *i << strtod(*i.c_str(),0)+val << '\n';
for no avail, here the *i puts out a string, c_str() converts it to a
char*, why is it complaining still?
saying vs_itr' has no member named 'c_str', then what am going to
apply c_str() upon? should I cast *i into a string?

Look up the operator precedence table:

"." has higher precedence than prefix-*, so *i.c_str() is parsed as
*(i.c_str()), but you want (*i).c_str() or simply i->c_str()

Instead of strtod, you could use the method that is in the FAQ:
http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
 

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,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top