Console Doesn't Clear and Provides nested outputs

Like the title says and i dont get it
No description
68 Replies
Protagonist
ProtagonistOP2w ago
public static void DisplaySeatingStatus()
{
Console.Clear();
var allSeats = FirstClassSeats.Concat(EconomyClassSeats).ToList();

if (!allSeats.Any())
{
Console.WriteLine("No seats available to display.");
Console.WriteLine("\nPress any key to return to the main menu...");
Console.ReadKey(true);
return;
}

Console.WriteLine("Choose sorting order:\n1. Sort by Seat Number\n2. Sort by Passenger Name\n3. Sort by Availability Status");
var key = Console.ReadKey(true).Key;

Console.Clear();

var sortedSeats = key switch
{
ConsoleKey.D1 => allSeats.OrderBy(s => int.Parse(s.SeatNumber[..^1])).ThenBy(s => s.SeatNumber.Last()).ToList(),
ConsoleKey.D2 => allSeats.OrderBy(s => s.IsOccupied ? Passengers.FirstOrDefault(p => p.BookedSeats.Contains(s))?.Name ?? "" : "")
.ThenBy(s => s.SeatNumber).ToList(),
ConsoleKey.D3 => allSeats.OrderBy(s => s.IsOccupied ? 0 : 1).ThenBy(s => s.SeatNumber).ToList(),
_ => allSeats.OrderBy(s => int.Parse(s.SeatNumber[..^1])).ThenBy(s => s.SeatNumber.Last()).ToList()
};
Console.Clear();
Console.WriteLine("\nSeating Status:");
foreach (var seat in sortedSeats)
{
string status = seat.IsOccupied ? "Occupied" : "Available";
string passengerName = seat.IsOccupied ? Passengers.FirstOrDefault(p => p.BookedSeats.Contains(seat))?.Name ?? "Unknown Passenger" : "";
Console.WriteLine($"Seat {seat.SeatNumber} ({seat.Class}): {status} {passengerName}");
}

Console.WriteLine("\nPress any key to return to the main menu...");
Console.ReadKey(true);
}
public static void DisplaySeatingStatus()
{
Console.Clear();
var allSeats = FirstClassSeats.Concat(EconomyClassSeats).ToList();

if (!allSeats.Any())
{
Console.WriteLine("No seats available to display.");
Console.WriteLine("\nPress any key to return to the main menu...");
Console.ReadKey(true);
return;
}

Console.WriteLine("Choose sorting order:\n1. Sort by Seat Number\n2. Sort by Passenger Name\n3. Sort by Availability Status");
var key = Console.ReadKey(true).Key;

Console.Clear();

var sortedSeats = key switch
{
ConsoleKey.D1 => allSeats.OrderBy(s => int.Parse(s.SeatNumber[..^1])).ThenBy(s => s.SeatNumber.Last()).ToList(),
ConsoleKey.D2 => allSeats.OrderBy(s => s.IsOccupied ? Passengers.FirstOrDefault(p => p.BookedSeats.Contains(s))?.Name ?? "" : "")
.ThenBy(s => s.SeatNumber).ToList(),
ConsoleKey.D3 => allSeats.OrderBy(s => s.IsOccupied ? 0 : 1).ThenBy(s => s.SeatNumber).ToList(),
_ => allSeats.OrderBy(s => int.Parse(s.SeatNumber[..^1])).ThenBy(s => s.SeatNumber.Last()).ToList()
};
Console.Clear();
Console.WriteLine("\nSeating Status:");
foreach (var seat in sortedSeats)
{
string status = seat.IsOccupied ? "Occupied" : "Available";
string passengerName = seat.IsOccupied ? Passengers.FirstOrDefault(p => p.BookedSeats.Contains(seat))?.Name ?? "Unknown Passenger" : "";
Console.WriteLine($"Seat {seat.SeatNumber} ({seat.Class}): {status} {passengerName}");
}

Console.WriteLine("\nPress any key to return to the main menu...");
Console.ReadKey(true);
}
Monsieur Wholesome
It kinda looks like old output? Like a Windows Terminal Buffering error Old output being overwritten with new output
Protagonist
ProtagonistOP2w ago
its oldouput with new output, i was thinking it to be a buffering issue but ive found no workarounds I've tried clearing console almost everywhere n it doe nothings
Matt
Matt2w ago
I get that issue all the time with random apps (e.g npm i, wget, ...)
Protagonist
ProtagonistOP2w ago
so how does one resolve it
Matt
Matt2w ago
sadly never quite figured out how it happens or how to solve it :/
Protagonist
ProtagonistOP2w ago
oof sad
Matt
Matt2w ago
it only happens on Windows too, never had it happen in Linux or MacOS
Protagonist
ProtagonistOP2w ago
bruh i know its 100% that method, it cos i go in and out of it via a menu n it shows the old with the new
WhiteBlackGoose
Not all terminals support the clear command What terminal are you using? and what to do is: try another one
Protagonist
ProtagonistOP2w ago
the default one VS uses then tried powershell n still same thing also it clears anything else i want but in that method specifically it doesnt
WhiteBlackGoose
oh
Protagonist
ProtagonistOP2w ago
or that part in the method rather
WhiteBlackGoose
if that's the case then you need to track down yourself what prevents it from clearing in that method try removing parts of your method until it clears it
Protagonist
ProtagonistOP2w ago
I've tried n tried for past hour or so
WhiteBlackGoose
and? what have you come down to so far
Protagonist
ProtagonistOP2w ago
that nothing I do make its clear even when its logically supposed to clear it doesnt
WhiteBlackGoose
you said your other methods work
Protagonist
ProtagonistOP2w ago
yes this specific section of my code doesnt want to clear
WhiteBlackGoose
then you need to find the difference between this method and others what prevents it from clearing
Protagonist
ProtagonistOP2w ago
if it was that easy i would of figured it out
WhiteBlackGoose
To achieve that, you need to remove parts of the method until it works once it works you know that the last bit you removed prevented it from working so you can look deeper into it realistically, the only thing I can think of is try removing Console.ReadKey()
Protagonist
ProtagonistOP2w ago
Ive added and removed tons of things, i was thinking maybe its deeper than that n looked elswhere but it dont seem to budge
WhiteBlackGoose
if you've already performed this procedure you can tell what prevents it from clearing
Protagonist
ProtagonistOP2w ago
becuase it displays info the read key should be there its literally not possible xD
WhiteBlackGoose
I think you're still not understanding To fix a problem you first need to find it In order to find the problem you need to perform this procedure of debugging, in particular what I suggest is removing parts of code until it works and see what was causing the problem Once you know what the problem is, you revert the changes, and think how you can fix it
Protagonist
ProtagonistOP2w ago
The issue is, I know where the issue is happening its just a matter of why I've debugged too btw
WhiteBlackGoose
and where does it happen? Which exact line in your code prevents Console.Clear from working properly?
Protagonist
ProtagonistOP2w ago
after the code is finished it seems to stay persistant even if i include a console clear at the end
No description
Protagonist
ProtagonistOP2w ago
so when leaving the method i provided the console doesnt clear for some reason
WhiteBlackGoose
You didn't answer the question
Protagonist
ProtagonistOP2w ago
it could be the readkey but that is needed to wait for user input to see if theyre done reading after the readkey i the issue it dont clear console
Protagonist
ProtagonistOP2w ago
it doesnt recognise these as lines or something
No description
Protagonist
ProtagonistOP2w ago
does no one know how to fix thi s @jcotton42 Mr pro ❤️
jcotton42
jcotton422w ago
?
Protagonist
ProtagonistOP2w ago
Theres this weird issue im having where the console doesnt clear n maintains throughout the lifetime of the console, the method i have is above
jcotton42
jcotton422w ago
Well, it seems someone was already helping, so.
Protagonist
ProtagonistOP2w ago
they dissapeared and everyone here who tried to help cant figure it out, so.
ero
ero2w ago
Put a break point on the Clear line. Show us that it's hitting that break point. Then step over it and show the result on the console
Protagonist
ProtagonistOP2w ago
this is before i step over
No description
Protagonist
ProtagonistOP2w ago
this is after
No description
Protagonist
ProtagonistOP2w ago
makes no sense at all what its printing
ero
ero2w ago
What do you mean what it's printing doesn't make sense? Did you do a single step over like I asked?
Protagonist
ProtagonistOP2w ago
ive done it plenty of times, ive showed u what happened after debug Seat 25G (economy): Available Seat 26A (economy): Available Seat 26B (economy): Available Welcome to Metropolitan Airlines it still hangs up on the console
ero
ero2w ago
Doing a single step over should not be able to print anything You must have done it incorrectly
Protagonist
ProtagonistOP2w ago
exactly yet it still displays
ero
ero2w ago
It cannot It is impossible
Protagonist
ProtagonistOP2w ago
u can go in vc so i can share screen
ero
ero2w ago
No
Protagonist
ProtagonistOP2w ago
nothing is impossible
ero
ero2w ago
Yes. This is impossible.
Protagonist
ProtagonistOP2w ago
Well its happening Lemme see if i can get a vid of it
ero
ero2w ago
It cannot. I'm telling you it cannot happen. A single step over would halt at the end of the method and would not reach any further writelines. It cannot print anything $debug
MODiX
MODiX2w 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.
ero
ero2w ago
ehh It's a bit too much info for just this case, but still a useful read
Protagonist
ProtagonistOP2w ago
look
ero
ero2w ago
Would you please do what I asked? Set a breakpoint on the Clear that's giving you problems. Show that it's hitting the break point. Then step over ONCE, show that you did so, and then show the console. Do not continue the app.
Protagonist
ProtagonistOP2w ago
did i not show u that here @ero
ero
ero2w ago
You showed that it's hitting the breakpoint. You did not show that you stepped over.
Protagonist
ProtagonistOP2w ago
this is the step over.
ero
ero2w ago
I cannot verify that
Protagonist
ProtagonistOP2w ago
Im telling you it is the only line that gets cleared is the please" No need to leave, just say you got proven wrong. 😛 @ero
ero
ero2w ago
You do not want to cooperate. I don't feel like helping.
jcotton42
jcotton422w ago
This is how you lose help.
Protagonist
ProtagonistOP2w ago
no its not. i did as he asked ne he jut quit when he was proved wrong and i show him his "impossible" was possible he gave up i cooperated and you had no soultion I done everything anyones asked here yet he didnt want to believe me like id lie
gerard
gerard2w ago
Sounds like https://github.com/dotnet/runtime/issues/83568 The second comments gives more info why Terminal behaves this way
GitHub
System.Console.Clear() sometimes did not clear the console buffer. ...
Description When running a console application that calls Console.Clear() from a command prompt on Windows Terminal, the console buffer may not be completely cleared. This is different behavior tha...
Protagonist
ProtagonistOP2w ago
Thank you boss <3, it looks like there is no solution though which is strange ah okay i think i might understand now what could be a workaround, i was testing some things out and its like it says its cos the scroll, anything above that is what retains so you either set the consolewindow or display a set amount, then have something like next and before for example
Want results from more Discord servers?
Add your server