Solved

Creating an error message if string entered instead of integer

Posted on 2003-10-27
9
253 Views
Last Modified: 2010-04-16
What I am trying to achieve is a simple error message to print on the screen if the user types a string instead of an integer something along the lines of ('Please type an integer!') however this isn't as simple as i first thought.  Here is what I have so far: -

program Task_8;

{$APPTYPE CONSOLE}

uses
  SysUtils;
var
  s:string;
  x,y:integer;
  total:integer;
  j:integer;
  oksofar:boolean;
  thischaracter:char;
  thisord:integer;
begin
        oksofar:=true;
  j:=1;
  begin
  Writeln('Please enter the first number: ');
    thischaracter:=s[j];
    thisord:=ord(thischaracter);
    if ((thisord<48) or (thisord>57)) then begin
      oksofar:=false;
    end; //end if
    if (oksofar = false) then begin
    write(strToInt(s));
    writeln('Please enter an integer!');
  end else begin
        Write('Please enter the first number: ');
        readln(x);
        write('Please enter the second number: ');
        writeln;
        write('Please enter the second number: ');
        readln(y);
        writeln;
        if (y < x) then  begin
              total:= x * y;
              writeln('x times y = ',total);
      end else begin
              total:= x - y;
              writeln('x minus y = ',total);
    end;
end;
end;
end.

Any help would be much appreciated
0
Comment
Question by:Jenko_2003
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 17

Assisted Solution

by:Wim ten Brink
Wim ten Brink earned 20 total points
ID: 9627754
Oh. brother... What's wrong with the Val function???

var
  s:string;
  X, Code: Integer;
begin
   ReadLn(s);
  Val(S, X, Code);
  if (Code=0) then begin
    // X is a valid number
  end
  else begin
    // Code contains the index of the invalid character so something like:
    WriteLn(S);
    WriteLn('':Code-1, '^ Invalid character');
    // Should do the trick.
  end;

Please don't re-invent the wheel again. We already have too many of them already. (And the Val function exists almost as long as the Pascal language itself...)
0
 

Author Comment

by:Jenko_2003
ID: 9628914
Thankyou for your help, the reason I havn't used the val function is because im newly studying the language and we have not covered val function yet but we have covered the ord function.
0
 
LVL 1

Accepted Solution

by:
JackNaif earned 20 total points
ID: 9637356
Jenko,
this looks very much like homework, so we're not allowed to solve for you, but we can give you some orientation. I can guide you a little bit. But you must do the heavy work. =)

In the first place, you must pick one of the two different ways of treating the input that I can see in your code:

Option 1: get a string and process the characters one by one with stuff like s[i] and the like. (In your code, you do this BEFORE assigning any value to s. That won't work.)

Option 2: get one char at a time with multiple readln() invocations. You've also got a draft of this in your code but it still needs some work.

Both options are good (for a beginer's homework) but in a professional programme, option 2 is completely unacceptable.

Once you've decided that, it's simple:

1) get the input
2) check for non numbers in the input (or make sure all the chars in the input are numbers, whichever way you like it).
3) unless input is OK, go back to one (note that this will mean that the begining of a loop is before the code that represents 1: a "repeat" or some initialisation and a "while")
4) by the time you get here, you've got an input that's OK: now you can do the "ord" thing (beginers only) or read the help for "val", as Workshop_Alex suggests.

Now try to get that into Pascal, and if you find problems doing it, ask again (with the new code, which will look *very* different. =)

Regards:

Jack


0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Expert Comment

by:JackNaif
ID: 9637371
Oops! One more detail:

You print the error message on step 3), only if the input is not OK and right before moving back to 1)

Jack
0
 

Author Comment

by:Jenko_2003
ID: 9638048
Thankyou Jack, it isn't homework, it's work which we can do which will help us learn more Pascal, I actually appreciate you telling me how to do it rather than just pasting the code, because as i said it isn't being marked it's just to help me.  So thankyou very much for taking the time out to explain things as it helps a great deal.
0
 
LVL 1

Expert Comment

by:JackNaif
ID: 9643713
No problem, Jenko.

As I said: just try to code it, and if you get stuck at any point, come back here for more help. =)
0
 
LVL 1

Expert Comment

by:JackNaif
ID: 9657692
Well????

How did it go???

Could you make it work??

Still stuck??
0
 

Author Comment

by:Jenko_2003
ID: 9662593
My bad, yeah I perserveered (sp?) and I got there in the end.  Im not at home at the minute so I can't paste my final code.  I decided to use the val function in the end as the ord function was lots more code than the val function.  Thanks for your help, I appreciate it.  How can I accept two comments?
0
 
LVL 1

Expert Comment

by:JackNaif
ID: 9670489
You've got to split points.
Check this URL:

<http://www.experts-exchange.com/help/closing.jsp#3>
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains the fundamentals of industrial networking which ultimately is the backbone network which is providing communications for process devices like robots and other not so interesting stuff.
First of all let me say that the only language that I speak is English, but in answering questions here I often come across people whose English skills are not the best and I’d like to be able to communicate better with them, and the following descr…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

615 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