Recent

Author Topic: Help with a program  (Read 2610 times)

Krims

  • New member
  • *
  • Posts: 9
Re: Help with a program
« Reply #15 on: February 24, 2020, 09:53:53 pm »
so I'm using this program
Code: Pascal  [Select][+][-]
  1. program Generate99;
  2.  
  3. {$mode objfpc}
  4. {$H+}
  5.  
  6. uses sysutils;
  7.  
  8. var
  9.   sourcestr: string;
  10.   resultstr: string;
  11.   n: integer;
  12.  
  13. begin
  14.   writeln('provide a string:');
  15.   readln(sourcestr);
  16.   for n := 0 to 99 do begin
  17.     resultstr := StringReplace(sourcestr, '*', IntToStr(n div 10), []);
  18.     resultstr := StringReplace(resultstr, '*', IntToStr(n mod 10), []);
  19.     writeln(resultstr);
  20.   end;
  21. end.                                                                            
and I have one more problem, I just need it to add ':password' to the end of every code generated like this : 71v64kpDW3QMToVcKGfL:password
And I also appreciate all the responses from y'all, this is definitely the best community I've ever joined. 

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Help with a program
« Reply #16 on: February 24, 2020, 09:58:39 pm »
and I have one more problem, I just need it to add ':password' to the end of every code generated like this : 71v64kpDW3QMToVcKGfL:password
And I also appreciate all the responses from y'all, this is definitely the best community I've ever joined.

resultStr := resultStr + ':password';

Or, depending on the precise compiler mode etc.,

resultStr += ':password';

MarkMLl

MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Krims

  • New member
  • *
  • Posts: 9
Re: Help with a program
« Reply #17 on: February 25, 2020, 04:40:33 pm »
Is there anyway that I can have it accept multiple entries? so I don't have to enter each value at once and execute the program.

rvk

  • Hero Member
  • *****
  • Posts: 6110
Re: Help with a program
« Reply #18 on: February 25, 2020, 05:03:04 pm »
Is there anyway that I can have it accept multiple entries? so I don't have to enter each value at once and execute the program.
How do you want this. First entering multiple strings.
Or entering a string, result, entering a new string.
(You can do both with a repeat/until loop.)

What have you come up with until now?
(it's best you try it yourself first otherwise you won't learn  ;))

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Help with a program
« Reply #19 on: February 25, 2020, 06:10:20 pm »
Is there anyway that I can have it accept multiple entries? so I don't have to enter each value at once and execute the program.

The best way to make it accept strings from wherever they come is to separate the processing from the input. That is, build a function doing the processing of one string and you can use it from elsewhere no matter how you get the string(s) from the user. For example, this program will ask and process as many strings as you can type until you get bored and just press <Enter> when asked for a new one:

Code: Pascal  [Select][+][-]
  1. program Generate99;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   sysutils;
  7.  
  8. var
  9.   SrcStr,
  10.   ResStr: string;
  11.  
  12. function ProcessString(const SrcStr: String): String;
  13. var
  14.   n: integer = 0;
  15. begin
  16.   Result := SrcStr;
  17.   while Pos('*', Result) > 0 do begin
  18.     Result := StringReplace(Result, '*', IntToStr(n mod 10), []);
  19.     Inc(n);
  20.     if n > 99 then begin
  21.       WriteLn(ErrOutput, 'It''s full of stars!');
  22.       Break;
  23.     end;
  24.   end;
  25.   Result := Result + ':password';
  26. end;
  27.  
  28. begin
  29.   repeat
  30.     writeln('Gimme a string!: ');
  31.     readln(SrcStr);
  32.     if SrcStr <> '' then begin
  33.       ResStr := ProcessString(SrcStr);
  34.       writeln(ResStr);
  35.     end;
  36.   until SrcStr = '';
  37.   WriteLn('That''s all, folks!');
  38. end.

Using a separate function like that allows you to modify easily the program to, for example, read the strings from the command line, load them from a text file, etc. For example, replacing the main block by this one:

Code: Pascal  [Select][+][-]
  1. var
  2.   srcFile: TextFile;
  3. begin
  4.   AssignFile(srcFile, 'mystrings');
  5.   Reset(srcFile);
  6.   try
  7.     while not Eof(srcFile) do begin
  8.       ReadLn(srcFile, SrcStr);
  9.       WriteLn(ProcessString(SrcStr))
  10.     end;
  11.   finally
  12.     CloseFile(SrcFile);
  13.   end;
  14.   WriteLn('That''s all, folks!');
  15. end.

will read (and process) each line from the file mystrings instead of reading them from the console.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Krims

  • New member
  • *
  • Posts: 9
Re: Help with a program
« Reply #20 on: March 01, 2020, 08:25:12 pm »
Is there anyway that I can have it accept multiple entries? so I don't have to enter each value at once and execute the program.
How do you want this. First entering multiple strings.
Or entering a string, result, entering a new string.
(You can do both with a repeat/until loop.)

What have you come up with until now?
(it's best you try it yourself first otherwise you won't learn  ;))
Dude! I want to really thank you for your advice, if you didn't tell me to go and try to do something on my own, I would've just kept copying what you guys posted. I came up with something on my own and I feel really proud that it worked, although it may seem easy to you, I really enjoyed correcting my errors and  finding out what wrong with my code. Thanks everybody!

 

TinyPortal © 2005-2018