Recent

Author Topic: Fsearch,if Else problems  (Read 17527 times)

captian jaster

  • Guest
Fsearch,if Else problems
« on: February 21, 2010, 09:04:45 pm »
Code: [Select]
BEGIN
     ClrScr;
     writeln('Hydraks Console is not installed');
     writeln('In order to use Hydraks Console, some files must be written to your hard drive');
     writeln('Input "Install" on your keyboard and press Enter to install');
     writeln('Input "Close" then press Enter to stop the program and not install');
     Readln(command);
     if command = 'Close' then Halt;
     if command = 'Install' then
      BEGIN
        writeln('Now installing, Please wait....');
        Delay(1000);
        writeln('Creating directorys');
        CreateDir('C:\Hydraks');
        CreateDir('C:\Hydraks\Console');
        CreateDir('C:\Hydraks\Docs');
        writeln('Writing Files');
        assign(file1, 'C:\Hydraks\Console\records.dat');
        rewrite(file1);
        writeln(file1, 'User input records:');
        close(file1);
        Delay(3000);
        assign(file2, 'C:\Hydraks\Docs\About.txt');
        rewrite(file2);
        writeln(file2,'Hydraks is a series of mini programs created by Adam N.Andujar');
        writeln(file2,'Current programs include;');
        writeln(file2,'Hydraks Console');
        close(file2);
        delay(2000);
        ClrScr;
        writeln('Installation complete');
        writeln('Press enter to continue');
        readln;
        Goto 1;
        END
     Else
     BEGIN
       writeln('ERROR! unknown command!');
       writeln('Press enter to close the program.');
       readln;
       Halt;
     END;

The program should take teh user to the main menu if the folder exists, but it doesnt even thought the folder is there

DirkS

  • Sr. Member
  • ****
  • Posts: 251
Re: Fsearch,if Else problems
« Reply #1 on: February 21, 2010, 10:06:32 pm »
Quote
The program should take teh user to the main menu if the folder exists, but it doesnt even thought the folder is there
I cannot see the relation between this problem and your code... No search or check for directories anywhere in the code as far as I can see.

