❔ Having issues placing a set of Console.WriteLine statements in the correct spot
First, I am a beginner, so I would REALLY appreciate explanations on criticism. That way, I can understand why my stuff doesnt work, instead of how to fix it!
I am making a "babysitting" simulator, and the purpose of this is to reinforce my knowledge as of now. I am not completely done with the fundamentals, I simply want to get this fully working as a personal milestone. Thus, the way I may be doing things might be super inefficient. I am definitely open to suggestions on how to make it less complicated or simply better!
Now, to the root of my current issue. In my foreach loop, every time we enter a new day, I wish to clear the console, print out the baby's "hunger" and "entertainment" values, and print out any events that have happened. I also wish to properly update the values after Events and User Input have been calculated, when entering a new day. My issue is that I do not know where to place the statement that prints out the "hunger" and "entertainment". Testing the code in its current state properly updates values, and I am able to win or lose the game. However, the only data being printed out is the random event, I am not being shown the hunger or entertainment.
Where should I place the "Console.WriteLine()" Statements in order to properly display the values of hunger and ent after calculations? (including keypress from the previous day)
If this is WAYY too much text, and I should make it shorter, please let me know!
PASTEBIN: https://pastebin.com/wEt8FvzV
Pastebin
babysim - Pastebin.com
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
38 Replies
((P.S, there is a horrendous amount of random whitespace, that is the result of me copy pasting and deleting stuff around. Apologies if its cancerous
((P.P.S, I forgot to mention, but i have read that using Environment.exit() SUCKS, but I dont know why. I kept in there because i dont know what to replace it with, if it truly is bad and i should figure out a replacement please let me know!!
It would probably help to sit down and think about each thing you want to do each day. In this case, why not clear console and print day+hunger+entertainment at the start of each day?
That is indeed my goal, my issue is that I cannot get the WriteLine statements to print (or rather, they are working as intended but something is erasing it)
I am confident that this is an oversight and that placing the WriteLine statements in the right place will help, however I’m stumped as to where that should be…
(Also, sorry for the late response, I forgot to check this channel)
I will check my code again now that I’ve taken a longer break, perhaps I have missed a Console.Clear that is erasing my WriteLine? If not, I am not sure as to what the problem would be.
As well, I need to figure out how to sync the numerical array for the week, as well as the string version, so I can print the day every time the foreach loop goes to a new day
Perhaps I should use a different approach to pass time rather than a foreach loop? If so, what technique should i use? I apologize if I seem like I want you guys to build the code for me, I am trying to see the big picture rather than getting hung up on an error. Perhaps the error stems from the inherent fault of the foreach loop technique? I don’t know! I thank you for any insight you are able to provide
so this is a tamagotchi
I never had one but I think
Press buttons and hope it don’t die
use the debugger. set a breakpoint where you print your messages and then step through and see what line deletes it and how it gets there
$debug
Tutorial: Debug C# code - 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.
I wasn’t aware that this was a thing! Thank you, I will try it as soon as possible.
Do you believe I can “feasibly” achieve my goal using my current method? Or should I be doing something completely different?
Its definately doable, might involve a bit more changing than just removing or adding a single line thou
Good to know, thank you
Whenever I try to use the debugger, I get this exception caused by the Console.Clear statement
I have no idea what it means </3
Hm, what IDE is that?
Visual Studio Code
Oh. Well, it should work, but maybe disable the integrated terminal
Not sure how to do that thou
ill do that in a sec
I personally highly recommend using VS or Rider, but vsc with devkit should be fine
Alright, I was using Vs code bc the Microsoft course i was following used it, but I definitely will
Closed the integrated terminal, and it still does it
I think I have vs installed, let me see if i can port over the code there
Can i directly port the code, or should I be doing something special instead?
Should just be able to open the same project
Don't need to port anything:)
Ah, alright!
Hm, when I try to open it theres nothing on the screen
completely blank
Uh oh
Can you expand the solution explorer for me?
wait im an idiot
I didnt open the program.cs file, but i cant even find it
You shouldnt
It should be the csproj file
OH, i got it working
I had to mess with the explorer
So, the code is visible
But when I click debug, it does nothing, and then gives me this
NEVERMIND
Got it working again, the debug selection was on the wrong thing
...I now see why environment.exit is bad
I cant even see why my baby dies before the program closes, i was doing everything on the command shell so i never realized
Slap a debug breakpoint in there and step through it 🙂
I found the issue!!
The initial Console.Clear is deleting everything AFTER it gets printed because once it reaches the end, it skips over to the next day
The issue is not in the Console.WriteLine, but in the positioning of my keypress snippet. That is the one thing stopping the foreach loop each day, and because its misplaced its looping at the wrong point
And I fixed the issue!
Honestly thank you for reminding me of the debug tool, I had no idea what it did and i was kind of afraid of it.
I have another question which might be completely irrelevant but its still important
Is it possible for me to sync the two Week related arrays in a nested foreach loop, so that I can print the day? Right now, since the clock system is working on a numerical counter, I dont know how to add the string variant of the array without doing a completely separate foreach loop (or, is that what I should do?)
Yeah I'm pretty sure you can combine those quite easily
One way would be to use a for loop instead of foreach
That way you'd have a number that is both your counter and the index for the name array
What is the difference between a foreach loop and a for? This is a dumb question, but I havent used a for/while loop yet
Iteration statements -for, foreach, do, and while - C#
C# iteration statements (for, foreach, do, and while) repeatedly execute a block of code. You use those statements to create loops or iterate through a collection.
Thank you\
I am having difficulties applying the "for loop" and connecting it to both the int array and string array
How would one connect the two? as in, I dont know how to switch the index for both arrays every time it loops...
I imagined you'd replace the int array
After all, it's just the index shifted by 1
So you don't need that as an array, you can calculate it based on the index variable
Oh, I see what you mean
Another issue I had is that I dont know how to make the for loop interact with the array (Not both, just one at all)
That is why I used the foreach approach first, so I am unsure how to use the for loop
The for loop gives you a number
Use that number to index the array
?
I don't understand, apologies
I was doing some research on traversing arrays with for loops
I should make the for loop initialize the currentDayCounter, and then make the condition check if currentDayCounter is the length of the array?
So that it does something on each day, and allows me to print the day name
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.