C
C#2mo ago
blunt

✅ while loops

while loops keep repeating the same
Console.WriteLine("");
Console.WriteLine("");
over and over without waiting for the user to respond to the
String a = Console.ReadLine();
String a = Console.ReadLine();
anyone know how to solve this?
18 Replies
ero
ero2mo ago
$code
MODiX
MODiX2mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
ero
ero2mo ago
$ask
MODiX
MODiX2mo ago
How to get the best help :catpog: Make a post in #help or one of the topic channels under Development. Avoid asking :catthinking: Can anybody help me? :catthinking: Has anyone used XYZ? :catthinking: Why doesn't my code work? C# is a big area! No one knows they can help unless you tell them about the small area you're trying to work in. Explain what you are doing, and potentially why for as much context as possible. Avoid screenshots where possible, share code directly in Discord. Type $code into chat to learn how to post code. See https://www.nohello.net and https://dontasktoask.com if you want common help chat room etiquette.
ero
ero2mo ago
tldr show your actual code
blunt
bluntOP2mo ago
what is tldr
Keswiik
Keswiik2mo ago
too long; didn't read
blunt
bluntOP2mo ago
Console.WriteLine("enter your name");
String name = Console.ReadLine();
while (name == "")
{
Console.WriteLine("Please enter a name");
}
Console.WriteLine("enter your name");
String name = Console.ReadLine();
while (name == "")
{
Console.WriteLine("Please enter a name");
}
my code .
TheRanger
TheRanger2mo ago
well, in the while loop u never asked for user input
Keswiik
Keswiik2mo ago
^ if you put in an empty name you're gonna get stuck looping forever
blunt
bluntOP2mo ago
how do i not i did in visual studio
Keswiik
Keswiik2mo ago
the code you just sent has your ReadLine outside of the while loop, so you definitely didn't
TheRanger
TheRanger2mo ago
code wise i mean
Keswiik
Keswiik2mo ago
and this is one of the situations where a do-while makes sense
do {
// your code here to get the name
} while (name is invalid)
do {
// your code here to get the name
} while (name is invalid)
blunt
bluntOP2mo ago
ohh tysm
Keswiik
Keswiik2mo ago
I'd also recommend using string.IsNullOrEmpty or string.IsNullOrWhitespace over someString == ""
blunt
bluntOP2mo ago
ok thanks
Keswiik
Keswiik2mo ago
:PepoSalute:
Want results from more Discord servers?
Add your server