Bee Learnt Logo
Bee Learnt
FeatureBrowse allPricingAboutSupport
Bee Learnt

Bee Learnt gives South African Grade 10–12 learners a cleaner route from classwork to NSC readiness — with parents and tutors supporting the journey.

WhatsApp: 0685537312

Support email: admin@beelearnt.co.za

Learn
FeaturesBrowse subjectsPricing
Get started
Create accountSign inContact support
Company
About Bee LearntAdvertisingContact
Legal
Privacy policyTerms of serviceCookie policyAd choices

© 2026 Bee Learnt. All rights reserved.

CAPS aligned · Grades 10–12 · NSC ready

Privacy choices

Allow analytics and advertising services to help us understand site usage and measure campaigns.

Privacy Policy
  1. Subjects
  2. /
  3. Information Technology
  4. /
  5. Information Technology Grade 12 — Practical
  6. /
  7. Programming Fundamentals
  8. /
  9. Reliable input, types and validation
Grade 12 · Practical

Reliable input, types and validation

Programming Fundamentals · about 8 min

2 MIN READ
1 study sections · pause after each one and recall the key idea

Start a solution by separating input, processing and output. A TEdit supplies text, even when the user types digits. Convert that text only after checking it. Integer suits whole counts; Double suits measurements; Currency is useful for fixed-point monetary calculations. Boolean represents a condition rather than the strings “true” and “false”. A variable must be assigned before it is read. A meaningful name such as iQuantity makes a trace easier to follow.

TryStrToInt reports whether conversion succeeded without raising a conversion exception for ordinary invalid input. Range checking is a separate step: a successfully converted negative quantity can still be invalid. Keep these checks in separate statements so the code does not depend on Delphi Boolean evaluation settings. Format a number when displaying it; do not convert it to a formatted string halfway through arithmetic.

Key points
  • Choose a suitable type for each value.
  • Validate input before calculating.
  • Distinguish conversion from formatting.

Worked example: procedure TForm1.btnCalculateClick(Sender: TObject); var iQuantity: Integer; cTotal: Currency; begin if not TryStrToInt(edtQuantity.Text, iQuantity) then begin ShowMessage('Enter a whole number.'); Exit; end; if (iQuantity < 1) or (iQuantity > 100) then begin ShowMessage('Quantity must be 1 to 100.'); Exit; end; cTotal := iQuantity * 12.50; lblTotal.Caption := FormatFloat('0.00', cTotal); end; With input 3, conversion succeeds, the range check passes and the output is 37.50. With abc, the procedure exits before using iQuantity. With 0, conversion succeeds but the business rule rejects the value. The example assumes the named controls exist on TForm1 and SysUtils is available.

COMMON MISTAKE

StrToInt alone does not validate a range. Formatting to two decimals changes presentation, not the validation rule.

Practice: Change the rule to allow quantities 0–50. Explain which of blank, -1, 0, 50 and 51 should pass.

Answer: Reject blank during conversion. Replace the range test with (iQuantity < 0) or (iQuantity > 50). Only 0 and 50 pass. The endpoints are included, so test both explicitly.

Recap: Convert, validate, calculate, then format. Invalid input must never reach the calculation.


Next

Problem solving and IPO