(and it's been mentioned before: please don't use goto's).

davesimplewear

  • Sr. Member
  • ****
  • Posts: 319
    • Davids Freeware
Re: Fsearch,if Else problems
« Reply #2 on: February 21, 2010, 10:08:11 pm »
if you are relying on goto1 to take you back to your menu then that is where your problem is.
Quote
if fileexists('c:\Hydracks\blah');
then
action you want to happen
All things considered insanity seems the best option

captian jaster

  • Guest
Re: Fsearch,if Else problems
« Reply #3 on: February 22, 2010, 02:01:10 am »
if file exist works for folders?

captian jaster

  • Guest
Re: Fsearch,if Else problems
« Reply #4 on: February 22, 2010, 02:12:57 am »
Grr, still not working!
Heres the whole code. Suggestions welcomed.(i know im using Goto still but it hasnt failed me yet! ill find an alternative when i get better at pascal.)
Code: [Select]
program console;

uses classes,crt,dos,windows,SysUtils;

var
 RootDir : Pathstr;
 userfile,file1,file2 : Text;
 records,command,usertext : String;

Label
1,CR;


BEGIN
   ClrScr;
   writeln('Please wait...');
   writeln('Now Loading');
   Delay(1000);
   RootDir := Fsearch('C:\Hydraks\Console', GetEnv(''));
   if RootDir = '' then
   BEGIN
     ClrScr;
     writeln('Hydraks Console is not installed');
     writeln('In order to use Hydraks Console, some files must be written to your hard drive');
     writeln('Type "Install" on your keyboard and press Enter to install');
     writeln('Type "Close" then press Enter to stop the program and not install');
     Readln(command);
     if command = 'Close' then Halt;
     if command = 'Install' then
      BEGIN
        writeln('Now installing, Please wait....');
        Delay(1000);
        writeln('Creating directorys');
        Delay(1000);
        CreateDir('C:\Hydraks');
        Delay(1000);
        CreateDir('C:\Hydraks\Console');
        Delay(1000);
        CreateDir('C:\Hydraks\Docs');
        Delay(1000);
        writeln('Writing Files');
        assign(file1, 'C:\Hydraks\Console\records.dat');
        rewrite(file1);
        writeln(file1, 'User input records:');
        close(file1);
        Delay(2000);
        assign(file2, 'C:\Hydraks\Docs\About.txt');
        rewrite(file2);
        writeln(file2,'Hydraks is a series of mini programs created by Adam N.Andujar');
        writeln(file2,'Current programs include;');
        writeln(file2,'Hydraks Console');
        close(file2);
        delay(2000);
        ClrScr;
        writeln('Installation complete');
        writeln('Press enter to continue');
        readln;
        Goto 1;
        END
     Else
     BEGIN
       writeln('ERROR! unknown command!');
       writeln('Press enter to close the program.');
       readln;
       Halt;
     END;
   END;
 if fileexists('C:\Hydraks\Console') then
  BEGIN
1:ClrScr;
  assignfile(file1, 'C:\Hydraks\console\records.dat');
  writeln('Welcome to the Hydraks console');
  writeln('Here you will be able to input a series of commands to get a series of options');
  writeln('Input Help and Press enter to get a list of some commands');
  readln(command);
  append(file1);
  writeln(file1,command);
  closefile(file1);
  if command = 'Help' then
   BEGIN
     writeln('The console works simply like this,');
     writeln('in between "[]" is were your command goes');
     writeln('What your commanding follows after');
     writeln('eg [read]Records');
     writeln('Whats in between the "[]" stays in lower caps');
     writeln('What follows after starts with a capital letter');
     writeln('Here is a list of some commands:');
     writeln('[open]Records(Read things you have entered into the console)');
     writeln('If the console tells you to put in a certain command in a certain way');
     writeln('You have to enter it in that exact way then press enter');
     writeln('Though you do not have to enter "" if it is in the command');
     writeln('Input "Close" to close the program');
     writeln('Input "Return" to go back');
     readln(command);
     if command = 'Return' then Goto 1;
     if command = 'Close' then Halt;
   END;

  if command = '[Open]Records' then
   BEGIN
     ClrScr;
     assignfile(file1, 'C:\Hydraks\console\records.dat');
     reset(file1);
     while not eof(file1) do
     BEGIN
     readln(file1,records);
     writeln(records);
     END;
     closefile(file1);
     writeln;
     writeln('Input "Return" to go back');
     writeln('Input "Clear" to clear your records');
     readln(command);
     if command = 'Return' then Goto 1;
     if command = 'Clear' then
      BEGIN
        ClrScr;
        assignfile(file1, 'C:\Hydraks\console\records.dat');
        Erase(file1);
        rewrite(file1);
        writeln(file1,'User input records:');
        closefile(file1);
        writeln('Erased records, Press Enter to return');
        readln;
        Goto 1;
      END;
   END
  Else
  BEGIN
    writeln('ERROR! Unknown command!');
    writeln('Press enter to go back');
    readln;
    Goto 1;
  END;
  END;
END.

davesimplewear

  • Sr. Member
  • ****
  • Posts: 319
    • Davids Freeware
Re: Fsearch,if Else problems
« Reply #5 on: February 22, 2010, 03:41:40 am »
worked for me by placing in the if fileexists records.dat, here is modified code
Quote
program console;

uses classes,crt,dos,windows,SysUtils;

var
 RootDir : Pathstr;
 userfile,file1,file2 : Text;
 records,command,usertext : String;

Label
1,CR;


BEGIN
   ClrScr;
   writeln('Please wait...');
   writeln('Now Loading');
   Delay(1000);
   RootDir := Fsearch('C:\Hydraks\Console', GetEnv(''));
   if RootDir = '' then
   BEGIN
     ClrScr;
     writeln('Hydraks Console is not installed');
     writeln('In order to use Hydraks Console, some files must be written to your hard drive');
     writeln('Type "Install" on your keyboard and press Enter to install');
     writeln('Type "Close" then press Enter to stop the program and not install');
     Readln(command);
     if command = 'Close' then Halt;
     if command = 'Install' then
      BEGIN
        writeln('Now installing, Please wait....');
        Delay(1000);
        writeln('Creating directorys');
        Delay(1000);
        CreateDir('C:\Hydraks');
        Delay(1000);
        CreateDir('C:\Hydraks\Console');
        Delay(1000);
        CreateDir('C:\Hydraks\Docs');
        Delay(1000);
        writeln('Writing Files');
        assign(file1, 'C:\Hydraks\Console\records.dat');
        rewrite(file1);
        writeln(file1, 'User input records:');
        close(file1);
        Delay(2000);
        assign(file2, 'C:\Hydraks\Docs\About.txt');
        rewrite(file2);
        writeln(file2,'Hydraks is a series of mini programs created by Adam N.Andujar');
        writeln(file2,'Current programs include;');
        writeln(file2,'Hydraks Console');
        close(file2);
        delay(2000);
        ClrScr;
        writeln('Installation complete');
        writeln('Press enter to continue');
        readln;
        Goto 1;
        END
     Else
     BEGIN
       writeln('ERROR! unknown command!');
       writeln('Press enter to close the program.');
       readln;
       Halt;
     END;
   END;
 if fileexists('C:\Hydraks\Console\records.dat') then
  BEGIN
1:ClrScr;
  assignfile(file1, 'C:\Hydraks\console\records.dat');
  writeln('Welcome to the Hydraks console');
  writeln('Here you will be able to input a series of commands to get a series of options');
  writeln('Input Help and Press enter to get a list of some commands');
  readln(command);
  append(file1);
  writeln(file1,command);
  closefile(file1);
  if command = 'Help' then
   BEGIN
     writeln('The console works simply like this,');
     writeln('in between "[]" is were your command goes');
     writeln('What your commanding follows after');
     writeln('eg [read]Records');
     writeln('Whats in between the "[]" stays in lower caps');
     writeln('What follows after starts with a capital letter');
     writeln('Here is a list of some commands:');
     writeln('[open]Records(Read things you have entered into the console)');
     writeln('If the console tells you to put in a certain command in a certain way');
     writeln('You have to enter it in that exact way then press enter');
     writeln('Though you do not have to enter "" if it is in the command');
     writeln('Input "Close" to close the program');
     writeln('Input "Return" to go back');
     readln(command);
     if command = 'Return' then Goto 1;
     if command = 'Close' then Halt;
   END;

  if command = '[Open]Records' then
   BEGIN
     ClrScr;
     assignfile(file1, 'C:\Hydraks\console\records.dat');
     reset(file1);
     while not eof(file1) do
     BEGIN
     readln(file1,records);
     writeln(records);
     END;
     closefile(file1);
     writeln;
     writeln('Input "Return" to go back');
     writeln('Input "Clear" to clear your records');
     readln(command);
     if command = 'Return' then Goto 1;
     if command = 'Clear' then
      BEGIN
        ClrScr;
        assignfile(file1, 'C:\Hydraks\console\records.dat');
        Erase(file1);
        rewrite(file1);
        writeln(file1,'User input records:');
        closefile(file1);
        writeln('Erased records, Press Enter to return');
        readln;
        Goto 1;
      END;
   END
  Else
  BEGIN
    writeln('ERROR! Unknown command!');
    writeln('Press enter to go back');
    readln;
    Goto 1;
  END;
  END;
END.
All things considered insanity seems the best option

davesimplewear

  • Sr. Member
  • ****
  • Posts: 319
    • Davids Freeware
Re: Fsearch,if Else problems
« Reply #6 on: February 22, 2010, 03:43:53 am »
find console folder with program attached
All things considered insanity seems the best option

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: Fsearch,if Else problems
« Reply #7 on: February 22, 2010, 12:25:39 pm »
Please get rid of the goto's!

Basically you can rewrite it without goto's somewhat like this:

Code: [Select]
begin
  ..
  repeat
    //write menu options to screen
    readln(command);
    command := UpperCase(command);
    if command = 'HELP' then
    begin
      //code for Help here
    end
    else if Command = 'INSTALL' then
    begin
      //code for Install here
    end
    else if Command = 'YET ANOTHER COMMAND' then
    begin
      //code for Yet Another Command here
    end
    else
    begin
      writeln('Unrecognized command');
      delay(1000); //make sure user actually sees this messsage
    end;
  until (Command = 'CLOSE');
  //write Goodbye's etc
end.

Notice that when the user enters an illegal command, he now gets an errormessage and the menu is shown again, which IMHO is more user friendly then exiting the program as you did.
Once the user inputs 'CLOSE', the repeat .. until loop is done and the program exits.

Now, if you put the code for Install, Help etc in seperate procedures, your code becomes much more readable and therefore easier to maintain.

Here's a slightly different approach that gets rid of the multiple if..then..else (especially if the number of options grows).

Code: [Select]
Program X;

uses dos,crt,sysutils;

procedure DrawMainChoice;
begin
  clrscr;
  writeln(' Welcome to my fabulous X install program');
  writeln;
  writeln(' I    Install');
  writeln(' H    Help');
  writeln(' S    Something else');
  writeln(' Q    Quit without installing');
  writeln;
  write('Enter your choice: ');
end;

procedure Install;
begin
  //code for Install here
end;

procedure SomethingElse;
begin
  //code for SomethingElse here
end;

procedure Help;
begin
  //code for Help here
end;

var Choice: Char;
begin
  //if necessary do stuff before drawing main choice
  repeat
    DrawMainChoice;
    Choice := Upcase(ReadKey);
    case Choice of
      'I': Install;
      'H': Help;
      'S': SomethingElse;
    end;//case
  until (Choice = 'Q');
  //do cleanup and goodbye's
end.

Now we only react if user presses a 'legal' key, all other keys are simply ignored.

(Code is not tested, so typo's may occurr)

Bart

eny

  • Hero Member
  • *****
  • Posts: 1634
Re: Fsearch,if Else problems
« Reply #8 on: February 22, 2010, 12:26:32 pm »
Grr, still not working!
i know im using Goto still but it hasnt failed me yet!
It won't, but that's not the point.
Incorrect usage (is there any correct usage...) will result in hard to find bugs.

Quote
ill find an alternative when i get better at pascal.
A good starting point would be to grasp the concept of modularity; divide your code into smaller, manageable blocks of code with procedures and functions.

[edit]
... exactly what Bart is showing in the above post... :D
« Last Edit: February 22, 2010, 12:29:12 pm by eny »
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

captian jaster

  • Guest
Re: Fsearch,if Else problems
« Reply #9 on: February 22, 2010, 07:03:42 pm »
It still isnt working fort some reason.
I decided ima make a console without an install and ima use barts idea so theres no Goto.
Thanks to all of you guys and ill keep you posted about how i do in the console

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: Fsearch,if Else problems
« Reply #10 on: February 24, 2010, 01:55:33 pm »
This code is tested and works.

Code: [Select]
Program menu;

uses dos,crt,sysutils;

const
  AllowedMainChoices = ['I','H','S','Q']; //Only these are allowed as the main choice

procedure DrawMainChoice;
begin
  clrscr;
  writeln(' Welcome to my fabulous X install program');
  writeln;
  writeln(' I    Install');
  writeln(' H    Help');
  writeln(' S    Something else');
  writeln(' Q    Quit without installing');
  writeln;
  write('Enter your choice: [ ]');
end;

procedure Install;
begin
  //code for Install here
  ClrScr;
  writeln('Now Installing');
  write('Done. Press <Enter> to continue ... ');
  readln;
end;

procedure SomethingElse;
begin
  //code for SomethingElse here
  ClrScr;
  writeln('Now Something Else');
  write('Done. Press <Enter> to continue ... ');
  readln;
end;

procedure Help;
begin
  //code for Help here
  ClrScr;
  writeln('Now Help');
  write('Done. Press <Enter> to continue ... ');
  readln;
end;

var Choice: Char;
begin
  //if necessary do stuff before drawing main choice
  repeat
    DrawMainChoice;
    //We do nothing until the user presses a key we allow
    repeat
      //Nicely between the brackets we wrote on the screen, just being fancy ;-)
      gotoXY(21,8);
      Choice := Upcase(ReadKey);
    until (Choice in AllowedMainChoices);
    write(Choice);
    gotoXY(21,8);
    delay(500); //actually give user chance to see what we write to screen
    case Choice of
      'I': Install;
      'H': Help;
      'S': SomethingElse;
    end;//case
  until (Choice = 'Q');
  //do cleanup and goodbye's
  ClrScr;
  writeln('Now quitting');
end.

Study the code.
What does it do and why?
If you have any questions about what does what, feel free to ask.

Bart

captian jaster

  • Guest
Re: Fsearch,if Else problems
« Reply #11 on: February 24, 2010, 10:10:26 pm »
Code: [Select]
program Console;

uses CRT,Dos,Windows,Classes,SysUtils;

const
Commands = ['Help','Close'];


Procedure Help;
BEGIN
  writeln('This is helping!!');
  readln;
END;

Procedure Choices;

var
 Command : String;

BEGIN
  writeln('Welcome!, Type a command and press <Enter>');
  repeat
  Readln(Command);
  if Command = 'Help' then Help;
  If Command = 'Close' then Halt
  Else writeln('Unkown Command!');
  Until(Command in Commands);
END



BEGIN
  ClrScr;
  repeat
  Choices
  until(Command = 'Close');
END.

 
i did something like what you said and got an ordinal expression expected error at the const

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: Fsearch,if Else problems
« Reply #12 on: February 24, 2010, 11:56:02 pm »
Hi,
Code: [Select]
const
  Commands = ['Help','Close'];

Read up on Sets and on Ordinal types.
Sets can only be made up of ordinal types.
So you can have a set of Char, set of Byte etc, but String is not an ordinal type, so you cannot have a set of it.
Yes, it's annoying, but that's just how it is.

Code: [Select]
Procedure Choices;

var
 Command : String;

BEGIN
  writeln('Welcome!, Type a command and press <Enter>');
  repeat
  Readln(Command);
  if Command = 'Help' then Help;
  If Command = 'Close' then Halt
  Else writeln('Unkown Command!');
  Until(Command in Commands);
END

For the sake of arguments, let's assume it would be possible to have sets of strings, then a more logical order would be:
(Note: this will NOT compile)

Code: [Select]
const
  Commands = ['HELP','CLOSE'];  //make them uppercase

BEGIN
  //Give the user some clue as to what command he is supposed to enter
  writeln('Welcome!, Type a "Help" or "Close" (without the quotes) and press <Enter>');
  repeat
    Readln(Command);
    //by comparing them UpperCase, we "forgive" the user if his Shift-Key is broken ;-)
    Command := UpperCase(Command);
    if not (Command in Commands) then  writeln('Unkown Command!');
  Until (Command in Commands);
  if Command = 'Help' then Help;
  //no need to do: if Command = 'Close' then Halt, just leave this procedure
  //in the main body, where you call this, you already check this and then leave the program, so that's OK
END;

This part rewritten so that ir should compile (if I make no typo's)

Code: [Select]
var
  ValidCommand: Boolean;
BEGIN
  //Give the user some clue as to what command he is supposed to enter
  writeln('Welcome!, Type a "Help" or "Close" (without the quotes) and press <Enter>');
  repeat
    Readln(Command);
    //by comparing them UpperCase, we "forgive" the user if his Shift-Key is broken ;-)
    Command := UpperCase(Command);
    //Since I need this comparison twice, I just save it in a variable
    //so that I do not have to do the string comparison again
    ValidCommand :=  ( (Command = 'HELP') or (Command = 'CLOSE') );
    if not ValidCommand then writeln('Unkown Command!');
  Until ValidCommand;
  if Command = 'Help' then Help;
END;

Your decision to let the user enter full commands, although being a matter of taste, makes it harder to write the code.
Comparing strings, especially if there are lots of allowed "answers", will require lots of if..then..else statements (and thus lots of typing strings, and possible typo's).

Bart

RudieD

  • Full Member
  • ***
  • Posts: 234
Re: Fsearch,if Else problems
« Reply #13 on: February 25, 2010, 01:45:20 am »
I know I shouldn't, .....
but you can always do this :

Code: [Select]
program ConsoleApp;

uses CRT,Dos,{$ifdef linux}Linux{$else}Windows{$endif},Classes,SysUtils;

var
  CmdLst: TStringList;
  Idx: integer;

Procedure Help;
BEGIN
  writeln('This is helping!! press <Enter>');
  readln;
END;

Procedure Install;
BEGIN
  writeln('This is installing!! press <Enter>');
  readln;
END;

Procedure Choices;
var
  Command : String;
BEGIN
  writeln('Welcome!, Type a command and press <Enter>');
  repeat
    Readln(Command);
    Idx := CmdLst.IndexOf(Command);
    if (Idx < 0) then
      writeln('Unkown Command! Try again');
  Until (Idx > -1);
END;

BEGIN
  CmdLst := TStringList.Create();
  try
    CmdLst.CaseSensitive := False;
    CmdLst.AddObject('Close',nil); // Make Close the first Item
    CmdLst.AddObject('Help',TObject(@Help));
    CmdLst.AddObject('Install',TObject(@Install));
    repeat
      ClrScr;
      Choices;
      if Idx > 0 then
        TProcedure(CmdLst.Objects[Idx]);
    until(Idx=0);
  finally
    CmdLst.Clear;
    CmdLst.Free;
  end;
END.

 :-X I just couldn't help my self....
 >:D Don't do what I do, do what they say !  :D
« Last Edit: February 25, 2010, 01:51:28 am by RudieD »
The FRED Trainer. (Training FRED with Lazarus/FPC)

captian jaster

  • Guest
Re: Fsearch,if Else problems
« Reply #14 on: February 25, 2010, 03:20:37 am »
dood i am were no nere that level

 

TinyPortal © 2005-2018