Forum > General

Fsearch,if Else problems

<< < (2/4) > >>

davesimplewear:
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.
--- End quote ---

davesimplewear:
find console folder with program attached

Bart:
Please get rid of the goto's!

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


--- Code: ---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.

--- End code ---

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: ---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.

--- End code ---

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:

--- Quote from: captian jaster on February 22, 2010, 02:12:57 am ---Grr, still not working!
i know im using Goto still but it hasnt failed me yet!

--- End quote ---
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.
--- End quote ---
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

captian jaster:
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

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version