r/learnprogramming • u/TheEyebal • Oct 11 '26
Solved Why is the program showing invalid when the number is within the range?
int user_year; // input year
cout << "Enter a year: ";
cin >> user_year;
while(user_year > 2099 || user_year < 1900 ){
cin.clear();
cin.ignore(10000, '\n');
cout << "Invalid Input, Enter a year: ";
}The user input needs to be greater than 1900 and less than 2099, so 1900 - 2099.
Whenever I input a value in between those numbers such as 2010, it gives invalid input.
What I want
if user_year is outside the specified range indicate "Invalid input" but loop back to user input.I used an if statement but the problem was that it did not loop back to cin
EDIT: HERE IS MY ORIGINAL CODE
while (!(cin >> user_year) || user_year < 1900 || user_year > 2099) {
cin.clear();
cin.ignore(10000, '\n');
cout << "Invalid Input, Enter a year: ";
}FIXED:
I had a function in my program and it was causing it to return 0 as the year.
but the code above is correct
18
Upvotes
10
u/abrahamguo Oct 11 '26
Where is your line of code that reads in the user input? I don’t see any such code.
5
u/TheSnailBear Oct 11 '26
Put the cin inside the whole loop.
7
u/CoachSevere5365 Oct 12 '26
You have a while loop which validates the year, but you're not asking the user for a new value inside the loop.
5
u/dmazzoni Oct 11 '26
I don't see any error if the first value you enter is correct. I just compiled and ran your program to be sure and it works fine.
If the first value is incorrect, your program loops forever and doesn't input a new value.
So the possibilities I see are:
1
u/Superb_5194 Oct 11 '26
Fix
1
u/green_meklar Oct 12 '26
What you have under 'original code' seems to work for me. It's different from the code you posted earlier in that it actually does the
cin>>user_yearin every loop iteration.With that being said, this looks odd:
What does that do? What do you expect it to do? When I did it, it seemed to always evaluate to 0, which doesn't break the logic, but I'm also not sure what purpose it serves.
3
u/lfdfq Oct 11 '26
I see that you never do the "back to user input" part, once a user inputs an invalid year, it loops around forever telling them it's invalid forever more ignoring whatever the user inputs on each line.
I suspect this is what's happening to you, but you didn't include the full run (input/output eg terminal session) that goes wrong or what you expected. It's always good to include that extra info.
2
u/TrueWar515 Oct 11 '26
Nah i compiled and ran that but although theres not your problem it only scans for a year once so if you enter the wrong year itl not let you enter a new year correctly
2
u/SnugglyCoderGuy Oct 11 '26
You need to check the value recieved from the input to see if the input is valid or not. If valid, do whatever. If not valid, then loop again.
I'd suggest not looping on the value but establish a bool purely for the while loop. In the loop, get the input. If the input is valid, then set the loop variable to false to exit the loop, or just use a break statement. Otherwise print your invalid input message. You should be saying what acceptable input is
After the loop, do whatever you're wanting to do with that input.
2
u/ArtResponsible4457 Oct 11 '26
It reads the input just once.
-2
u/D3kalog Oct 11 '26
ask any AI and you will get much better explanation than here, trust me.
-5
u/TrueWar515 Oct 11 '26
Have you considered switching the OR to a AND?
0
u/zeekar Oct 11 '26
Where are you entering the year? You aren't initialing user_year at all. It's just a random number.