Thread: problems with entering a char when an input is specified as a float

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    29

    problems with entering a char when an input is specified as a float

    I'm trying to make a function work without the possibility of an infinite loop. The function has two inputs: an operator and one operand and returns a sum. The inputs would look like -8 or *7 for example. The problem I have is if I enter in a "--" instead of -8, it will go to an infinite loop.


    Code:
    #include <iostream>
    #include <cmath>  
    using namespace std;
    
    void repeat(char, float, float&);
    float operand1, sum ;
    int SENTINEL;
    char operator1;
    
    int main()
    {	
    	sum = 0;
    	do 
    	{
    	cout << "enter asterisk or minus sign and a number" << endl;
    	
    
    	cin >> operator1 >> operand1;
    	
    
    	if ((operator1 == '-')||(operator1 == '*'))  //if-statement #1
    		repeat(operator1, operand1, sum);
    
    	if ((operator1 == '='))
    		SENTINEL = 1;
    
    	cout << "Total = " << sum << endl;
    
    	} while (SENTINEL != 1);
    	
    	return 0;
    }
    
    void repeat(char choice, float z, float& total)
    {
    	switch (choice)
    	{ 
    		case '-': 
    			total -= z;
    		break;
    
    		case '*':  
    			total *= z;
    		break;
    
    		case '+':			//shouldn't be able to add if... if-statement #1 is working correctly
    			total += z;
    		break;
    
    	}
    
    }

    In the version below, I added another do-while statement to repeat the inputs if a positive number wasn't entered in for the operand. The outcome of this is it doesn't do the infinite loop, but just stops letting you enter in the operator and operand.


    Code:
    #include <iostream>
    #include <cmath>  
    using namespace std;
    
    void repeat(char, float, float&);
    float operand1, sum ;
    int SENTINEL;
    char operator1;
    
    int main()
    {	
    	sum = 0;
    	do 
    	{
    	cout << "enter asterisk or minus sign and a number" << endl;
    	
    
    	do{
    	cin >> operator1 >> operand1;
    	}while (!(operand1 > 0));
    
    	if ((operator1 == '-')||(operator1 == '*'))  //if-statement #1
    		repeat(operator1, operand1, sum);
    
    	if ((operator1 == '='))
    		SENTINEL = 1;
    
    	cout << "Total = " << sum << endl;
    
    	} while (SENTINEL != 1);
    	
    	return 0;
    }
    
    void repeat(char choice, float z, float& total)
    {
    	switch (choice)
    	{ 
    		case '-': 
    			total -= z;
    		break;
    
    		case '*':  
    			total *= z;
    		break;
    
    		case '+':			//shouldn't be able to add if... if-statement #1 is working correctly
    			total += z;
    		break;
    
    	}
    
    }

    With either of these versions it lets me enter in random numbers like 74 or 12 (and no operators) and the loop repeats once. What I would like to be able to do is enter in two random characters like -- or %@ and have the loop repeat just once as well to re-enter the inputs. Any ideas?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Code:
    while (!(cin >> value))
    {
      cin.clear(); // clear the fail state
      cin.ignore(1000, '\n'); // ignore bad characters (1000 is arbitrary)
      cout << "you failed, please try again: "; // re-prompt
    }
    The above loop will run until the user enters the proper value. You can integrate it into your program and it should allow you to do what you want.

    (Note that it won't catch it if somebody types 23abc, but if you want to handle that there is an easy addition to do so.)

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    29
    well, I'm not sure how to use it. Haven't seen the (cin >> value) line before. I was thinking "value" was going to be a reserved word for filtering any number, but doesn't appear so.

    I'm reading up on the clear function and seems like a.clear clears what is in a, but when I substituted the identifier operand1 it didn't work out.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Brian_Jones
    I'm reading up on the clear function and seems like a.clear clears what is in a, but when I substituted the identifier operand1 it didn't work out.
    It clears the error flags of a, if a is a std::istream like std::cin.
    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
    Oct 2009
    Posts
    29
    Gotcha, that makes sense. I don't really have the know-how to integrate it into my code though.
    I haven't used the notation of "std::istream like std::cin" because the way I was taught was to use the "using namespace std;"...

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    "value" was just a generic variable name, in your code it would probably be "operand1".

    >> I haven't used the notation of "std::istream like std::cin" because the way I was taught was to use the "using namespace std;"...

    laserlight is just saying that the clear function works on any istream type. If you don't know what that means, then maybe a simpler explanation would be that it applies to cin. When cin reads in a bad value (like when you type a letter and it expects a number) it gets into a bad state. Calling clear() clears the flag that marks cin as bad so you can continue using it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. Trouble with a lab
    By michael- in forum C Programming
    Replies: 18
    Last Post: 12-06-2005, 11:28 PM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM