Why is my program showing my else statement when the response is valid?
Hello, I'm currently in a college course for C# and I'm learning loops right now. I'm doing an assignment where I'm keeping a running sum of a value and then stopping the program once the sum reaches the intended value. The program is supposed to look like the 2nd picture. My professor also attached pseudocode for use to use as a format (3nd picture).
However, if I input a valid number twice, it doesn't give me the error message the first time but gives it on the second number even if they're the same number. What am I missing that could be causing this?
98 Replies
I'd use the debugger to see what the values are and when, and where the code goes at which point
how can i see that in visual studio?
$debug
Tutorial: Debug C# code and inspect data - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
I do see the issue tho
the code could also be cleaned up to not have to do that whole temp reading twice
i tried it without the temp reading line twice, but then i got results like this when i put in a number
well I didn't say you could just remove it 🙂
Protip: a
do..while
loop would be useful^
lol
while
runs 0 or more times.
do...while
runs 1 or more times
the difference is sometimes important, like hereoh ok so i put the validTemp = line in the do {} part and then put the same while{} statement after it
like this?
the
while
in a do..while
doesnt have a bodyRefer to my example
thats why you have a red squiggly there
what do you mean by doesnt have a body?
i see
the body moves to
do
in do..while
so like this?
no
you are still trying to have a body on the while
Look at ZZZZZZZZs example. Look at mine.
Read the error message your code has.
so the error message i have says that there should be a ; where the red squiggly is
And the error message is correct
but then my program looks like this when i add that. It's not including the "Enter temperature:" message when asking for new inputs
you cant do a single line fix my dude
Because your body is still outside of the
do
you gotta actually fix your entire program
ok so beyond the do...while loop, i have one other small error that i'm not sure what the cause is. i'm gettin this error message on the final line even though i have the line written above it in the exact same way with no errors
you're a genius
and i'm a moron lmao
ty
I had a friend in college who I was helping because he was legally blind, he would often mistake
{[(
etc, so I'm sensitive to those errors lolyeah i've made plenty of those errors already as you can tell lol
did you manage to fix the loop?
i think so, this is what i have
While. Has. No. Body. In. A. Do. While. Loop.
Look at where you are checking if its a valid temp and adding to your sum
check WHERE that is being done, in regards to your loop
so if the while has no body, i couldnt do an if...else loop inside of do...while loop? In my program, since I have to check if the temperature is valid, can i do that in the do...while loop or would that have to be outside of the loop?
i'm really trying to wrap my head around this, sorry if it's aggravating lol
the
do
body has the loop in it
turns into
because the picture i posted runs perfectly fine
It's that simple
it has to be inside the loop, as you are (potentially) reading multiple temperatures
so in my case:
do {
Console.WriteLine("Enter Temperature:");
} while (temp != SENTINEL;
and then my if statement under that all by itself?no
no no no and no.
no.
no? no.
:noted:
So, you know what a loop is right?
i think so
and that only the stuff INSIDE the loop will be repeated?
yes
with the code you just posted, how many times can
count
be incremented?
assume happy flow+1 per input
wrong
+1 total
it can never ever be incremented twice
why is that?
because there's no way for the end of my if...else statement to point back to the beginning of the do statement?
to restart
not entirely true, but I get what you mean
so, how do we fix this?
if we want the
if(validtemp)...
stuff to also be repeatedi'm not sure
i would say write the WriteLine and validTemp lines again but i dont think thats correct
hint: you have a loop.
the if statement is currently not looping
how is it not looping? i feel so dumb right now
because its outside the loop?
but i thought the if...else was the loop
what the fuck?
i would say you'd have to remove the ; but then i get an error
the red is the loop
the yellow is outside the loop
yeah so then in my head, i would remove the ; after the while part to include the if...else statement in the loop
but then i get an error
Angius
turns into
Quoted by
<@105026391237480448> from #Why is my program showing my else statement when the response is valid? (click here)
React with ❌ to remove this embed.
if you want something looped, put it inside the
do
body
if
is not a looping constructok so like this?
yep
ok awesome
it can be cleaned up a bit, if you want
how else could it be cleaned up?
this is a beginner course, so it can't look too cleaned up if you know what i mean
😄
yeah I get it
thank you so much for sticking with me through this btw, i really really appreciate it
is the same as
ah ok i see, i just formatted it like that because thats my professor had it in the pseudocode she provided
if you want extra style points, read up on
pattern matching
in conditions, and early exit
as part of understanding your problem, I wrote my own solution to it and its quite a bit shortercan i see?
actually there might be an imbalanced parens in there
no, we good 🙂
ok so as far as the string interpolation in your code, i noticed you dont have any of the placeholders like I had in my code. Why is that?
because I'm using actual string interpolation, and you used string formatting
see the dollar sign at the start of my strings?
yeah
that indicates that string interpolation is active
oh wait yeah you're right, i've done that before
so i should probably rewrite my WriteLine like you did instead of the string formatting
imho, yes.
other stuff to notice about my solution:
temp is >= MIN_TEMP and <= MAX_TEMP
if (!(int.TryParse...
+ continue;
the first is pattern matching, and the second is "early exit"
the idea is that instead of checking for valid, we check for INVALID
because then we can stop right therei see
so if temp is not valid, print an error and restart the loop
instead of go into an if-pyramid and stack elses at the end
yeah that seems a lot cleaner
well, its currently 5 am for me lol. thank you so much again for your help! i really appreciate it
np