Weekly Qbasic and Qb64 Lesson Topics
March 19, 2024, 05:08:33 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 5231 times)
GarrisonRicketson
Admin
Administrator
Hero Member
*****
Posts: 583



WWW
« on: April 23, 2011, 12:25:52 pm »

This is a simple bit of code, but I would like to add a line that calculates the age,and then prints "your age is (age)
Code:
DIM name1 AS STRING
DIM num1 AS INTEGER
DIM num2 AS INTEGER


INPUT "What is your name"; name1$
INPUT "What is the current year"; num1$
INPUT "What year were you born"; num2$


PRINT name$
PRINT num1$
PRINT num2$
'PRINT num1$ - num2$ 'but this dose not work


SLEEP
 
Thanks from Garry
Report Spam   Logged

Share on Facebook Share on Twitter

GarrisonRicketson
Admin
Administrator
Hero Member
*****
Posts: 583



WWW
« Reply #1 on: April 23, 2011, 11:23:23 pm »

Ok,  VAL is a new one to me,but thats what it was saying to, cannot convert a string, something like that,... I think this could be used to calculate, pesos / dollars, or other moneys,...and other things too....
thanks from
Garry
Report Spam   Logged

none
Guest
« Reply #2 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
GarrisonRicketson
Admin
Administrator
Hero Member
*****
Posts: 583



WWW
« Reply #3 on: April 24, 2011, 12:07:03 pm »

Ok yea, I see what you mean,....It gave me a 0,....
from Garry
Report Spam   Logged

bobostno
Newbie
*
Posts: 2


« Reply #4 on: April 28, 2011, 10:20:17 am »

This is my attempt at it, although my error checking is rather outdated.

Code:
thedate$ = DATE$
year% = VAL(RIGHT$(thedate$, 4))

10 INPUT "Please input birth year. eg. 1980"; birthyear%
IF birthyear% < 1000 OR birthyear% > year% THEN
    PRINT "Your Real birth year please!"
    GOTO 10
END IF

age% = year% - birthyear%
PRINT "You are"; age%; "Years old!"

SLEEP
Report Spam   Logged
GarrisonRicketson
Admin
Administrator
Hero Member
*****
Posts: 583



WWW
« Reply #5 on: April 29, 2011, 07:33:38 am »

Thant is pretty neat,...all you do is enter the year you were born,...
from Garry
Report Spam   Logged

ChatKing
Post Demos
*
Posts: 23


« Reply #6 on: November 21, 2011, 02:36:37 pm »

simply code for that program is following:-


DIM name1 AS STRING
DIM num1 AS INTEGER
DIM num2 AS INTEGER


INPUT "What is your name"; name1
INPUT "What is the current year"; num1
INPUT "What year were you born"; num2


PRINT name1
PRINT num1
PRINT num2
PRINT num1 - num2 'but this dose not work


SLEEP
Report Spam   Logged
ChatKing
Post Demos
*
Posts: 23


« Reply #7 on: November 21, 2011, 03:34:04 pm »

I have a complete code of Calculate Age but there is still a bug, can anybody find out and solve it and tell me. Thanks


Code:
DIM YourName AS STRING
DIM SHARED year1 AS INTEGER
DIM SHARED year2 AS INTEGER
DIM SHARED year3 AS INTEGER
DIM SHARED month1 AS INTEGER
DIM SHARED month2 AS INTEGER
DIM SHARED month3 AS INTEGER
DIM SHARED day1 AS INTEGER
DIM SHARED day2 AS INTEGER
DIM SHARED day3 AS INTEGER

CLS
INPUT "Your Good name, please : ", YourName
'assigning current dates to the varialbles
year1 = VAL(RIGHT$(DATE$, 4))
month1 = VAL(LEFT$(DATE$, 2))
day1 = VAL(MID$(DATE$, 4, 2))

Start

IF (day2 > day1) AND ((month1 = 4) OR (month1 = 6) OR (month1 = 9) OR (month1 = 11)) THEN
    day1 = day1 + 30
    month1 = month1 - 1
