Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

string to double

Posted on 2003-12-11
5
Medium Priority
?
839 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 200 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

876 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