C
C#3mo ago
Mix

Help with why code is returning this way.

Hello, I'm a CS student learning C# for the first time and i'm having some confusion on why my code is returning the way it is. If I input a number and it's a valid number in the array, the program will say it's wrong multiple times before finally printing all of the right data. When I put a break on the end of the else statement, it just says "Sorry, no data for 2020." and then just ends. But if I remove the break , it says its wrong multiple times before finally printing the expected result. If anyone has any insight into why this is happening, I'd really appreciate it!
No description
15 Replies
Angius
Angius3mo ago
Well you print it in a loop Print it after the loop has completed
Mix
Mix3mo ago
so i tried to put the Console.WriteLine outside of the loop, but now it doesn't have a reference for the [i] pointer
No description
Angius
Angius3mo ago
This one can be inside of the loop Since you break out of it anyway if the condition is met
Mix
Mix3mo ago
No description
Mix
Mix3mo ago
like that? inside of the for loop, but outside of the if statement?
Angius
Angius3mo ago
No This line doesn't cause any issues It gets printed out once, as you want it It's the other one that causes issues and gets printed multiple times
Mix
Mix3mo ago
the else statement? or the yearFound = true;?
Angius
Angius3mo ago
No description
Angius
Angius3mo ago
That way you also use your yearFound variable
Mix
Mix3mo ago
i tried that already and it gave the same result
Mix
Mix3mo ago
No description
Angius
Angius3mo ago
You did not try that
Mix
Mix3mo ago
i tried it before i posted the question in here
Angius
Angius3mo ago
The code you posted now is not doing what I told you to do So, no, you have not tried what I told you to do
Mix
Mix3mo ago
oh yeah, you're right didn't realize i put it in the for loop, that's probably why it was giving me the same result the last time I tried it. But it seems to be fixed now, thanks for the help!