C
C#4mo 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
Angius4mo ago
I'd use the debugger to see what the values are and when, and where the code goes at which point
Mix
Mix4mo ago
how can i see that in visual studio?
Pobiega
Pobiega4mo ago
$debug
MODiX
MODiX4mo 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
Angius4mo ago
No description
Angius
Angius4mo ago
I do see the issue tho
Pobiega
Pobiega4mo ago
the code could also be cleaned up to not have to do that whole temp reading twice
Mix
Mix4mo 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
Pobiega4mo ago
well I didn't say you could just remove it 🙂
Angius
Angius4mo ago
Protip: a do..while loop would be useful
Pobiega
Pobiega4mo ago
^
Mix
Mix4mo ago
lol
Pobiega
Pobiega4mo ago
while runs 0 or more times. do...while runs 1 or more times the difference is sometimes important, like here
Mix
Mix4mo ago
oh ok so i put the validTemp = line in the do {} part and then put the same while{} statement after it
Angius
Angius4mo 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
Mix4mo ago
like this?
No description
Pobiega
Pobiega4mo ago
the while in a do..while doesnt have a body
Angius
Angius4mo ago
Refer to my example
Pobiega
Pobiega4mo ago
thats why you have a red squiggly there
Angius
Angius4mo ago
No description
Mix
Mix4mo ago
what do you mean by doesnt have a body?
Pobiega
Pobiega4mo 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
Mix4mo ago
i see
Pobiega
Pobiega4mo ago
the body moves to do in do..while
Mix
Mix4mo ago
so like this?
No description
Pobiega
Pobiega4mo 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
Angius4mo ago
do {
body
} while(condition); // <--- semicolon and nothing else
do {
body
} while(condition); // <--- semicolon and nothing else
Mix
Mix4mo ago
so the error message i have says that there should be a ; where the red squiggly is
Angius
Angius4mo ago
And the error message is correct
Mix
Mix4mo 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
Pobiega4mo ago
you cant do a single line fix my dude
Angius
Angius4mo ago
Because your body is still outside of the do
Pobiega
Pobiega4mo ago
you gotta actually fix your entire program
Angius
Angius4mo ago
Mix
Mix4mo 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
Angius4mo ago
No description
Mix
Mix4mo ago
you're a genius and i'm a moron lmao ty
Angius
Angius4mo 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
Mix4mo ago
yeah i've made plenty of those errors already as you can tell lol
Pobiega
Pobiega4mo ago
did you manage to fix the loop?
Mix
Mix4mo ago
i think so, this is what i have
No description
Angius
Angius4mo ago
While. Has. No. Body. In. A. Do. While. Loop.
Pobiega
Pobiega4mo 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
Mix4mo 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
Pobiega4mo ago
the do body has the loop in it
Angius
Angius4mo ago
while (condition) {
code
}
while (condition) {
code
}
turns into
do
{
code
} while (condition);
do
{
code
} while (condition);
Mix
Mix4mo ago
because the picture i posted runs perfectly fine
Angius
Angius4mo ago
It's that simple
Pobiega
Pobiega4mo ago
it has to be inside the loop, as you are (potentially) reading multiple temperatures
Mix
Mix4mo ago
so in my case: do { Console.WriteLine("Enter Temperature:"); } while (temp != SENTINEL; and then my if statement under that all by itself?
Mix
Mix4mo ago
No description
Pobiega
Pobiega4mo ago
no no no no and no. no. no? no.
Mix
Mix4mo ago
:noted:
Pobiega
Pobiega4mo ago
So, you know what a loop is right?
Mix
Mix4mo ago
i think so
Pobiega
Pobiega4mo ago
and that only the stuff INSIDE the loop will be repeated?
Mix
Mix4mo ago
yes
Pobiega
Pobiega4mo ago
with the code you just posted, how many times can count be incremented? assume happy flow
Mix
Mix4mo ago
+1 per input
Pobiega
Pobiega4mo ago
wrong +1 total it can never ever be incremented twice why is that?
Mix
Mix4mo 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
Pobiega4mo 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
Mix4mo ago
i'm not sure i would say write the WriteLine and validTemp lines again but i dont think thats correct
Pobiega
Pobiega4mo ago
hint: you have a loop. the if statement is currently not looping
Mix
Mix4mo ago
how is it not looping? i feel so dumb right now
Pobiega
Pobiega4mo ago
because its outside the loop?
Mix
Mix4mo ago
but i thought the if...else was the loop
Pobiega
Pobiega4mo ago
what the fuck?
Mix
Mix4mo ago
i would say you'd have to remove the ; but then i get an error
Pobiega
Pobiega4mo ago
No description
Pobiega
Pobiega4mo ago
the red is the loop the yellow is outside the loop
Mix
Mix4mo 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
MODiX4mo 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
Pobiega4mo ago
if you want something looped, put it inside the do body if is not a looping construct
Mix
Mix4mo ago
ok so like this?
No description
Pobiega
Pobiega4mo ago
yep
Mix
Mix4mo ago
ok awesome
Pobiega
Pobiega4mo ago
it can be cleaned up a bit, if you want
Mix
Mix4mo 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
Pobiega4mo ago
😄 yeah I get it
Mix
Mix4mo ago
thank you so much for sticking with me through this btw, i really really appreciate it
Pobiega
Pobiega4mo ago
validTemp = a..z;
if(validTemp)
{
...
validTemp = a..z;
if(validTemp)
{
...
is the same as
if(a..z)
{
...
if(a..z)
{
...
Mix
Mix4mo ago
ah ok i see, i just formatted it like that because thats my professor had it in the pseudocode she provided
Mix
Mix4mo ago
No description
Pobiega
Pobiega4mo 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
Mix4mo ago
can i see?
Pobiega
Pobiega4mo 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
Mix4mo 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
Pobiega4mo ago
because I'm using actual string interpolation, and you used string formatting see the dollar sign at the start of my strings?
Mix
Mix4mo ago
yeah
Pobiega
Pobiega4mo ago
that indicates that string interpolation is active
Mix
Mix4mo 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
Pobiega4mo 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
Mix4mo ago
i see
Pobiega
Pobiega4mo 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
Mix4mo 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
Pobiega4mo ago
np
Want results from more Discord servers?
Add your server
More Posts