0

I am trying to write a program for my school-based assignment. It is supposed to accept names as strings, and a salary input as an integer, and tell you whether you are illegible or able to participate in party sections, and repeats until stop=9.

Everything went well, but the issue is the string variables accept erroneous names as inputs and continue. For example 'John32@#$^^'.

I want the program to not accept these inputs, and write a line stating that it will not continue with any name with anything other than characters. This should also not add a number to stop. The source code is below.

program Sba;

Uses
  Windows;

var
  FName,LName:String;
  Stop,Salary:Integer;

//procedure Sleep(milliseconds: Cardinal); stdcall; external 'kernel32.dll'; //Added sleep as a procedure for delay

Begin
  Repeat //Repeats until stop = 9
    Writeln('Hi User,please Enter your First Name'); //Prompting user to enter their First initials
    Readln(FName); //Attaches the string entered to the variable FName
    Sleep(1000);//Delay 1second
    Writeln('First Name entered');  //Lets the user know they have entered their first initials
    Sleep(1000);
    Writeln('Please Enter your Last name'); //Prompting user to enter their last initial
    Readln(LName);  //Attaches the string entered to the variable LName
    Sleep(1000);
    Writeln('Last Name entered'); //Lets the user know they have entered their last initials
    Sleep(800);
    Writeln('Hi ',' ',FName,' ',LName); //Writes Line Hi (First Name) (Last Name)
    Sleep(1000);
    Writeln('Please enter The amount you have paid');//Prompts user for their payment
    Readln(Salary); //Atttached value entered to Salary
    Writeln('Reading your payment will be done in four seconds');
    Sleep(4000);//delay 4 seconds
    Writeln('Done!');
    If (Salary<1000) Then
    Begin
      Writeln('You do not have sufficient funds to play in a section');
    Sleep(1000);
    End;
    Sleep(1000);
    If (Salary>=1000) AND (Salary<=2000) then //Testing conditions
    Begin
      Sleep(1000);
      Writeln('Congrats, You are eligible to play in the Tribe Section'); //Tells use what section they are available to play in
    End;
    If (Salary>=2100) AND (Salary<=3000) then //Testing conditions
    Begin
      Sleep(1000);
      Writeln('Congrats, You are eligible to play in the Poison Section');
    End;
    If (Salary>=3100) AND (Salary<=5000) then //Testing conditions
    Begin
      Sleep(1000);
      Writeln('Congrats, You are eligible to play in the Harts Section');
    End;
    Writeln('Press enter to continue');
    Readln;
    Writeln('Enjoy your day ' , FName,'!');
    Stop:=Stop+1; //Repeats until stop= 9
    Sleep(1000);
  Until Stop=9;//Repeats this 10 times
  Writeln('Written By Timothy Adams 4-1');
  Readln;
end.
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Moha
  • 33
  • 5
  • How about iterating over the name and checking that each character is either a letter or a whitespace character? What Delphi version? – Andreas Rejbrand May 08 '20 at 13:51
  • Delphi 10.3 is the version I am currently using. – Moha May 08 '20 at 13:59
  • I don't understand what your question is. You can't ask for general help. You need to ask a specific question. – David Heffernan May 08 '20 at 14:03
  • My apologies sir. the basis of my post is is there a way to not accept Special characters in strings. – Moha May 08 '20 at 14:06
  • What if user enters 2050 as payment? When you ask the user for input, and you then classify their input, be sure to check for all values in the complete range of all classes. F.ex. >=1000 .. <2000, >=2000 ... <3000. Also check for values higher than the highest allowed, so you can present an error message. – Tom Brunberg May 08 '20 at 16:30
  • You might also want to not accept user input consisting of one or multiple spaces only; I'd hate a program that outputs `Enjoy your day !`. – AmigoJack May 08 '20 at 22:01
  • I'm particularly new to programming this is my second time writing a program forgive me for my very unintellectual usage in the program – Moha May 08 '20 at 23:57

1 Answers1

2

Since this is homework, I will not give you exactly what you ask for. Instead, I'll give you a somewhat more involved example to study.

First, we must decide what a valid name looks like. Because if we don't know exactly what names are to be considered valid, we surely cannot program a computer to tell the difference!

Just as a toy example, let's say that a name is valid iff:

  1. It contains only letters, whitespace, and the HYPHEN-MINUS (-) character. [Notice that everything in a string is a character. Examples of character classes are letters, digits, whitespace, and punctuation. So when you say that the string must only contain "characters", you mean something else, like "only letters".]

  2. It contains at least two letters.

