Is FreeBASIC good for beginners to progrmaming?

New to FreeBASIC? Post your questions here.
Post Reply
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: Is FreeBASIC good for beginners to progrmaming?

Post by caseih »

Looks good, @joeyxl. Maybe as a next step add logic that prevents you from overdrawing your account.
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Is FreeBASIC good for beginners to progrmaming?

Post by BasicCoder2 »

Looks ok to me. Well done. Now we can tell you how to improve it :)
Last edited by BasicCoder2 on Jun 14, 2021 3:14, edited 3 times in total.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Is FreeBASIC good for beginners to progrmaming?

Post by MrSwiss »

Whenever a single variable is tested for different values only, Select Case i
statement is the better choice, than "endless if's" (or elseif's).
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Is FreeBASIC good for beginners to progrmaming?

Post by deltarho[1859] »

@joeyxl Image

You are hooked. Image
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Is FreeBASIC good for beginners to progrmaming?

Post by BasicCoder2 »

. deleted
Last edited by BasicCoder2 on Jun 14, 2021 6:36, edited 1 time in total.
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Is FreeBASIC good for beginners to progrmaming?

Post by deltarho[1859] »

@BasicCoder2

You are rushing him and imposing your ideas; it may not be what he has in mind.
joeyxl wrote:i actually plan on adding more to it as i learn more.
So let him develop. We can suggest better ways of doing something, but the ideas should come from him.
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Is FreeBASIC good for beginners to progrmaming?

Post by BasicCoder2 »

deltarho[1859] wrote:@BasicCoder2

You are rushing him and imposing your ideas; it may not be what he has in mind.
joeyxl wrote:i actually plan on adding more to it as i learn more.
So let him develop. We can suggest better ways of doing something, but the ideas should come from him.
Ok. I have deleted the suggested methodology. I thought I was suggesting better ways of doing it. It was the kind of feedback I would have appreciated when I was a programming neophyte. He was always free to ignore it but at least it was there should he have chosen to learn from it.

.
joeyxl
Posts: 27
Joined: Jun 08, 2021 13:23
Location: Toronto, Canada

Re: Is FreeBASIC good for beginners to progrmaming?

Post by joeyxl »

BasicCoder2 wrote:
deltarho[1859] wrote:@BasicCoder2

You are rushing him and imposing your ideas; it may not be what he has in mind.
joeyxl wrote:i actually plan on adding more to it as i learn more.
So let him develop. We can suggest better ways of doing something, but the ideas should come from him.
Ok. I have deleted the suggested methodology. I thought I was suggesting better ways of doing it. It was the kind of feedback I would have appreciated when I was a programming neophyte. He was always free to ignore it but at least it was there should he have chosen to learn from it.

.
What did you say? I'm sure it's fine.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Is FreeBASIC good for beginners to progrmaming?

Post by fxm »

I think BasicCoder2 can restore its previous post which offered structure and sample code.
joeyxl
Posts: 27
Joined: Jun 08, 2021 13:23
Location: Toronto, Canada

Re: Is FreeBASIC good for beginners to progrmaming?

Post by joeyxl »

fxm wrote:I think BasicCoder2 can restore its previous post which offered structure and sample code.
I was personally ok with his first comment that he edited. He called me out on my grammar and that's cool, I know i have some bad grammar lol.
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Is FreeBASIC good for beginners to progrmaming?

Post by BasicCoder2 »

Not your grammar just the spelling of withdrawal. I use a spell checker :)

Making use of functions you can break your code into modules which makes it easier to read and modify.
You can define a TYPE to store account data. I have only defined one variable, balance, but more can be added.

Code: Select all

'define a type
type ACCOUNT
    as single balance
end type

dim as ACCOUNT myAccount   'create an ACCOUNT type called myAccount
'initialize myAccount
myAccount.balance = 0

function getChoice() as integer
    dim as integer selection = 0

    Print "(1) balance"
    Print "(2) deposit"
    Print "(3) withdrawal"
    Print "(4) exit"
    Print
    
    while selection = 0
        input "Please make a choice.";selection
        if selection > 4 or selection < 1 then
            locate 11,2
            print " That is an invalid selection. Try again"
        end if
    wend

    return selection
end function

sub printBalance(acc as ACCOUNT)
    Print
    Print "Your Balance is:"; acc.balance
    Print
end sub

sub makeDeposit(acc as ACCOUNT)
    dim as string reply
    dim as single deposit
    print
    Input "Please type in how much you would like to deposit: ", deposit
    Print "OK, you deposited"; deposit
    acc.balance += deposit
    Input "Would you like to see your balance, yes/no ? ", reply
    If reply = "yes" Then
        Print
        Print "Your Balance is:";acc.balance
        Print
    End If
end sub

sub makeWithdrawal(acc as ACCOUNT)
    dim as single withdrawal
    dim as string reply
    print
    Input "Please type in how much you would like to withdraw: ", withdrawal
    if acc.balance - withdrawal < 0 then
        print
        print "Sorry. You have insufficient funds for that amount."
    else
        Print "OK, you withdrew"; withdrawal
        acc.balance -= withdrawal
        Input "Would you like to see your balance yes/no ?", reply
        If reply = "yes" Then
            Print
            Print "Your balance is:" ;acc.balance
            Print
        End If
    end if
    Print
end sub

Print "Hello, and welcome to the atm software"
print

Do

    Select Case getChoice()
    Case 1
        printBalance(myAccount)
    Case 2
        makeDeposit(myAccount)
    Case 3
        makeWithdrawal(myAccount)
    Case 4
        Exit Do
    End Select
   
Loop

Print
Print "Thank you for using our service. goodbye"

Sleep
joeyxl
Posts: 27
Joined: Jun 08, 2021 13:23
Location: Toronto, Canada

Re: Is FreeBASIC good for beginners to progrmaming?

Post by joeyxl »

BasicCoder2 wrote:Not your grammar just the spelling of withdrawal. I use a spell checker :)

Making use of functions you can break your code into modules which makes it easier to read and modify.
You can define a TYPE to store account data. I have only defined one variable, balance, but more can be added.

Code: Select all

'define a type
type ACCOUNT
    as single balance
end type

dim as ACCOUNT myAccount   'create an ACCOUNT type called myAccount
'initialize myAccount
myAccount.balance = 0

function getChoice() as integer
    dim as integer selection = 0

    Print "(1) balance"
    Print "(2) deposit"
    Print "(3) withdrawal"
    Print "(4) exit"
    Print
    
    while selection = 0
        input "Please make a choice.";selection
        if selection > 4 or selection < 1 then
            locate 11,2
            print " That is an invalid selection. Try again"
        end if
    wend

    return selection
end function

sub printBalance(acc as ACCOUNT)
    Print
    Print "Your Balance is:"; acc.balance
    Print
end sub

sub makeDeposit(acc as ACCOUNT)
    dim as string reply
    dim as single deposit
    print
    Input "Please type in how much you would like to deposit: ", deposit
    Print "OK, you deposited"; deposit
    acc.balance += deposit
    Input "Would you like to see your balance, yes/no ? ", reply
    If reply = "yes" Then
        Print
        Print "Your Balance is:";acc.balance
        Print
    End If
end sub

sub makeWithdrawal(acc as ACCOUNT)
    dim as single withdrawal
    dim as string reply
    print
    Input "Please type in how much you would like to withdraw: ", withdrawal
    if acc.balance - withdrawal < 0 then
        print
        print "Sorry. You have insufficient funds for that amount."
    else
        Print "OK, you withdrew"; withdrawal
        acc.balance -= withdrawal
        Input "Would you like to see your balance yes/no ?", reply
        If reply = "yes" Then
            Print
            Print "Your balance is:" ;acc.balance
            Print
        End If
    end if
    Print
end sub

Print "Hello, and welcome to the atm software"
print

Do

    Select Case getChoice()
    Case 1
        printBalance(myAccount)
    Case 2
        makeDeposit(myAccount)
    Case 3
        makeWithdrawal(myAccount)
    Case 4
        Exit Do
    End Select
   
Loop

Print
Print "Thank you for using our service. goodbye"

Sleep
wow, that is so much better than the one i made. using sub routines for each action.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Is FreeBASIC good for beginners to progrmaming?

Post by MrSwiss »

It can even be made more complete.
Attaching Type specific procedures to the type they belong to:

Code: Select all


' ----- define a type -----
type ACCOUNT
    as Double balance
    Declare Sub addACC()    ' deposit
    Declare Sub subAcc()    ' withdrawal
    Declare Sub Show()      ' show balance
End Type

sub ACCOUNT.Show()
    Print
    Print "Your current Balance is: "; This.balance
    Print
end Sub

Sub ACCOUNT.addAcc()
    dim as Double deposit
    
    print
    Input "Please type in how much you would like to deposit: ", deposit
    Print "OK, you deposited "; deposit
    This.balance += deposit
    Print
    Print "Your NEW balance is now: "; This.balance
End sub

Sub ACCOUNT.subAcc()
    dim as Double withdrawal
    
    print
    Input "Please type in how much you would like to withdraw: ", withdrawal
    if This.balance - withdrawal < 0 then
        print
        print "Sorry. You have insufficient funds for that amount."
    else
        Print "OK, you withdrew"; withdrawal
        This.balance -= withdrawal
        Print
        Print "Your NEW balance is now: "; This.balance
    end if
    Print
end Sub
' ----- type end -----

' program specific menu ...
function Menu() as UByte
    dim as integer selection = 0

    Print
    Print "(1) balance"
    Print "(2) deposit"
    Print "(3) withdrawal"
    Print "(4) exit"
    Print
   
    while selection = 0
        input "Please make a choice.",selection
        if selection > 4 OrElse selection < 1 then
            locate 11, 2
            print "That is an invalid selection. Try again"
        end If
        Sleep 10, 1
    wend

    return selection
end Function

' MAIN
dim as ACCOUNT myAccount   'create an ACCOUNT type called myAccount
'initialize myAccount
myAccount.balance = 0

Print "Hello, and welcome to the atm software"
print

Do
    Select Case As Const Menu
        Case 1 : myAccount.Show
        Case 2 : myAccount.addACC
        Case 3 : myAccount.subAcc
        Case 4 : Exit Do
    End Select
    Sleep(50, 1)
Loop

Print
Print "Thank you for using our service. goodbye"

Sleep
' ----- EOF -----
BTW: copying the whole previous post is a waste of forum resources.
(just copy the part that you want to reference your answer to)
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Is FreeBASIC good for beginners to progrmaming?

Post by BasicCoder2 »

@joeyxl

deltarho[1859] has a point when he warned against rushing someone ahead too fast. You will only lose heart and think it is all too hard. Best to move at your own speed with code you can fully understand and could write yourself. Move at your own pace, and take on new methods only when you are able to or want to.
Last edited by BasicCoder2 on Jun 15, 2021 0:58, edited 4 times in total.
joeyxl
Posts: 27
Joined: Jun 08, 2021 13:23
Location: Toronto, Canada

Re: Is FreeBASIC good for beginners to progrmaming?

Post by joeyxl »

BasicCoder2 wrote:@joeyxl

deltarho[1859] has a point when he warned against rushing someone ahead too fast. You will only lose heart and think it is all too hard. Best to move at your own speed with code you can fully understand and could write yourself. Move at your own pace, and take on new methods only when you are able to or want to.
Yes I see that logic. That's why im not copying his version at all. Im just going to do improvements at my own pace how I want it to turn out. It was just nice to see an alternative. I didn't loose heart :)
Post Reply