-6

here is my code :

var
aChar,temp : char ;
test : string ;
k : integer ;
begin
K := 1 ;
test := edit1.Text ;
for k := 1 to 10 do
temp := test[k] ;
aChar := upcase(Temp) ;
richEdit1.Lines.Add(aChar);
richEdit1.Lines.Add(#13) ;
end;

for some reason it returns random values such as # and T but i have initialized them ? anybody can figure it out please let me know as im writing on this stuff tomorrow .

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 4
    Do some debugging. The single most neglected skill of the novice programmer. – David Heffernan Jun 26 '17 at 16:12
  • After formatting your code properly, the problem becomes clear. You don't have any begin/end block on your `for` statement, so you're just overwriting the value of `temp` each loop without ever using it until the last iteration, and gets just the 10th character instead of all of them. Or at least that's part of the problem. Also, what are the contents of your `edit1` control? Is it guaranteed to always have at least 10 characters? – Jerry Dodge Jun 26 '17 at 16:18
  • no it isnt always guaranteed , i will go have a look quickly and fix the begin and end problem – delphi247 Jun 26 '17 at 16:28
  • When the format is what conceals the problem, @Jerry, please don't change the format. It essentially makes the question go away. – Rob Kennedy Jun 26 '17 at 20:58
  • @Rob I did not see this until well after I formatted it. Like I said, "**after**" formatting... – Jerry Dodge Jun 26 '17 at 21:42
  • That was the point in time where you could have gone back and undone the mistaken edit Jerry, when you realised what the error was. – David Heffernan Jun 27 '17 at 06:24
  • @David I'm sorry, I didn't get the memo that it was EVERYBODY ATTACK JERRY DAY. PS - Someone else came and did the same thing since then anyway. I. GIVE. UP. – Jerry Dodge Jun 27 '17 at 20:30
  • @guido please don't format the question and thereby remove the entire crux of the question – David Heffernan Jun 27 '17 at 20:45
  • @Jerry Why so tetchy? It's not personal. Nobody is picking on you. – David Heffernan Jun 27 '17 at 22:41
  • @DavidHeffernan Sorry but at first glance it really needed formatting. I did not read the comments my bad – GuidoG Jun 28 '17 at 06:40
  • @guido no probs, quite understandable – David Heffernan Jun 28 '17 at 06:41

1 Answers1

3

As Jerry already said, your loop is wrong:

  var
    Len: Integer;
  ...
    Test := Edit1.Text;
    Len := Length(Test);
    if Len > 10 then 
      Len := 10;
    for K := 1 to Len do
    begin // !!!
      Temp := Test[K] ;
      AChar := UpCase(Temp) ;
      RichEdit1.Lines.Add(AChar);
      RichEdit1.Lines.Add(#13);
    end; // !!!
  end;

You would probably have found out if you had used the debugger (which is quite easy, in Delphi) and/or if you had formatted your code properly (the IDE can do that for you too).

Proper formatting is a great tool in finding things that somehow look weird or don't format as expected. These are often errors.

Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
  • Perfect code is working , Thank you very much . ill probably be asking a few more throughout the night as im behind and trying to recatch up the work for the exam tomorrow. – delphi247 Jun 26 '17 at 16:35
  • 3
    Why not do your work yourself? Won't that be better for you? Why not invest in some debugging skills? – David Heffernan Jun 26 '17 at 17:04
  • 2
    @David is right. This is not a homework support site. And if you like it, please upvote and/or accept my answer. – Rudy Velthuis Jun 26 '17 at 19:00
  • 3
    @delphi247: Debugging is usually faster than waiting for an answer on S.O., and you learn from it. Learn to help yourself. And switch on all checks: overflow, range, etc. in the Project options. – Rudy Velthuis Jun 26 '17 at 19:02