Computer Science Canada

Help required with Error-Proofing

Author:  Da_Big_Ticket [ Mon Oct 31, 2005 2:13 pm ]
Post subject:  Help required with Error-Proofing

In my mortgage calculator there needs to be error proofing. For example, a mortgage needs to be a real variable and if the user enters letters it will mess up. I have some error proofing done now, however, I cant get the program to repeat to get another input for the mortgage variable if theye eneter an invalid input. The bold section of my code is where it is going wrong:

code:


import GUI in "%oot/lib/GUI" % imports the graphic - user - interface for buttons to work

% -------------------------------------------------- Variable Table -------------------------------------------------------
var WinID, pic1, pic2, pic3, pic4, pic5, pic6, font1, year, rows, flag : int
var key : string (1)
var mortgage, customername, phonenum, address, postalcode, answer, interestpercent, multiplier, owingstatement : string
var mortgage2, rateinterest, amountinterest, yearlypay, owing, openingbalance, totalowing, closingbalance, payment : real
% ----------------------------------------------------- End Table ---------------------------------------------------------

% -------------------------------------------------- Picture Values -------------------------------------------------------
pic1 := Pic.FileNew ("g:/bankintro.jpg")
pic2 := Pic.FileNew ("g:/banklayout1.jpg")
pic3 := Pic.FileNew ("g:/banklayout2.jpg") % Assigns values to the picture variables, so a picture can be drawn later
pic4 := Pic.FileNew ("g:/banklayout3.jpg")
pic5 := Pic.FileNew ("g:/banklayout4.jpg")
pic6 := Pic.FileNew ("g:/banklayout5.jpg")
% --------------------------------------------------- End Pictures --------------------------------------------------------

% --------------------------------------------------- Font Values ---------------------------------------------------------
font1 := Font.New ("Franklin Gothic Medium:14")
% ---------------------------------------------------- End Fonts ----------------------------------------------------------
flag := 0

% ---------------------------------------------- Window Close Procedure ---------------------------------------------------
procedure finish
    Window.Close (WinID) % Closes the window opened at the beginning of the program, done by the button
end finish
% ------------------------------------------------- End Window Close ------------------------------------------------------

% ----------------------------------------------- Chartline Procedure -----------------------------------------------------
procedure chartlines
    drawline (10, 420, 790, 420, black)
    drawline (10, 420, 10, 30, black)
    drawline (790, 420, 790, 30, black)
    drawline (10, 30, 790, 30, black)
    drawline (120, 420, 120, 30, black)
    drawline (280, 420, 280, 30, black)
    drawline (430, 420, 430, 30, black)
    drawline (550, 420, 550, 30, black) % Draws all the lines for the chart to be displayed later
    drawline (670, 420, 670, 30, black)
    drawline (10, 385, 790, 385, black)
    drawline (10, 350, 790, 350, black)
    drawline (10, 315, 790, 315, black)
    drawline (10, 280, 790, 280, black)
    drawline (10, 245, 790, 245, black)
    drawline (10, 210, 790, 210, black)
    drawline (10, 175, 790, 175, black)
    drawline (10, 140, 790, 140, black)
    drawline (10, 105, 790, 105, black)
    drawline (10, 70, 790, 70, black)
end chartlines
% -------------------------------------------------- End Chatlines ---------------------------------------------------------

procedure complete
    rateinterest := 0
    WinID := Window.Open ("position:middle;middle, graphics:800;600, title: Big Ticket Bank")
    View.Set ("nocursor")
    Pic.Draw (pic1, 0, 0, picCopy)
    getch (key)
    View.Set ("cursor")
    loop
        cls
        Pic.Draw (pic2, 0, 0, picCopy)
        locate (23, 32)
        get customername : *
        locate (27, 32)
        get phonenum : *
        locate (31, 32)
        get address : *
        locate (36, 32)
        get postalcode : *
        cls
        Pic.Draw (pic3, 0, 0, picCopy)
        Font.Draw (customername, 230, 155, font1, white)
        Font.Draw (phonenum, 230, 110, font1, white)
        Font.Draw (address, 230, 70, font1, white)
        Font.Draw (postalcode, 230, 25, font1, white)
        locate (26, 50)
        get answer
        exit when answer = "y" or answer = "yes" or answer = "Y" or answer = "Yes" or answer = "YES"
    end loop
