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

strtod(*iter) + double

Hi

I have a
vector<stringwhich 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
Aug 7 '06 #1
4 2448
Gary Wessle wrote:
Hi

I have a
vector<stringwhich 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()

--
Ian Collins.
Aug 7 '06 #2
Ian Collins <ia******@hotmail.comwrites:
Gary Wessle wrote:
>Hi

I have a
vector<stringwhich 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()

--
Ian Collins.
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?
Aug 7 '06 #3
Gary Wessle schrieb:
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-lit....html#faq-39.2

--
Thomas
Aug 7 '06 #4
Gary Wessle wrote:
Ian Collins <ia******@hotmail.comwrites:
>Gary Wessle wrote:
>>Hi

I have a
vector<stringwhich 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()

--
Ian Collins.

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.

Aug 7 '06 #5

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

Similar topics

3
by: Leslaw Bieniasz | last post by:
Cracow, 20.09.2004 Hi, I want to replace: char *text; double val = strtod(text,NULL); by an equivalent using std::string in the place of char *.
1
by: Mathieu Malaterre | last post by:
Hello, I would like to have some advices on this problem I am having. In my code I have hardcoded a string like: const char foo = "11 0.438482 "; I was then calling strtod to transform it...
21
by: Marky C | last post by:
atof is not working. double a = atof("12.345"); a gets set to 12.000 I am working on a toshiba micro. The data map has no space allocated to it for dynamic memory. Does anyone have an...
7
by: Dawn Minnis | last post by:
hey guys i have char *argv to receive command line arguements. I can get characters out no problem I can get integers parsed out no problem But how do I get double values. eg if my program...
9
by: Adam Warner | last post by:
Hi all, Message ID <c1qo3f0tro@enews2.newsguy.com> is one of many informative articles by Chris Torek about C. The particular message discusses aliasing and concludes with this paragraph: ...
18
by: coder | last post by:
Hi experts, Is the following usage of strtod okay (p is a char pointer): value = strtod(p, &p); Is it possible that this would evoke undefined behaviour? Or should I use a temporary pointer...
8
by: Bill Cunningham | last post by:
Since I've been told that char *argv or char **argv must be the second parameter to main's command line structure I have turned to strtod( ) but can't get it to work so far. This function is...
4
by: Bob Nelson | last post by:
Fellow C pushers: Deliberately avoiding a look at Jack Klein's bulletproof code for a robust use of strtol(), I respectfully submit the following for review and vigorous critique, desiring...
22
by: Bill Reid | last post by:
I just noticed that my "improved" version of sscanf() doesn't assign floating point numbers properly if the variable assigned to is declared as a "float" rather than a "double". (This never...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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:
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.