C
C#7mo 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
Angius7mo ago
I'd use the debugger to see what the values are and when, and where the code goes at which point
Mix
Mix7mo ago
how can i see that in visual studio?
Pobiega
Pobiega7mo ago
$debug
MODiX
MODiX7mo 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
Angius7mo ago
No description
Angius
Angius7mo ago
I do see the issue tho
Pobiega
Pobiega7mo ago
the code could also be cleaned up to not have to do that whole temp reading twice
Mix
Mix7mo 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
Pobiega7mo ago
well I didn't say you could just remove it 🙂
Angius
Angius7mo ago
Protip: a do..while loop would be useful
Pobiega
Pobiega7mo ago
^
Mix
Mix7mo ago
lol
Pobiega
Pobiega7mo ago
while runs 0 or more times. do...while runs 1 or more times the difference is sometimes important, like here
Mix
Mix7mo ago
oh ok so i put the validTemp = line in the do {} part and then put the same while{} statement after it
Angius
Angius7mo 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...");
Want results from more Discord servers?
Add your server