C
C#9mo ago
Mix

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?
No description
No description
No description
98 Replies
Angius
Angius9mo ago
I'd use the debugger to see what the values are and when, and where the code goes at which point
Mix
MixOP9mo ago
how can i see that in visual studio?
Pobiega
Pobiega9mo ago
$debug
MODiX
MODiX9mo ago
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.
Angius
Angius9mo ago
No description
Angius
Angius9mo ago
I do see the issue tho
Pobiega
Pobiega9mo ago
the code could also be cleaned up to not have to do that whole temp reading twice
Mix
MixOP9mo ago
i tried it without the temp reading line twice, but then i got results like this when i put in a number
No description
Pobiega
Pobiega9mo ago
well I didn't say you could just remove it 🙂
Angius
Angius9mo ago
Protip: a do..while loop would be useful
Pobiega
Pobiega9mo ago
^
Mix
MixOP9mo ago
lol
Pobiega
Pobiega9mo ago
while runs 0 or more times. do...while runs 1 or more times the difference is sometimes important, like here
Mix
MixOP9mo ago
oh ok so i put the validTemp = line in the do {} part and then put the same while{} statement after it
Angius
Angius9mo ago
string? input;
do
{
Console.WriteLine("Yee!");
input = Console.ReadLine();
} while (input.Equals("haw!", StringComparison.OrdinalIgnoreCase));

Console.WriteLine("Aww, you broke the chain...");
string? input;
do
{
Console.WriteLine("Yee!");
input = Console.ReadLine();
} while (input.Equals("haw!", StringComparison.OrdinalIgnoreCase));

Console.WriteLine("Aww, you broke the chain...");
Mix
MixOP9mo ago
like this?
No description
Pobiega
Pobiega9mo ago
the while in a do..while doesnt have a body
Angius
Angius9mo ago
Refer to my example
Pobiega
Pobiega9mo ago
thats why you have a red squiggly there
Angius
Angius9mo ago
No description
Mix
MixOP9mo ago
what do you mean by doesnt have a body?
Pobiega
Pobiega9mo ago
while (true)
{
//this is a body
}

do
{
// this is a body
}
while(true);
while (true)
{
//this is a body
}

