Advice on basic number validation
I have been experimenting with basic number validation and i need a little advice...
Code:
#include <iostream>
using namespace std;
int main() {
int num;
start:
cout << "Please enter a Number between 1 and 10... ";
cin >> num;
if (num < 1 || num > 10) {
cout << "Invalid Number" << endl;
goto start;
}
else {
cout << "Valid Number";
}
cin.ignore();
cin.get();
}
Is this the right approach for validating a number? How would i validate that it's a number and not any other character? I noticed that if i enter a letter instead at run time the console closes presumably crashing the program.
And finally, have i looped correctly? If an invalid number is entered it needs to ask for a number to be inputted again, I seem to recall reading that it's not advisable to use goto anymore, but I'm not sure how else to do it.
I would appreciate any advice you can give me