#include <iostream>
int main()
{
constint minv = 0 ;
constint maxv = 360 ;
int number ;
while(true)
{
std::cout << "please enter a number in [" << minv << ',' << maxv << "]: " ;
// if the user has entered a valid number, there is nothing more to be done
if( std::cin >> number && number >= minv && number <= maxv ) break ;
// else
std::cin.clear() ; // clear the error state
// http://www.cplusplus.com/reference/ios/ios/clear/
std::cin.ignore( 1000, '\n' ) ; // discard invalid input
// http://www.cplusplus.com/reference/istream/istream/ignore/
// and try again
}
std::cout << "you entered " << number << '\n' ;
}
#include <iostream>
usingnamespace std;
int main()
{
int number;
cout<<"Please enter a number (0 - 360): " <<endl;
cin>>number;
if ( number > 360 )
{
do
{
cout<<"Please enter a number between 0 - 360 (no higher): " <<endl;
cin>>number;
}while ( number > 360 ); //keeps looping as long as the number is higher than 360
}
elseif ( number < 0 )
{
do
{
cout<<"Please etner a number between 0 - 360 (no lower): " <<endl;
cin>>number;
}while ( number < 0 );//keeps looping as long as the number is Lower than 0
}
return 0;
}
or like this if you happen to be a while loop fan:
#include <iostream>
usingnamespace std;
int main()
{
int number;
cout<<"Please enter a number (0 - 360): " <<endl;
cin>>number;
while ( number > 360 )
{
cout<<"Please enter a number between 0 - 360 (no higer): " <<endl;
cin>>number;
}
while ( number < 0 )
{
cout<<"Please enter a number between 0 - 360 (no lower): " <<endl;
cin>>number;
}
return 0;
}
The two previous answers will allow you to enter a number outside 0-360 (enter -1 first then 361).... try this:
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
usingnamespace std;
int main()
{
int number;
do {
cout << "Please enter a number (0 - 360): " << endl;
cin >> number;
} while (number < 0 || number > 360);
return 0;
}