r/learnprogramming • u/TheEyebal • 1d ago
Debugging 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
7
u/CoachSevere5365 1d ago
You have a while loop which validates the year, but you're not asking the user for a new value inside the loop.
7
u/dmazzoni 1d ago
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:
- There's a bug elsewhere, you didn't show us all of your code
- You have a bug in your code that isn't present in the version you're showing us, because you retyped it rather than copying and pasting
- You didn't save your code or didn't recompile, so you're running an old version of it
5
u/TheSnailBear 1d ago
Put the cin inside the whole loop.
3
3
u/lfdfq 1d ago
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 1d ago
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 1d ago
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
1
u/Superb_5194 1d ago
Fix ```C++ int user_year; // input year
cout << "Enter a year: "; cin >> user_year;
while (user_year > 2099 || user_year < 1900) { cin.clear(); // Clear the error flag if non-integer was entered cin.ignore(10000, '\n'); // Discard the invalid input from the buffer cout << "Invalid Input, Enter a year: "; cin >> user_year; // Read the new input so the loop can eventually exit }
```
1
u/TheEyebal 1d ago
I want the
cin >> user_yearinside the while loop parameter. I am also making sure the user inputs ints1
1
u/green_meklar 22h ago
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_year in every loop iteration.
With that being said, this looks odd:
!(cin >> user_year)
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.
1
u/TheEyebal 15h ago
basically if I type something that is not the data type of user_year than it throws the error inside the while loop
user_year is an integer so if I type a character than it'll run the while loop
-4
u/TrueWar515 1d ago
Have you considered switching the OR to a AND?
2
2
u/TheEyebal 1d ago
Yeah I tried that already and it logically does not make since because it cannot be both less and greater than the max/min numbers
0
u/Outside_Complaint755 1d ago edited 1d ago
When you change between OR and AND, you also either 1) add NOT, or 2) Flip the direction of the comparisons
Example ```
EDIT: a > b as in OP's original example
if value > a or value < b -> if (not value > a) and (not value < b) -> if value < a and value > b ```
2
u/MarsupialMisanthrope 1d ago
Those aren’t equivalent. Assuming a < b the first always returns true, and the second always returns false (there are no values less than a and greater than b).
1
1
11
u/abrahamguo 1d ago
Where is your line of code that reads in the user input? I don’t see any such code.