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

This is a discussion on problems with entering a char when an input is specified as a float within the C++ Programming forums, part of the General Programming Boards category; I'm trying to make a function work without the possibility of an infinite loop. The function has two inputs: an ...

  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,317
    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
    20,975
    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.
    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
    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,317
    "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, 04:42 PM
  3. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  4. Trouble with a lab
    By michael- in forum C Programming
    Replies: 18
    Last Post: 12-06-2005, 10:28 PM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 12:18 AM

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