Endless loop?

Why if i input a negative number, it all goes fine, but if i input a letter or an invalid input (ex .8) it goes on a endless loop? Help me out please.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  #include <iostream>
using namespace std;
int main()
{
int x;
	do
	{
	cout<<"Input cathetuses' length'"<<endl;
	cin>>x;
		if	(x<=0 || cin.fail())
		{
	 	cout<<"Invalid input"<<endl;
		cin.clear();
		}
		else break;	
	}
	while(true);
	return 0;
	}
First of all, you should declare a bool variable as true.

Second, after your else statement, you should set that variable to false.

That should stop the loop.
Yea take your code and run through it on a piece of paper pretend you're the computer! :) it will explain everything.

Basically
 
if(x<=0 || cin.fail()) == true 


then

 
while(true)


will always be true and youll just see

Invalid Input
Invalid Input
...
Last edited on
Thanks it works fine now. Hower, it doesn't ask the input again after an invalid input (as it did before). Any solutions?
Basically, you never asked it to ask for more input after your invalid input. It simply just breaks off after that. Assuming your code looks something like this

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

#include <iostream>
using namespace std;
int main()
{
int x;
bool goodInput = true;

do
{
    cout<<"Input cathetuses' length'"<<endl;
	cin>>x;

    if(x<=0 || cin.fail()){
        cout<<"Invalid input"<<endl;
        cin.clear();
        goodInput = false;
    }else{
        goodInput = true;
    }
}
while(goodInput);
    return 0;
}
Last edited on
I still don't get it why it's a loop. When the cycle starts again, it should ask for the input again (cin>>x;), but it actually doesn't. Why?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;
int main()
{
int x;
	do
	{
	cout<<"Input cathetuses' length'"<<endl;
	cin>>x;
		if	(x<=0 || cin.fail())
		{
	 	cout<<"Invalid input"<<endl;
		cin.clear();
		}
		else break;	
	}
	while(true);
	return 0;
	}
Search do while loops and youll know why, do is only being ran once basically
But as i said before with negative numbers it works perfectly... I'm kinda confused.
So if you set the check to true, and say while (check), that will keep the loop going while the input is an int. However, it does not work if the input is a char. So I think something's wrong with the cin.fail and cin.clear.
Yeah i tried fixing it with cin.ignore() (which kinda fixes the problem), but other glitches came out.
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

put this right after your cin.clear it should solve the problem

Here's the website that I found this on, this will probably clear things up for you

https://stackoverflow.com/questions/5864540/infinite-loop-with-cin-when-typing-string-while-a-number-is-expected
Last edited on
Just a lot of compile errors with:
 
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); 
Last edited on
Ok i included the library "limits". Everything works fine now. Thanks for the help.
Topic archived. No new replies allowed.