Thread: Checking Floats

  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
    28,413
    Check the result of cin >> f_n.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    It could be along the lines of:
    Code:
    if (!(cin >> f_n))
    {
        cout << "Invalid input" << endl;
        // exit program here?
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  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, 05:31 AM
  5. Floats
    By JFK in forum C Programming
    Replies: 2
    Last Post: 11-27-2001, 07:53 AM