Solved

avoiding integer run time errors

Posted on 2000-03-03
11
166 Views
Last Modified: 2010-04-16
HOW DO I STOP THE PROGRAM FROM FAILING IF A NON-NUMERICAL CHARACTER IS ENTERED ie NOT AN INTEGER ?????

 

VAR

CHOICE INTEGER

{MAIN PROGRAM BODY}


begin
        choice:= 10;
        while choice <> 9 do
        begin
        clrscr;
        showmenu;
        readln(choice);
        case choice of
        1: CREATEPERMANENTFILE;
        2: VIEW;
        3: entercostumedetails;
        4: ENTERCUSTOMERDETAILS;
        5: ENTERCUSTOMERORDER;
        6: SHOWALLCUSTOMERORDERS;
        7: SEARCHDATEORDER;
        8: SEARCHCNUMORDER;
        end;
        end;

I'm not allowed to change the choice to a char !!!!!!
0
Comment
Question by:elniniokev
11 Comments
 
LVL 12

Expert Comment

by:Hypo
ID: 2582160
you can use the command val...

Procedure Val(S: string; var V,Code : integer);

S is a string wich you want to convert to an integer, V is the target variable, and Code contains the possible error code!

Ok, so you might wonder why you want to use this function? I'll tell you why.

When you use readln(choice) and choice is an integer... the program will halt with an error code if you don't input a valid integer string.

But if you use a string instead of an integer in the readln procedure... You can enter whatever you like without have to worry about error messages halting the program! Also, the Val procedure converts a string into an integer variable and leaving an error code in a variable if the string is not a valid integer string.

So... to replace this process  [readln(choice)], you need 2 more variables except choice. One called St, of type string (St : string;) and one called Code, of type integer (Code : integer;)
St will contain the string you get from the Readln, Code will contain the error code (if any), and Choice will contain... the users choice!!!

This is how you could use it.

readln(St);
val(St,Choice,Code);
If Code <> 0 then ... {Do something to handle the error!}

And this is what your code would look like!

VAR

CHOICE,
CODE : INTEGER;
ST : STRING;

{MAIN PROGRAM BODY}

begin
        choice:= 10;
        while choice <> 9 do
        begin
        clrscr;
        showmenu;
        readln(St);
        Val(St,Choice,Code);
        If Code <> 0 then Choice := 10;
        case choice of
        1: CREATEPERMANENTFILE;
        2: VIEW;
        3: entercostumedetails;
        4: ENTERCUSTOMERDETAILS;
        5: ENTERCUSTOMERORDER;
        6: SHOWALLCUSTOMERORDERS;
        7: SEARCHDATEORDER;
        8: SEARCHCNUMORDER;
        end;
        end;

That should work just fine... If you don't enter a valid integer string... the program does nothing!

Hope it works!
0
 
LVL 1

Author Comment

by:elniniokev
ID: 2583443
the code didn't accept the choic e 1..9
an error was displyed
0
 
LVL 3

Expert Comment

by:Alisher_N
ID: 2583516
run-time error ?
what exactly ?
hypo's code must work fine
0
 
LVL 1

Author Comment

by:elniniokev
ID: 2584032
the program doesn't accept the choices between 1 & 9, the error message I added is displayed no matter what I enter. I'm new to programming it could be something with the placement of the code any more help would be greatly appreciated.
0
 
LVL 1

Author Comment

by:elniniokev
ID: 2584037
The Readln(st) is in the procedure showmenu as well as the main body code, I didn't understand why I have to have procedure val and what else it should contain.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:elniniokev
ID: 2584039
Adjusted points to 40
0
 
LVL 12

Accepted Solution

by:
Hypo earned 40 total points
ID: 2584147
You have to use the procedure val since it returns an errorcode in the Code parameter instead of halting the entire program as readln does if there's an error in the input.

If you don't enter an integer in val(st,choice,code) the "code" variable  will contain a non zero value... If you don't enter an integer in readln(Choice) the program will halt with a runtime error. You didn't want that to happen... that's why you should use procedure val!
0
 
LVL 3

Expert Comment

by:sumant032199
ID: 2584420
{
   This is good working example of how you can eliminate error
   occuring due to input of invalid number.
   This function is verymuch similar to sscanf() in C.

}

program validint;
uses crt;

var str : string[10];
    int : integer;

function give_int(s : string) : integer;
var n,i : integer;
begin
    i:=1;
    n:=0;
    if not (s[i] in ['0'..'9','-']) then n := -1
    else
    begin
        if s[i] = '-' then i:=2;
        while s[i] in ['0'..'9'] do
        begin
              n := (n * 10) + ord(s[i]) - 48;
              i := i + 1;
        end;
    end;
    if s[1] = '-' then give_int := -n
    else give_int := n;
end;

begin
     clrscr;
     write('Enter a number: ');
     readln(str);

     str[length(str)+1] := #0;
     int := give_int(str);

     write('The number is: ',int);
end.
0
 
LVL 1

Author Comment

by:elniniokev
ID: 2585030
Hypo,  please re-submit your answer I got your code working and I want to give you the points you deserve. Thanks a million!!!!!
0
 
LVL 12

Expert Comment

by:Hypo
ID: 2585638
I can't answer this quizz as long as Sumant has proposed an answer to it. But I think that you can accept one of my comments as the answer? If you can't do that you have to reject Sumants answer first before I can propose an answer to it.

Anyway, I'm glad you got it to work.

regards Hypo.
0
 
LVL 1

Author Comment

by:elniniokev
ID: 2585775
Hypo's code is the one I'm using, I was wrong to reject it initialy. Sumant's answer is very good and shouldn't regard this as a rejection. Hypo's code was correct initially, it was me who stuffed up in the placement of the code he supplied.

Thanks to both !

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
TEdit.SetFocus trouble 3 171
In Delphi VCL, how can you exit without completing all actions? 4 234
Inno Setup -> Add App Pin to Task Bar 1 604
indy tidtcpclient threading issue 8 63
Scenario: Your operations manager has discovered an anomaly in your security system. The business will start to suffer within 15 minutes if it is a major IT incident. What should she do? We have 6 recommendations for managing major incidents (https:…
A customer recently asked me about anti-malware and the different deployment options available for his business. Daily news about cyberattacks, zero-day vulnerabilities, and companies that suffered a security breach made him wonder if the endpoint a…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now