Weekly Qbasic and Qb64 Lesson Topics
April 19, 2024, 03:41:19 am
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Want to see a specific Tutorial? ASK!
 
  Home Help Search Arcade Links Staff List Login Register  

Calculate Age

Pages: [1]
  Print  
Author Topic: Calculate Age  (Read 5452 times)
none
Guest
« on: April 24, 2011, 12:11:05 am »

    A picture is worth a thousand words. Try this example and do what it says inside the brackets and then examine the outcome.
Code:
DIM name1 AS STRING
DIM num1 AS INTEGER
DIM num2 AS INTEGER

INPUT "What is your name(instead enter numbers here)"; name1$
INPUT "What is the current year(instead enter letters here)"; num1$
INPUT "What year were you born(instead enter letters here)"; num2$

PRINT name$
PRINT num1$
PRINT num2$
Past% = VAL(num2$)
Present% = VAL(num1$)
Age% = Present% - Past%
PRINT
PRINT "hello "; name1$; " your age is "; Age%
SLEEP

    In the old days of QBasic this would bring about an immediate runtime error. Under QB64 there is no runtime error and the program continues. Obviously unintended input can cause a program to error the result as the above program indicates. The solution for the programmer is the CHECK after each entry and redo it if its wrong. Thats why strings are better. There are built in commands that help convertions.
    The best solution is whats called a "Filtered input routine". Which means only the correct recognised symbols (either numbers of letters) are allowed to be entered. Incorrect entry is not allowed into the string and not visually shown back. Hence the user presses other buttons to find out what is allow
« Last Edit: November 23, 2011, 10:51:34 pm by GarrisonRicketson » Report Spam   Logged

Pages: [1]
  Print  
 
Jump to:  

Powered by EzPortal
Bookmark this site! | Upgrade This Forum
SMF For Free - Create your own Forum


Powered by SMF | SMF © 2016, Simple Machines
Privacy Policy