i am just a beginner and i am trying to learn C++ as my first language. It has been just 2-3 days since i have started learning it so i am truly a novice in this field and that is the reason why this maybe sound a little bit stupid question to you. So, I am still learning the commands in C++ and while trying to learn more on the while loop I created this code just to practise.
It works fine on what I intended it to do but the problem arised when i tried to forcefully error and to check on how the while loop will work. At first, After I entered a year that will make the while loop give an error and give "Please input a valid year" statement, It worked fine when you enter a valid year on the first try but if you put a wrong year at first and then put a right year it would still show to put a valid year. Anyways I fixed that problem with some tweaks to the code.
The problem is that when I input a no. that is 10 digit or higher, the program goes on a rampage and goes on with the "Please input a valid year" statement. It works fine with a no. that is lesser than that. (See the attached image.)
I know this is not that big of a problem but as I told I am just a beginner and I really would love to know on what I am doing wrong. I used the Eclipse IDE and tried compiling manually in the Windows CMD with the MinGW compiler. Result was the same.
It would be really helpful to know what I am doing wrong because if I cant even figure out the basics then how am I gonna learn the more complicated stuff. The image link and code is given below.
#include <iostream>
using namespace std;
int main()
{
cout << "Enter your birth year \n";
int age, year;
cin >> year;
age = 2020 - year;
while (age <= 0) {cout << "Please input a valid year \n"; cin >> year; age = 2020 - year;}
cout << "So you are " << age << " years old \n";
if (13 < age && age < 18 ) { cout << "That classifies you as a teenager. \n"; }
else { if ( age < 13 ) { cout << "You are still a child \n";}}
if (age >= 18) { cout << "That classifies you as an adult. \n";
if (age > 60)
{cout << "And also thats quite a number you have reached. \nIt's actually higher than the average human life expectancy \nCONGRATS \n";}
}
return 0;
}