Hi


If I am asking the name and identification number like

[TEX] write(' Introduce your name: ');
readln(name);
write(' Introduce your identification number: ');
readln(ident);[/TEX]

being

name:string
ident:integeer

and a dull user comes and introduces as a name a number (or a string as identification number) the program stops. How can I solve it?


Related with that. Sometimes I ask for a number and I have as variable a char, so that the program is more robust and doesn't stop if the user introduces the letter 'a'. But, other times, it can't be a char because I need it to indicate the position of an array. The problem is the same. If the user introduces any letter, the program stops.

Recommended Answers

All 3 Replies

Please learn to use code tags. I've already given you a number of examples.

In general, the rule is that you must verify user input at every step. Generally with user input this means to always accept strings. This also requires a little ingenuity when deciding how to validate the input.

In the following example, I have chosen an interactive program. It will complain until you give it valid input.

{$apptype console}
uses SysUtils;

var
  // Here's the stuff we want
  name: string;
  ident: integer;

  // Here's stuff we need to validate user's input
  userinput: string;
  num_digits_and_punct, cntr: integer;

begin
  // Get the user's name.
  //
  // The user could enter anything, even a number.
  // Some people actually have digits in their name,
  // so it is not really feasible to say "that's not a name",
  // excepting for one case: most principalities do not
  // permit a person's name to be just a number or
  // punctuation (that is, it can't be composed only of
  // digits and/or punctuation).
  repeat
    write( 'What is your name? ' );
    readln( name );

    // Convert all tabs, whitespace, ctrl chars, etc. to spaces
    for cntr := 0 to 31 do
      name := StringReplace( name, chr( cntr ), ' ', [rfReplaceAll] );
    name := StringReplace( name, #127, ' ', [rfReplaceAll] );

    // Get rid of leading/trailing spaces
    name := trim( name );

    // Get rid of excessive internal space
    while pos( '  ', name ) <> 0 do 
      name := StringReplace( name, '  ', ' ', [rfReplaceAll] );

    // Determine whether what's left is non-numeric/non-punctuation
    num_digits_and_punct := 0;
    for cntr := 1 to length( name ) do 
      if not (name[ cntr ] in ['A'..'Z', 'a'..'z'])
        then inc( num_digits_and_punct );

    // Valid?
    if length( name ) = num_digits_and_punct then begin
      writeln( 'Your name can''t be blank or just punctuation or digits.' );
      continue
      end;
    break
  until false;

  // Get a number.
  //
  // Again, the user could enter anything, but now we
  // have a simple standard which we can apply against the
  // (potentially invalid) input.
  repeat
    write( 'Enter a number: ' );
    readln( userinput );
    try
      ident := strToInt( userinput )
    except on EConvertError do begin
      writeln( 'That isn''t a number. Please try again.' );
      continue
      end
    end;
    break
  until false;

  writeln( 'Thanks ', name, '. You entered the number ', ident, '.' )
end.

Handling user input always requires some careful thought. The general rule is to try to break your own program: if it expects one type of input, give it anything and everything except what it expects and see what breaks, then fix it.

Whether or not the program refuses to continue what it was doing with bad input (but keeps running properly), halts (terminates), or demands correct input (as my example does) is up to you and/or the needs of the customer and/or environment.

I know this isn't exactly the simple answer you were hoping for, but it is the correct one...

Alas.

Hi Duoas,

If I introduce a letter as an identification number, a window opens with the next message:

Project Project1.exe raised exception class EConvert Error with message “a’ is not a valid integer value’. Process Stopped . Use step or Run to Continue.

Yes, the debugger will catch those before the program does. Just click continue. If you run the program from the command prompt and not the IDE you won't see that message.

You can turn it off in the IDE if you go to Tools-->Debugger Options and uncheck "Integrated Debugging".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.