Error Checking


This is a very important part of programming. You need to make sure you have good information coming into your program! Here is a simple test that you can add to your programs. This test wil check to see that the user input a number in the range you selected (in this case, between 1 and 1000).

//if loop examples
 
#include <iostream>
using namespace std;

int main()
{
	int x;			//user will initialize with input (cin)

	// get user input	
	cout << "\nEnter a number between 1 and 1000\n";
	cin  >> x;									

	//do your error checking here!
	if (x<1 || x > 1000)
	{
		cout << "\nSelect another number.  Make sure it's between 1 and 1000!\n";
		cin  >> x;
	} //end of error checking loop

	
	//continue with good number
	if (x>500)
	{
		cout << "Your number is greater than 500!\n";
	}


	if (x < 500)
	{
		cout << "\nYour number is less than 500!\n";
	}

	if (x ==500)		//note the double equal signs!
	{
		cout << "\nYour number is exactly 500!\n\n";
	}


	return 0;
}	// end of main

//PROGRAM OUTPUT
// I want to see evidence of your error checking!
// you may need to run your program two or three times to show that it works