This is by far not good enough for a real-world application. It would disallow many valid names. But it is good enough for this toy example.

Let's implement this!

function IsValidName(const S: string): Boolean;
var
  i, c: Integer;
begin

  // The string must only contain letters, whitespace, and HYPHEN-MINUS

  for i := 1 to S.Length do
    if not (S[i].IsLetter or S[i].IsWhiteSpace or (S[i] = '-')) then
      Exit(False);

  // The string must contain at least two letters

  c := 0;
  for i := 1 to S.Length do
    if S[i].IsLetter then
      Inc(c);

  if c < 2 then
    Exit(False);

  Result := True;

end;

Study this function until you fully understand how it works!

Done? Great! Now let's make use of it:

program Project1;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,
  Character;

var
  Name: string;

function IsValidName(const S: string): Boolean;
begin
  // same as above, won't repeat it here
end;

begin
  try
    try

      Writeln('Hello! What is your name?');

      while True do
      begin
        Readln(Name);
        Name := Name.Trim;
        if IsValidName(Name) then
        begin
          Writeln('Welcome, ', Name, '!');
          Break;
        end
        else
          Writeln('Surely that isn''t your real name? What is your actual name?');
      end;

    except
      on E: Exception do
        Writeln(E.ClassName, ': ', E.Message);
    end;
  finally
    Writeln('Program about to end. Press Return to exit.');
    Readln;
  end;
end.

Again, since I am almost breaking the rules by doing your homework for you, I won't explain the logic in detail. Instead, I'll let you figure out how it works for yourself. That way you will learn a lot more.

Update: The complete program should look like this:

program Project1;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,
  Character;

var
  Name: string;

function IsValidName(const S: string): Boolean;
var
  i, c: Integer;
begin

  // The string must only contain letters, whitespace, and HYPHEN-MINUS

  for i := 1 to S.Length do
    if not (S[i].IsLetter or S[i].IsWhiteSpace or (S[i] = '-')) then
      Exit(False);

  // The string must contain at least two letters

  c := 0;
  for i := 1 to S.Length do
    if S[i].IsLetter then
      Inc(c);

  if c < 2 then
    Exit(False);

  Result := True;

end;

begin
  try
    try

      Writeln('Hello! What is your name?');

      while True do
      begin
        Readln(Name);
        Name := Name.Trim;
        if IsValidName(Name) then
        begin
          Writeln('Welcome, ', Name, '!');
          Break;
        end
        else
          Writeln('Surely that isn''t your real name? What is your actual name?');
      end;

    except
      on E: Exception do
        Writeln(E.ClassName, ': ', E.Message);
    end;
  finally
    Writeln('Program about to end. Press Return to exit.');
    Readln;
  end;
end.
Andreas Rejbrand
  • 105,602
  • 8
  • 282
  • 384
  • Thank you I appreciate the help and not giving the answer, I'll try my best to figure out and understand the program and functions you have provided. Have a great day. – Moha May 08 '20 at 14:34
  • I am currently doing other classes I'll get back to you on your answer, Thanks once again. – Moha May 08 '20 at 14:38
  • I tried running your function in delphi 10.3 and a bit of compilation errors – Moha May 08 '20 at 17:16
  • No, the code above runs as is. You need to replace the `IsValidName` function from the first snippet into the program, and you must make sure that the program name is correct. You can for instance use the IDE to create a new console application and then (1) add the `Character` unit to the `uses` list, (2) add the `Name` variable declaration, (3) add the `IsValidName` function, and then the body between `begin` and `end.`. It will run, if you make no mistakes. – Andreas Rejbrand May 08 '20 at 17:26
  • (Or do you mean CodeInsight errors in the IDE, like red wavy lines? That's because the IDE is buggy. The compiler, however, is fine. The program compiles and runs.) – Andreas Rejbrand May 08 '20 at 17:28
  • This is how I wrote my program, it was your functions and the example written as one, I may have done something wrong and I apologise if its something very easy to solve and I'm just over looking.I also left the error outputs alongside the lines of which the error occurred. https://pastebin.com/rq5CqvV1 – Moha May 09 '20 at 00:36
  • @Moha: You forgot the header of the function `IsValidName`! – Andreas Rejbrand May 09 '20 at 07:18
  • Your completed program worked very well, I ought to say that you are an advanced pascal user, this is how I added your functions into my program, I know it has a lot of errors, I just wanted to see how it looked so far https://pastebin.com/XmDcb3ff – Moha May 09 '20 at 15:49