Solved

string to double

Posted on 2003-12-11
5
809 Views
Last Modified: 2006-11-17
i get a string from a line I'm parsing.

string notdegree= buf[2];    returns a number but its a string

string degree=  notdegree.substr(0,5); returns the part of the number i want still a string

         indegree = degree/60  -- i need to be able to do this  -- how do I change something from a string to a number

the following is the error i get

C:\Program Files\Microsoft Visual Studio\MyProjects\parse\parse.cpp(98) : error C2676: binary '/' : 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' does not define this operator or a conversion to a type acc
eptable to the predefined operator
0
Comment
Question by:tpiazza
  • 2
  • 2
5 Comments
 
LVL 19

Accepted Solution

by:
Dexstar earned 50 total points
ID: 9922388
@tpiazza:

> indegree = degree/60  

Try this:

     indegree = atof(degree.c_str())/60.0f;

Hope That Helps,
Dex*
0
 

Author Comment

by:tpiazza
ID: 9922527
tried that it errors out

C:\Program Files\Microsoft Visual Studio\MyProjects\parse\parse.cpp(100) : error C2664: 'atof' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *'
        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Error executing cl.exe.
0
 

Author Comment

by:tpiazza
ID: 9922539
take that back -- it works now

indegree = atof(degree.c_str())/60.0;

.c_str() -- I dint have that -- what does it do?

0
 
LVL 9

Expert Comment

by:tinchos
ID: 9922578
c_str method

Returns a pointer to a null-terminated array of characters representing the string's contents.

Tincho
0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9922621
It returns a const char* to the data in the string...  :)

Dex*
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question