C
C#4w 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
ero4w ago
$code
MODiX
MODiX4w 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
ero4w ago
$ask
MODiX
MODiX4w 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
ero4w ago
tldr show your actual code
blunt
bluntOP4w ago
what is tldr
Keswiik
Keswiik4w ago
too long; didn't read
blunt
bluntOP4w 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
TheRanger4w ago
well, in the while loop u never asked for user input
Keswiik
Keswiik4w ago
^ if you put in an empty name you're gonna get stuck looping forever
blunt
bluntOP4w ago
how do i not i did in visual studio
Keswiik
Keswiik4w ago
the code you just sent has your ReadLine outside of the while loop, so you definitely didn't
TheRanger
TheRanger4w ago
code wise i mean
Keswiik
Keswiik4w 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
bluntOP4w ago
ohh tysm
Keswiik
Keswiik4w ago
I'd also recommend using string.IsNullOrEmpty or string.IsNullOrWhitespace over someString == ""
blunt
bluntOP4w ago
ok thanks
Keswiik
Keswiik4w ago
:PepoSalute:
Want results from more Discord servers?
Add your server