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

1. ## 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. 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. 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. 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.

5. 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. "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.