Question need help with a bit of code

AlphaWe3p0n54

New member
Joined
Sep 18, 2024
Messages
2
Programming Experience
Beginner
I am a student and I am learning C# with visual studio 2022. I was tasked with creating a code featuring a do while loop. Everything i have in the code is going ok, but when i create the loop, it adds in the last number that is inputted that is used to exit out of the loop.
for instance:
Screenshot 2024-09-18 102957.png

how do i get the loop to exit out without counting the last line?
Screenshot 2024-09-18 103217.png
 
Please do not post code as screenshots. Please post code as text. Use the code tags. (It's the </> button on the toolbar).
 
Anyway, do your break out check right after you get the input. Currently in your code, you are getting the entered -1, and doing your computation, and then doing a check to see if the value entered was -1.

It's still not clear what value you are getting out of the do-while code with the current way you've structured your code. Yes, you are repeating while the value is greater or equal to zero, but you condition to break out never become true because you break out before that in the case when it will be false.
 
If you want to get rid of the break, this this pseudo code might be another approach, but now it features a duplicate check of the input. Might be a case of 6 of one, and half a dozen of the other.

C#:
do
{
    get input from user
    if (input >= 0)
    {
        add input into running total
    }
} while (input >= 0)
 

Latest posts

Back
Top Bottom