do
{
// this is a body
}
while(true);
Mix
MixOP9mo ago
i see
Pobiega
Pobiega9mo ago
the body moves to do in do..while
Mix
MixOP9mo ago
so like this?
No description
Pobiega
Pobiega9mo ago
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.
Angius
Angius9mo ago
do {
body
} while(condition); // <--- semicolon and nothing else
do {
body
} while(condition); // <--- semicolon and nothing else
Mix
MixOP9mo ago
so the error message i have says that there should be a ; where the red squiggly is
Angius
Angius9mo ago
And the error message is correct
Mix
MixOP9mo ago
but then my program looks like this when i add that. It's not including the "Enter temperature:" message when asking for new inputs
No description
Pobiega
Pobiega9mo ago
you cant do a single line fix my dude
Angius
Angius9mo ago
Because your body is still outside of the do
Pobiega
Pobiega9mo ago
you gotta actually fix your entire program
Angius
Angius9mo ago
Mix
MixOP9mo ago
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
No description
Angius
Angius9mo ago
No description
Mix
MixOP9mo ago
you're a genius and i'm a moron lmao ty
Angius
Angius9mo ago
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 lol
Mix
MixOP9mo ago
yeah i've made plenty of those errors already as you can tell lol
Pobiega
Pobiega9mo ago
did you manage to fix the loop?
Mix
MixOP9mo ago
i think so, this is what i have
No description
Angius
Angius9mo ago
While. Has. No. Body. In. A. Do. While. Loop.
Pobiega
Pobiega9mo ago
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
Mix
MixOP9mo ago
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
Pobiega
Pobiega9mo ago
the do body has the loop in it
Angius
Angius9mo ago
while (condition) {
code
}
while (condition) {
code
}
turns into
do
{
code
} while (condition);
do
{
code
} while (condition);
Mix
MixOP9mo ago
because the picture i posted runs perfectly fine
Angius
Angius9mo ago
It's that simple
Pobiega
Pobiega9mo ago
it has to be inside the loop, as you are (potentially) reading multiple temperatures
Mix
MixOP9mo ago
so in my case: do { Console.WriteLine("Enter Temperature:"); } while (temp != SENTINEL; and then my if statement under that all by itself?
Mix
MixOP9mo ago
No description
Pobiega
Pobiega9mo ago
no no no no and no. no. no? no.
Mix
MixOP9mo ago
:noted:
Pobiega
Pobiega9mo ago
So, you know what a loop is right?
Mix
MixOP9mo ago
i think so
Pobiega
Pobiega9mo ago
and that only the stuff INSIDE the loop will be repeated?
Mix
MixOP9mo ago
yes
Pobiega
Pobiega9mo ago
with the code you just posted, how many times can count be incremented? assume happy flow
Mix
MixOP9mo ago
+1 per input
Pobiega
Pobiega9mo ago
wrong +1 total it can never ever be incremented twice why is that?
Mix
MixOP9mo ago
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
Pobiega
Pobiega9mo ago
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 repeated
Mix
MixOP9mo ago
i'm not sure i would say write the WriteLine and validTemp lines again but i dont think thats correct
Pobiega
Pobiega9mo ago
hint: you have a loop. the if statement is currently not looping
Mix
MixOP9mo ago
how is it not looping? i feel so dumb right now
Pobiega
Pobiega9mo ago
because its outside the loop?
Mix
MixOP9mo ago
but i thought the if...else was the loop
Pobiega
Pobiega9mo ago
what the fuck?
Mix
MixOP9mo ago
i would say you'd have to remove the ; but then i get an error
Pobiega
Pobiega9mo ago
No description
Pobiega
Pobiega9mo ago
the red is the loop the yellow is outside the loop
Mix
MixOP9mo ago
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
MODiX
MODiX9mo ago
Angius
while (condition) {
code
}
while (condition) {
code
}
turns into
do
{
code
} while (condition);
do
{
code
} while (condition);
React with ❌ to remove this embed.
Pobiega
Pobiega9mo ago
if you want something looped, put it inside the do body if is not a looping construct
Mix
MixOP9mo ago
ok so like this?
No description
Pobiega
Pobiega9mo ago
yep
Mix
MixOP9mo ago
ok awesome
Pobiega
Pobiega9mo ago
it can be cleaned up a bit, if you want
Mix
MixOP9mo ago
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
Pobiega
Pobiega9mo ago
😄 yeah I get it
Mix
MixOP9mo ago
thank you so much for sticking with me through this btw, i really really appreciate it
Pobiega
Pobiega9mo ago
validTemp = a..z;
if(validTemp)
{
...
validTemp = a..z;
if(validTemp)
{
...
is the same as
if(a..z)
{
...
if(a..z)
{
...
Mix
MixOP9mo ago
ah ok i see, i just formatted it like that because thats my professor had it in the pseudocode she provided
Mix
MixOP9mo ago
No description
Pobiega
Pobiega9mo ago
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 shorter
Mix
MixOP9mo ago
can i see?
Pobiega
Pobiega9mo ago
const int MIN_TEMP = -20;
const int MAX_TEMP = 120;
const int SENTINEL = 999;

int sum = 0;
int count = 0;

int temp;
do
{
Console.WriteLine($"Enter temperature between {MIN_TEMP} and {MAX_TEMP}. Enter {SENTINEL} to stop.");
if (!(int.TryParse(Console.ReadLine(), out temp) && temp is >= MIN_TEMP and <= MAX_TEMP))
{
Console.WriteLine($"Valid temperatures range between {MIN_TEMP} and {MAX_TEMP}. Please try again.");
continue;
}

sum += temp;
count++;
} while (temp != SENTINEL);

var mean = sum / count;
Console.WriteLine($"You entered {count} temperatures.");
Console.WriteLine($"The mean temperature is {mean} degrees.");
const int MIN_TEMP = -20;
const int MAX_TEMP = 120;
const int SENTINEL = 999;

int sum = 0;
int count = 0;

int temp;
do
{
Console.WriteLine($"Enter temperature between {MIN_TEMP} and {MAX_TEMP}. Enter {SENTINEL} to stop.");
if (!(int.TryParse(Console.ReadLine(), out temp) && temp is >= MIN_TEMP and <= MAX_TEMP))
{
Console.WriteLine($"Valid temperatures range between {MIN_TEMP} and {MAX_TEMP}. Please try again.");
continue;
}

sum += temp;
count++;
} while (temp != SENTINEL);

var mean = sum / count;
Console.WriteLine($"You entered {count} temperatures.");
Console.WriteLine($"The mean temperature is {mean} degrees.");
actually there might be an imbalanced parens in there no, we good 🙂
Mix
MixOP9mo ago
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?
Pobiega
Pobiega9mo ago
because I'm using actual string interpolation, and you used string formatting see the dollar sign at the start of my strings?
Mix
MixOP9mo ago
yeah
Pobiega
Pobiega9mo ago
that indicates that string interpolation is active
Mix
MixOP9mo ago
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
Pobiega
Pobiega9mo ago
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 there
Mix
MixOP9mo ago
i see
Pobiega
Pobiega9mo ago
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
Mix
MixOP9mo ago
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
Pobiega
Pobiega9mo ago
np
Want results from more Discord servers?
Add your server