r/learnprogramming • • 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

14 Upvotes

32 comments sorted by

11

u/abrahamguo 1d ago

Where is your line of code that reads in the user input? I don’t see any such code.

3

u/TheEyebal 1d ago

sorry I forgot to add when writing the post I updated it

8

u/dmazzoni 1d ago

Is it possible you made a different mistake in your original code too?

You should always copy and paste into a site like Reddit when asking a coding question, never retype - that might miss the actual issue

Also: Did you mean to put this in your while loop too?

cin >> user_year;

-4

u/TheEyebal 1d ago

YES sorry. I was just inputting the issue itself because my code is pretty big

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

u/TheEyebal 1d ago

Yeah I did that but still showing the invalid error

5

u/spinwizard69 1d ago

did you put it in the right place?

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

u/ArtResponsible4457 1d ago

It reads the input just once.

1

u/zeekar 1d ago

Where are you entering the year? You aren't initialing user_year at all. It's just a random number.

0

u/TheEyebal 1d ago

sorry I forgot to add when writing the post I updated it

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_year inside the while loop parameter. I am also making sure the user inputs ints

1

u/green_meklar 22h ago

I want the cin >> user_year inside the while loop parameter.

Why?

1

u/TheEyebal 15h ago

so if the user types a character the loop needs to run

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

-2

u/D3kalog 1d ago

ask any AI and you will get much better explanation than here, trust me.

-4

u/TrueWar515 1d ago

Have you considered switching the OR to a AND?

2

u/TrueWar515 1d ago

nah that doesnt seem to be a problem

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

u/Outside_Complaint755 1d ago

I didn't specify, but a > b, as in the OPs example.

1

u/TheEyebal 1d ago

Oh I will try this