coding-horror/basic-computer-games · error · ArgumentException
{DisplayText} must be greater than zero
Error message
{DisplayText} must be greater than zero What it means
Thrown as ArgumentException by GetNumber() in the Banner program when the parsed integer is <= 0. Int32.TryParse writes 0 into TempInt on failure (non-numeric or null input), so both genuinely non-positive input and unparseable garbage trigger it. GetNumber feeds Horizontal/Vertical scaling factors for the banner print, which must be positive.
Source
Thrown at 06_Banner/csharp/banner.cs:83
};
/// <summary>
/// This displays the provided text on the screen and then waits for the user
/// to enter a integer value greater than 0.
/// </summary>
/// <param name="DisplayText">Text to display on the screen asking for the input</param>
/// <returns>The integer value entered by the user</returns>
private int GetNumber(string DisplayText)
{
Console.Write(DisplayText);
string TempStr = Console.ReadLine();
Int32.TryParse(TempStr, out int TempInt);
if (TempInt <= 0)
{
throw new ArgumentException($"{DisplayText} must be greater than zero");
}
return TempInt;
}
/// <summary>
/// This displays the provided text on the screen and then waits for the user
/// to enter a Y or N. It cheats by just looking for a 'y' and returning that
/// as true. Anything else that the user enters is returned as false.
/// </summary>
/// <param name="DisplayText">Text to display on the screen asking for the input</param>
/// <returns>Returns true or false</returns>
private bool GetBool(string DisplayText)
{
Console.Write(DisplayText);
return (Console.ReadLine().StartsWith("y", StringComparison.InvariantCultureIgnoreCase));
}
View on GitHub (pinned to 5301155192)
Solutions
- Enter a positive integer (1 or greater) at the Horizontal/Vertical prompts.
- If automating, pipe a positive integer into stdin for those prompts.
- Make GetNumber loop and re-prompt on <= 0 instead of throwing, for a friendlier UX.
Example fix
// before
Int32.TryParse(TempStr, out int TempInt);
if (TempInt <= 0) {
throw new ArgumentException($"{DisplayText} must be greater than zero");
}
return TempInt;
// after: re-prompt instead of throwing
int TempInt;
while (!Int32.TryParse(Console.ReadLine(), out TempInt) || TempInt <= 0) {
Console.Write($"{DisplayText} must be a positive integer. Try again: ");
}
return TempInt; Defensive patterns
Strategy: validation
Validate before calling
// C#: validate before relying on TryParse result
int GetNumber(string prompt) {
int value;
while (!Int32.TryParse(Console.ReadLine(), out value) || value <= 0) {
Console.Write($"{prompt} must be a positive integer: ");
}
return value;
} Try / catch
// In GetInput(), catch and re-prompt instead of letting it propagate
try { Horizontal = GetNumber("Horizontal "); }
catch (ArgumentException) { /* re-ask or default */ } Prevention
- Never assume TryParse succeeded; check its boolean return.
- Loop on invalid input rather than throwing for user-entry errors.
- When automating, feed positive integers for every GetNumber call.
When it happens
Trigger: User pressing Enter without typing (null/empty -> TryParse fails -> TempInt=0); typing 0, a negative number, or letters like 'abc' at the 'Horizontal ' or 'Vertical ' prompts in GetInput() (banner.cs:119-120).
Common situations: Empty input at the dimension prompt; a user typing a negative dimension; pasting non-numeric text; running the program with redirected empty stdin.
AI-assisted analysis of coding-horror/basic-computer-games@5301155192 (2026-08-13).
Data as JSON: /api/errors/bac2707a6523a50a.
Report an issue: GitHub.