cls
Pic.Draw (pic4, 0, 0, picCopy)
    [b]loop
        locate (25, 73)
        get mortgage
        for a : 1 .. length (mortgage)
            if index ("0123456789", mortgage (a)) = 0 then
                put "Sorry that is an invalid input."
                flag := 1
                exit
            end if
        end for
        if flag = 0 then
            mortgage2 := strreal (mortgage)
        end if
    end loop
    if flag = 0 then[/b]
        if mortgage2 >= 0 and mortgage2 <= 100000 then
            rateinterest := 3.5
        else
            if mortgage2 >= 100001 and mortgage2 <= 200000 then
                rateinterest := 5.5
            else
                if mortgage2 >= 200001 and mortgage2 <= 300000 then
                    rateinterest := 6.5
                else
                    if mortgage2 >= 300001 then
                        rateinterest := 7.5
                    else
                        put "That is an impossible mortgage."
                    end if
                end if
            end if
        end if
        interestpercent := realstr (rateinterest, 5)
        interestpercent += "%"
        Font.Draw (interestpercent, 575, 110, font1, black)
        View.Set ("nocursor")
        locate (35, 75)
        getch (key)
        amountinterest := (rateinterest / 100) * mortgage2
        loop
            cls
            Pic.Draw (pic5, 0, 0, picCopy)
            multiplier := "$"
            multiplier += realstr (amountinterest, 5)
            Font.Draw (multiplier, 370, 210, font1, black)
            owingstatement := "THIS MEANS YOU MUST PAY A MINIMUM OF :$"
            owing := amountinterest + 1000
            owingstatement += realstr (owing, 2)
            Font.Draw (owingstatement, 190, 130, font1, white)
            View.Set ("cursor")
            locate (32, 50)
            get yearlypay
            const amountyears := mortgage2 / yearlypay
            if amountyears > 30 then
                Font.Draw ("That payment plan is not possible, it takes more than 30 years.", 100, 100, font1, black)
                Font.Draw ("Please enter a payment plan greater, which will not take 30 years.", 80, 100, font1, black)
            elsif yearlypay < owing then
                Font.Draw ("That payment plan is not possible, it does not meet the minimum payment.", 80, 60, font1, black)
                Font.Draw ("Please re-enter an appropriate yearly payment.", 200, 40, font1, black)
            else
                Font.Draw ("It appears your mortgage will work. Your breakdown will appear on the following screen.", 50, 60, font1, black)
            end if
            View.Set ("nocursor")
            getch (key)
            exit when amountyears <= 30 and yearlypay >= owing
        end loop
        cls
        Pic.Draw (pic6, 0, 0, picCopy)
        year := 0
        rows := 8
        openingbalance := mortgage2
        chartlines
        loop
            year := year + 1
            totalowing := openingbalance + amountinterest
            amountinterest := openingbalance * rateinterest
            closingbalance := totalowing - yearlypay

            if yearlypay > totalowing then
                yearlypay := totalowing
                closingbalance := totalowing - yearlypay
            end if
            if closingbalance <= 0 then
                closingbalance := 0
            end if
            put year : 3, openingbalance : 10 : 2, amountinterest : 10 : 2, totalowing : 10 : 2, yearlypay : 10 : 2, closingbalance : 10 : 2
            exit when closingbalance <= 0
            rows := rows + 1
            if rows = 10 then
                getch (key)
                cls
            end if
            openingbalance := closingbalance
        end loop
        var repeatbutton : int := GUI.CreateButton (17, 252, 120, "Repeat Program", complete)
        var quitButton := GUI.CreateButton (17, 30, 120, "Quit", finish)
        loop
            exit when GUI.ProcessEvent
        end loop
    end if
end complete

complete


Thanks for your help


: