Checking Floats

This is a discussion on Checking Floats within the C++ Programming forums, part of the General Programming Boards category; How would I go about checking to see if a cin >> input is a number/float? I'm making a really ...

  1. #1
    Registered User ScottyBoy's Avatar
    Join Date
    Apr 2011
    Posts
    4

    Checking Floats

    How would I go about checking to see if a cin >> input is a number/float? I'm making a really basic calculator, which works fine, unless you put in letters or symbols. Then you get some really funky results. LOL

    Here's what I have so far:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main ()
    {
    	string action;
    	float f_n;
    	float s_n;
    	
    	cout << "\n\n What do you want to do? (Possible answers: multiply, add, subtract, divide) \n";
    	cin >> action;
    	
    	if (action != "add" && action != "subtract" && action != "multiply" && action != "divide")
    	{
    		cout << "Unknown action command.";
    		return 1;
    	}
    	
    	cout << "\n\n First Number \n";
    	cin >> f_n;
    	
    	// Somehow check to see if "f_n" is an int/float....	
    
    	cout << "\n\n Second Number \n";
    	cin >> s_n;
    	
    	// Somehow check to see if "s_n" is an int/float....
    	
    	if (action == "add")
    	{
    		cout << "Your answer is >> " << f_n + s_n << " \n\n";
    	}
    	else if (action == "subtract")
    	{
    		cout << "Your answer is >> " << f_n - s_n << " \n\n";
    	}
    	else if (action == "multiply")
    	{
    		cout << "Your answer is >> " << f_n * s_n << " \n\n";
    	}
    	else if (action == "divide")
    	{
    		cout << "\n\n Your answer is >> " << f_n / s_n << " \n\n";
    	}
    	else
    	{
    		cout << "\n\n unknown operation" << " \n\n";
    		return 1;
    	}
    	return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    Check the result of cin >> f_n.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User ScottyBoy's Avatar
    Join Date
    Apr 2011
    Posts
    4
    And how do I do that? That's kinda what I was asking. Thanks for your help, BTW.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    It could be along the lines of:
    Code:
    if (!(cin >> f_n))
    {
        cout << "Invalid input" << endl;
        // exit program here?
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,245
    If you check documentation for stream objects, you will find they support an operator!() which gives the error state. If an istream is in an error state after attempting to read a float, a float has not been successfully read in. Note that any error state will need to be cleared before attempting to read anything else.
    Right 98% of the time, and don't care about the other 3%.

  6. #6
    Registered User ScottyBoy's Avatar
    Join Date
    Apr 2011
    Posts
    4
    Okay, thanks, peeps. I'll check the documentation, and see where I get with that. If its anything like the PHP manual, I'm all set with reading for the next six months. XD

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rounding off floats
    By creeping death in forum C Programming
    Replies: 6
    Last Post: 04-01-2009, 01:49 PM
  2. % and floats
    By confuted in forum C++ Programming
    Replies: 2
    Last Post: 08-04-2003, 09:41 PM
  3. floats
    By phptech in forum C Programming
    Replies: 6
    Last Post: 07-07-2003, 08:42 PM
  4. Need Help With Floats.....
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 02-15-2002, 04:31 AM
  5. Floats
    By JFK in forum C Programming
    Replies: 2
    Last Post: 11-27-2001, 06:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21