END IF

IF (day2 > day1) AND ((month1 = 1) OR (month1 = 3) OR (month1 = 5) OR (month1 = 7) OR (month1 = 8) OR (month1 = 10) OR (month = 12)) THEN
    day1 = day1 + 31
    month1 = month1 - 1
END IF

IF (day2 > day1) AND (month1 = 2) AND (year1 MOD 4 <> 0) THEN
    day1 = day1 + 28
    month1 = month1 - 1
END IF

IF (day2 > day1) AND (month1 = 2) AND (year1 MOD 4 = 0) THEN
    day1 = day1 + 29
    month1 = month1 - 1
END IF


IF (month2 > month1) THEN
    month1 = month1 + 12
    year1 = year1 - 1
END IF

'Calcualtion of year
day3 = day1 - day2
month3 = month1 - month2
year3 = year1 - year2


CLS
PRINT
PRINT YourName; " you are "; day3; " days "; month3; " months  and "; year3; " years old."





SUB Wrong
CLS
PRINT "Wrong Entry of your date of birth."
PRINT
PRINT "It should be valid year like 1998"
PRINT
PRINT "Valid month like 9"
PRINT
PRINT "Valid Date like 12"
PRINT
PRINT "or your computer date is not updated."
PRINT
INPUT "Press any key ............"; dummy$
Start
END SUB

SUB Start
year2 = 0
month2 = 0
day2 = 0

CLS
INPUT "Enter Year of Birth [eg 1999] : "; year2
IF (year2 < 1000) OR (year2 > year1) THEN Wrong
INPUT "Enter Month of Birth [eg 11]  : "; month2
IF (month2 < 1) OR (month2 > 12) THEN Wrong
INPUT "Enter Day of Birth [eg 23]    : "; day2
IF (day2 < 1) OR (day2 > 31) THEN Wrong
IF (day2 > 29) AND (month2 = 2) THEN Wrong
IF (day2 > 28) AND (month2 = 2) AND (year2 MOD 4 <> 0) THEN Wrong
END SUB
Report Spam   Logged
GarrisonRicketson
Admin
Administrator
Hero Member
*****
Posts: 583



WWW
« Reply #8 on: November 21, 2011, 03:58:58 pm »

Thanks for posting this, I have not tried it yet,..I am right in the middle of something,..but also I am glad to see,  I recieve the notices,when you post fine, this will help me respond sooner,..
from Garry
 Edited: I tried it now, but it seems to be pretty good to me, I did notice one time it asked for the dates twice,...Is, that what you ment by "bug" ?
« Last Edit: November 21, 2011, 04:31:59 pm by GarrisonRicketson » Report Spam   Logged

ChatKing
Post Demos
*
Posts: 23


« Reply #9 on: November 22, 2011, 10:53:14 am »

Hi to All

Yes GarrisonRicketson, the bug is when you enter something wrong it repeats for the input that is the bug, can anybody tell me the solution.
Report Spam   Logged
GarrisonRicketson
Admin
Administrator
Hero Member
*****
Posts: 583



WWW
« Reply #10 on: November 23, 2011, 10:49:42 pm »

Hope fully maybe someone can, but I don't know,
from Garry
P.S. If you want I could post the code on another forum, and probley get a answer ?,.
Report Spam   Logged

GarrisonRicketson
Admin
Administrator
Hero Member
*****
Posts: 583



WWW
« Reply #11 on: November 25, 2011, 07:48:51 am »

Well I did post a link to this thread, on qb64.net, and this is what I got,
posted by DeeBee
at qb64.net

The wrong sub should not call start.
It should return to start, and after it calls wrong, there should
be an evil GOTO to re-ask the value.

Also the leap year formula is incorrect, but it will probably work for what they are doing.
http://en.wikipedia.org/wiki/Leap_year
-----------------------------------
Garry (me) added,
 I notice he says "evil" goto,.. some people are against useing goto,..but there also are times when it is useful,or even neccessary,...
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