How do I loop back if it is invalid input?

Anyone kindly teach me how to loop back to input when it is not an integer?It will very much helpful to improve my c++ skill
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
#include <iostream>
using namespace std;
int main()
{
	int x,y;
	cout<<"Please enter x coordination : ";
	cin>>x;
	cout<<"Please enter y coordination : ";
	cin>>y;
	
	if(x==0 && y>0)
	cout<<"Coordinate of ("<<x<<", "<<y<<")is along y-axis";
	else if(x>0 && y==0)
	cout<<"Coordinate of ("<<x<<", "<<y<<")is along x-axis";
	else if(x>0 && y>0)
	cout<<"Coordinate of ("<<x<<", "<<y<<") is in the 1st quadrant";
	else if(x<0 && y>0)
	cout<<"Coordinate of ("<<x<<", "<<y<<")is in the 2nd quadrant";
	else if(x<0 && y<0)
	cout<<"Coordinate of ("<<x<<", "<<y<<") is in the 3rd quadrant";
	else if(x>0 && y<0)
	cout<<"Coordinate of ("<<x<<", "<<y<<")is in the 4th quadrant";
	
	return 0;	
}
closed account (48T7M4Gy)
There are many permutations on the same theme, this is one:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using namespace std;

int main ()
{
    int x;
    
    char prompt[] = "Please enter an integer: ";
    
    cout << prompt;
    cin >> x;
    
    while ( !cin.good() )
    {
        cout << "Error!" << endl;
        cin.clear();
        cin.ignore(1000, '\n');
        cout << prompt;
        cin >> x;
    }
    
    return 0;
}
Topic archived. No new replies allowed.