C
C#3w ago
Kirri

✅ Troubles with looping

I'm building a text based adventure game to try and practice some syntaxes, as I'm a beginner. I'm stuck at this one part in the code where I need to repeat the loop only once if the user inputs an invalid input, however, despite me putting a valid input in there, it repeats it once, and after reinputting the same thing, it continues. I have one debug thing in there, please ignore that as I'll get rid of it whenever I figure this loop out. I'll post it after sending this, sending the entire code goes over the discord text limit.
46 Replies
Kirri
KirriOP3w ago
might be better to do a text file actually, ill send it in a min
MODiX
MODiX3w 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/ If your code is too long, you can post to https://paste.mod.gg/, save, and copy the link into chat for others to see your shared code!
Angius
Angius3w ago
That said, just paste the loop here No need to send the whole entire project
Kirri
KirriOP3w ago
alright, gimme a sec to get it then
bool validInput = false;

Console.WriteLine("What do you do? Sleep, or Get up?");
userInput = Console.ReadLine()?.Trim().ToLower() ?? string.Empty;
Console.WriteLine($"{validInput}");
do
{
switch (userInput)
{
case "sleep":
case "sleep.":
validInput = true;
Console.WriteLine("You shut the curtains, making it darker in your room. You lay back down and close your eyes, falling back to sleep.");
EndOfGame++;
break;
case "get up":
case "get up.":
validInput = true;
Console.WriteLine("You stretch, attempting to wake up. You get out of bed, picking out your clothes and getting dressed.");
Console.WriteLine("You walk out and eat some cereal for breakfast.");
System.Threading.Thread.Sleep(1000);
break;
default:
Console.WriteLine("Invalid selection. Please pick one of the listed options: Sleep, or Get up.");
break; // Invalid input; loop again
}
}
while (validInput == false);
bool validInput = false;

Console.WriteLine("What do you do? Sleep, or Get up?");
userInput = Console.ReadLine()?.Trim().ToLower() ?? string.Empty;
Console.WriteLine($"{validInput}");
do
{
switch (userInput)
{
case "sleep":
case "sleep.":
validInput = true;
Console.WriteLine("You shut the curtains, making it darker in your room. You lay back down and close your eyes, falling back to sleep.");
EndOfGame++;
break;
case "get up":
case "get up.":
validInput = true;
Console.WriteLine("You stretch, attempting to wake up. You get out of bed, picking out your clothes and getting dressed.");
Console.WriteLine("You walk out and eat some cereal for breakfast.");
System.Threading.Thread.Sleep(1000);
break;
default:
Console.WriteLine("Invalid selection. Please pick one of the listed options: Sleep, or Get up.");
break; // Invalid input; loop again
}
}
while (validInput == false);
sorry if it seems a bit sloppy, still trying to learn how to use it before optimizing it better i've also tried a lot of different combinations for the while part, but it never worked i've also tried simply using while instead of do-while, but it does the exact same thing i described
Angius
Angius3w ago
Odd, when I give the correct input, everything works as it should
Kirri
KirriOP3w ago
really? ill show you what it does on my end, give me a sec
Angius
Angius3w ago
No description
Kirri
KirriOP3w ago
No description
Angius
Angius3w ago
Ah, giving incorrect input creates an infinite loop
Kirri
KirriOP3w ago
this is what it's doing i haven't tested that yet cause I'm trying to make it work on my end, and it's not for some reason, despite having the same code i put in here
Angius
Angius3w ago
Huh, you give a valid answer, and it just repeats the question?
Kirri
KirriOP3w ago
yes, but only once if i put a valid answer after it repeats, it works just fine
Angius
Angius3w ago
Seems like it would be something wrong with the outside code, then Is this bit of code inside of another loop, maybe?
Kirri
KirriOP3w ago
no. if you want, ill send the entire code. it's not that lengthy, as in the text based adventure game, i got stuck in the prologue section
Angius
Angius3w ago
Sure $paste
MODiX
MODiX3w ago
If your code is too long, you can post to https://paste.mod.gg/, save, and copy the link into chat for others to see your shared code!
Kirri
KirriOP3w ago
https://paste.mod.gg/uybtfujvcgxn/0 runOnce = true was a boolean i tried to use to fix this and abandoned it when it didn't work, just havent got rid of it yet
Angius
Angius3w ago
Right, I was able to reproduce the issue
Kirri
KirriOP3w ago
alright i tried looking over it again, i can't seem to find the issue lol
Angius
Angius3w ago
Ah Well You ask the question twice And get input twice
Kirri
KirriOP3w ago
i did?
Angius
Angius3w ago
No description
Angius
Angius3w ago
Yes
Kirri
KirriOP3w ago
yeah i must be blind cause i didnt see that at all thank you, sorry for wasting your time lol
Angius
Angius3w ago
No problem It's the smallest issues like this that are the hardest to notice lol
Kirri
KirriOP3w ago
i swear i looked over it 5 times lol true
Angius
Angius3w ago
Debugger helps a lot Learning how to use even the basics like breakpoints is a gamechanger
Kirri
KirriOP3w ago
so the loop does work, it's just the code that was screwed up
Angius
Angius3w ago
$debug
MODiX
MODiX3w 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
Angius3w ago
Yeah
Kirri
KirriOP3w ago
tbh idk how to debug yet
Angius
Angius3w ago
Hence the link It teaches you how
Kirri
KirriOP3w ago
so i've been using Console.WriteLine("status is {status}") for my debugging lol thank you for that is there a resolved tag or something i can put on this post so others know its solved? hey, quick question, how did you get the debugger to stay in the app? mine opens a seperate console tab
Angius
Angius3w ago
Not sure tbh Been a while since I used VS Oh, I remember the tag, $close
MODiX
MODiX3w ago
If you have no further questions, please use /close to mark the forum thread as answered
Angius
Angius3w ago
'Ere
Kirri
KirriOP3w ago
No description
Kirri
KirriOP3w ago
does this can't really use breakpoints bc of it
Angius
Angius3w ago
How come?
Kirri
KirriOP3w ago
well, despite it telling me to press shift f5 to stop the debugging, it doesn't unless im dumb ok well it does, but the point is i have to switch back and forth between visual studio and the console tab app thing repeatedly instead of it all being in visual studio
Angius
Angius3w ago
How do I run my .NET console app in the integrated terminal instead...
Whenever you run or debug your .NET Console App, it opens a new windows terminal where the program is run. Isn't there a way to instead make that process headless and only show it in the integrated terminal or something? VSCode has a similar feature.
Kirri
KirriOP3w ago
thank you, i was looking for that and breakpoints magically decided to work now did nothing different but it worked
Angius
Angius3w ago
It do be like that sometimes lol
Kirri
KirriOP3w ago
$close
MODiX
MODiX3w ago
If you have no further questions, please use /close to mark the forum thread as answered

Did you find this page helpful?