Thread: String-float conversions

  1. #1
    Registered User
    Join Date
    Nov 2005

    String-float conversions

    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.

  2. #2
    Registered User
    Join Date
    Jan 2005
    There is a FAQ entry on this that includes different options. I generally prefer stringstreams.

    Here is one example:

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005

    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:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Nov 2005
    what are stringstreams?
    Remember, if at first you don't succeed, destroy all evidence that you tried in the first place.

  5. #5
    Cat Lover
    Join Date
    May 2005
    Sydney, Australia
    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)

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Northern Virginia/Washington DC Metropolitan Area
    Quote Originally Posted by Victor4015
    what are stringstreams?
    An object dedicated to processing strings through the use of a stream (ala cout/cin). Think about it, when you say:
    double d;
    cout << "Enter in a floating-point value: ";
    cin >> d;
    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.
    "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

  7. #7
    Registered User
    Join Date
    Apr 2003
    what are stringstreams?
    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:

    using namespace std;
    int main()
    	string str = "15.5";
    	istringstream inFile(str);
    	double num = 0;
    	cout<<2 * num<<endl;
    	return 0;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM