3

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;
}

drkld.mnd
  • 33
  • 5
  • 3
    Pro tip: Don't put multiple statements on one line. It doesn't make your code smaller / faster. – Waqar Aug 20 '20 at 17:00
  • 1
    @Waqar It do make their *source* code smaller, but don't make their *binary* code smaller. – MikeCAT Aug 20 '20 at 17:01
  • 1
    Where's the user input on terminal? Did you just keep tapping enter? Type 2019 on the terminal and press enter. It must stop. – Red.Wave Aug 20 '20 at 17:18
  • 2
    It's not really possible to 'figure out' C++, it's far too complicated for that (maybe if you're a programming genius). The only way to learn by yourself is to read a good [book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Even good quality videos and web sites can't really do more than scratch the surface. – john Aug 20 '20 at 17:21
  • 2
    Readable code is often more valuable than fast code. Computers are always getting faster. People, on the other hand... Well, if they aren't getting stupider, it sure looks like it sometimes. Anyway the co-op who winds up maintaining your code a few years from now is a lot more likely to completely things up if you confuse them with cramped code. – user4581301 Aug 20 '20 at 17:22

1 Answers1

3

istream::operator>> will set failbit when what is inputted is invalid. failbit will prevent it from accepting new input.

Typically integers larger than 10 digits is considered as invalid because int (32-bit long) can store numbers only upto 2,147,483,647.

To overcome this, you can use basic_ios::clear to reset the failbit.

while (age <= 0) {cout << "Please input a valid year \n"; cin >> year; age = 2020 - year;}

Should be

while (age <= 0) {
    cout << "Please input a valid year \n";
    cin.clear(); // add this to clear failbit
    cin >> year;
    age = 2020 - year;
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70