The problem is to convert a string into a float. What happens is a user inputs a string, but the string only contains numbers. How do I get the program to recognize the numbers as numbers?
The problem is to convert a string into a float. What happens is a user inputs a string, but the string only contains numbers. How do I get the program to recognize the numbers as numbers?
Remember, if at first you don't succeed, destroy all evidence that you tried in the first place.
There is a FAQ entry on this that includes different options. I generally prefer stringstreams.
Here is one example: http://www.parashift.com/c++-faq-lit....html#faq-39.2
FAQ entries on converting ints to strings:
C: http://faq.cprogramming.com/cgi-bin/...&id=1043284385.
C++: http://faq.cprogramming.com/cgi-bin/...&id=1043284385.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
what are stringstreams?
Remember, if at first you don't succeed, destroy all evidence that you tried in the first place.
He wants to convert a string to a float, not int to string.
Try atof
*edit*To recognise the numbers as numbers you can just use a selction statement. if(c == '1' || c == '2' || etc)
An object dedicated to processing strings through the use of a stream (ala cout/cin). Think about it, when you say:Originally Posted by Victor4015
If you enter 13.49 at the prompt, the program accepts all that data as a string of characters and then converts it into a double and stores the result in the variable d. The program has to take a string from the cin stream and convert. The problem you appear to have is that you have an existing string in your program (not something that is going to be comming through an input stream) and need to process it as if you sent it through the cin stream and could store the result in a variable. A stringstream will allow you to do this. You initialize the stringstream with the string you wish to process and then simply extract the value from the stream into a variable by using >> just like you would if you were processing user input directly.Code:double d; cout << "Enter in a floating-point value: "; cin >> d;
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
They let you treat a string similar to as if it were a file with data in it. You can then use the operator<< and operator>> to read or write to the string. Here is an example:Code:what are stringstreams?
Code:#include<iostream> #include<sstream> using namespace std; int main() { string str = "15.5"; istringstream inFile(str); double num = 0; inFile>>num; cout<<2 * num<<endl; return 0; }