C
C#2mo ago
Protagonist

Console Doesn't Clear and Provides nested outputs

Like the title says and i dont get it
No description
68 Replies
Protagonist
ProtagonistOP2mo 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
ProtagonistOP2mo 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
Matt2mo ago
I get that issue all the time with random apps (e.g npm i, wget, ...)
Protagonist
ProtagonistOP2mo ago
so how does one resolve it
Matt
Matt2mo ago
sadly never quite figured out how it happens or how to solve it :/
Protagonist
ProtagonistOP2mo ago
oof sad
Matt
Matt2mo ago
it only happens on Windows too, never had it happen in Linux or MacOS
Protagonist
ProtagonistOP2mo 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
WhiteBlackGoose2mo ago
Not all terminals support the clear command What terminal are you using? and what to do is: try another one
Protagonist
ProtagonistOP2mo 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
WhiteBlackGoose2mo ago
oh
Protagonist
ProtagonistOP2mo ago
or that part in the method rather
WhiteBlackGoose
WhiteBlackGoose2mo ago
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
ProtagonistOP2mo ago
I've tried n tried for past hour or so
WhiteBlackGoose
WhiteBlackGoose2mo ago
and? what have you come down to so far
Protagonist
ProtagonistOP2mo ago
that nothing I do make its clear even when its logically supposed to clear it doesnt
WhiteBlackGoose
WhiteBlackGoose2mo ago
you said your other methods work
Protagonist
ProtagonistOP2mo ago
yes this specific section of my code doesnt want to clear
WhiteBlackGoose
WhiteBlackGoose2mo ago
then you need to find the difference between this method and others what prevents it from clearing
Protagonist
ProtagonistOP2mo ago
if it was that easy i would of figured it out
WhiteBlackGoose
WhiteBlackGoose2mo ago
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
ProtagonistOP2mo 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
WhiteBlackGoose2mo ago
if you've already performed this procedure you can tell what prevents it from clearing
Protagonist
ProtagonistOP2mo ago
becuase it displays info the read key should be there its literally not possible xD
WhiteBlackGoose
WhiteBlackGoose2mo ago
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
ProtagonistOP2mo ago
The issue is, I know where the issue is happening its just a matter of why I've debugged too btw
WhiteBlackGoose
WhiteBlackGoose2mo ago
and where does it happen? Which exact line in your code prevents Console.Clear from working properly?
Protagonist
ProtagonistOP2mo ago
after the code is finished it seems to stay persistant even if i include a console clear at the end
No description
Protagonist
ProtagonistOP2mo ago
so when leaving the method i provided the console doesnt clear for some reason
WhiteBlackGoose
WhiteBlackGoose2mo ago
You didn't answer the question
Protagonist
ProtagonistOP2mo 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
ProtagonistOP2mo ago
it doesnt recognise these as lines or something
No description
Protagonist
ProtagonistOP2mo ago
does no one know how to fix thi s @jcotton42 Mr pro ❤️
jcotton42
jcotton422mo ago
?
Protagonist
ProtagonistOP2mo 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
jcotton422mo ago
Well, it seems someone was already helping, so.
Protagonist
ProtagonistOP2mo ago
they dissapeared and everyone here who tried to help cant figure it out, so.
ero
ero2mo 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
ProtagonistOP2mo ago
this is before i step over
No description
Protagonist
ProtagonistOP2mo ago
this is after
No description
Protagonist
ProtagonistOP2mo ago
makes no sense at all what its printing
ero
ero2mo ago
What do you mean what it's printing doesn't make sense? Did you do a single step over like I asked?
Protagonist
ProtagonistOP2mo 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
ero2mo ago
Doing a single step over should not be able to print anything You must have done it incorrectly
Protagonist
ProtagonistOP2mo ago
exactly yet it still displays
ero
ero2mo ago
It cannot It is impossible
Protagonist
ProtagonistOP2mo ago
u can go in vc so i can share screen
ero
ero2mo ago
No
Protagonist
ProtagonistOP2mo ago
nothing is impossible
ero
ero2mo ago
Yes. This is impossible.
Protagonist
ProtagonistOP2mo ago
Well its happening Lemme see if i can get a vid of it
ero
ero2mo 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
MODiX2mo 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
ero2mo ago
ehh It's a bit too much info for just this case, but still a useful read
Protagonist
ProtagonistOP2mo ago
look
ero
ero2mo 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
ProtagonistOP2mo ago
did i not show u that here @ero
ero
ero2mo ago
You showed that it's hitting the breakpoint. You did not show that you stepped over.
Protagonist
ProtagonistOP2mo ago
this is the step over.
ero
ero2mo ago
I cannot verify that
Protagonist
ProtagonistOP2mo 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
ero2mo ago
You do not want to cooperate. I don't feel like helping.
jcotton42
jcotton422mo ago
This is how you lose help.
Protagonist
ProtagonistOP2mo 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
gerard2mo 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
ProtagonistOP2mo 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