Did you even look at the link?
In this basic example, you declare an age, as an integer. Your program will keep asking the user his age until a number is provided AND that number is actually smart.Code:int age = 0;
while ((std::cout << "How old are you? ")
&& (!(std::cin >> age) || age < 1 || age > 200))
{
std::cout << "That's not a number between 1 and 200; ";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
std::cout << "You have entered a valid age of " << age << ";" << std::endl;
Do the same thing for revenue. And all your other variables.
I know you want to "code yourself", but that shouldn't keep you from doing things the right way. The above code is pretty self forward, and easy to understand. I'd just replace "std::numeric_limits<std::streamsize>::max()" with 255.
for your Yes/No:
Once done, you have a simple boolean variable that represents yes or no.Code:char answer;
while ((std::cout << "Y/N? ")
&& (!(std::cin >> answer) || !(answer=='Y' || answer=='y' ||answer=='N' ||answer=='n') ))
{
std::cout << "Please provide Y or N; ";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
bool isYes = (answer=='y' || answer=='Y');
Where I work, 50% of our activity consists in parsing, validating and using user provided data. Not with simple user interface of course, they are actually more complex binary files, but the way it is done is ALWAYS in the same 3 steps, which you should follow too:
1-Read user input into an adequate container: int for numbers, strings for text.
2-Validate the numeric validity of the number: for example, an age cannot be negative.
3-Repeat until all data is valid.
If you mix these steps, your code will become un-maintainable.
If you do these steps, then step 2 will guarantee your "birthyear" is a number between 1 and 200, and you will not have to worry every step of your code if it is "5 digits long" or "apple". You have already done step 2 and step 3, so you know it is a normal birthyear.

