C
C#9mo 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
Angius9mo ago
Well you print it in a loop Print it after the loop has completed
Mix
MixOP9mo 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
Angius9mo ago
This one can be inside of the loop Since you break out of it anyway if the condition is met
Mix
MixOP9mo ago
No description
Mix
MixOP9mo ago
like that? inside of the for loop, but outside of the if statement?
Angius
Angius9mo 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
MixOP9mo ago
the else statement? or the yearFound = true;?
Angius
Angius9mo ago
No description
Angius
Angius9mo ago
That way you also use your yearFound variable
Mix
MixOP9mo ago
i tried that already and it gave the same result
Mix
MixOP9mo ago
No description
Angius
Angius9mo ago
You did not try that
Mix
MixOP9mo ago
i tried it before i posted the question in here
Angius
Angius9mo 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
MixOP9mo 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!
Want results from more Discord servers?
Add your server