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 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.
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?
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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 *.
|
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...
|
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...
|
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...
|
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:
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
| |