Board index » delphi » What's wrong with this program?

What's wrong with this program?

Quote
In article <36867C45.AF4B3...@hknet.com>, klai  <k...@hknet.com> wrote:
>The program below is for printing out a simple bar chart. I used
>'Repeat' in the main program, but when it repeats, it doesn't wait for
>the user to input the title the print the other sentence, so it becomes
>:
>Enter chart title: Enter data:

>Pls tell me what's wrong with it. Thanks alot.
>?

>Program Chart;
>type NumType= array[1..8] of integer;
>var title,Bar,Asterisks,Space : string;
>??? num : NumType;
>??? i,j,NoOfAsterisk, NoOfSpace : integer;

>Procedure GetTitle;
>begin
>???? write('Enter chart title: ');
>???? readln(title)
>end;

>Procedure Getdata;
>begin
>???? write('Enter data: ');
>???? for i:=1 to 8 do
>???????? read(Num[i])

The problem is here, you do not read the carriage return. Add a single
readln; and you should be OK. The input in TP is buffered. That means if
you leave something unread it will be read next time.

Quote
>end;

>Procedure PrintOut;
>begin

Osmo
 

Re:What's wrong with this program?


klai [mailto:k...@hknet.com] decided it was auspicious moment

Quote
>The program below is for printing out a simple bar chart. I used
>'Repeat' in the main program, but when it repeats, it doesn't wait for
>the user to input the title the print the other sentence, so it becomes
>:
>Enter chart title: Enter data:

>Pls tell me what's wrong with it. Thanks alot.
>?

>Procedure Getdata;
>begin
>???? write('Enter data: ');
>???? for i:=1 to 8 do
>???????? read(Num[i])
          Readln;
>end;

Your 8 reads of numbers do not read the end of line so when you ask for
a readln later in the program, it reads the carriage return.

If you put Readln; in the place shown, this will "soak up" the Carriage
Return.

--
Pedt
Kellemes Karacsonyi Unnepeket

Re:What's wrong with this program?


Quote
Pedt Scragg wrote:
> Pedt
> Kellemes Karacsonyi Unnepeket

This looks Hungarian, but, of course, it is not!?
Is ist Man's Gale and what does it mean, something
about the story with the crocodile's mother?

:-) Franz

Re:What's wrong with this program?


The program below is for printing out a simple bar chart. I used
'Repeat' in the main program, but when it repeats, it doesn't wait for
the user to input the title the print the other sentence, so it becomes
:
Enter chart title: Enter data:

Pls tell me what's wrong with it. Thanks alot.
?

Program Chart;
type NumType= array[1..8] of integer;
var title,Bar,Asterisks,Space : string;
??? num : NumType;
??? i,j,NoOfAsterisk, NoOfSpace : integer;

Procedure GetTitle;
begin
???? write('Enter chart title: ');
???? readln(title)
end;

Procedure Getdata;
begin
???? write('Enter data: ');
???? for i:=1 to 8 do
???????? read(Num[i])
end;

Procedure PrintOut;
begin
???? writeln('|---------|---------|---------|');
???? for i:=1 to 8 do
???????? begin
????????????? NoOfAsterisk:=round(Num[i]/2);
????????????? NoOfSpace:=30-NoOfAsterisk;
????????????? Asterisks:='';
????????????? Space:='';
????????????? if NoOfAsterisk > 0
???????????????? then
???????????????????? begin
????????????????????????? for j:=1 to NoOfAsterisk do
????????????????????????????? Asterisks := Asterisks + '*';
???????????????????? end;
????????????? if NoOfSpace > 0
???????????????? then
???????????????????? begin
????????????????????????? for j:=1 to NoOfSpace do
????????????????????????????? Space := Space + ' ';
???????????????????? end;
????????????? Bar:='|'+Asterisks+Space;
????????????? writeln(bar,Num[i]:4)
???????? end;

???? writeln(Title)
end;

begin
???? repeat
???? GetTitle;
???? If title<>'XXX'
??????? then
??????????? begin
???????????????? Getdata;
???????????????? PrintOut
??????????? end
???? until Title = 'XXX'
end.
?
?

Re:What's wrong with this program?


Ing. Franz Glaser [mailto:meg-gla...@eunet.at] decided it was auspicious
for

Quote
>Pedt Scragg wrote:

>> Pedt
>> Kellemes Karacsonyi Unnepeket

>This looks Hungarian, but, of course, it is not!?

It is indeed Hungarian - it means "Merry Christmas" :-)

--
Pedt

Re:What's wrong with this program?


Try Changing

Procedure Getdata;
begin
      write('Enter data: ');
      for i:=1 to 8 do
          read(Num[i])
end;

For The Following:

Procedure Getdata;
begin
      write('Enter data: ');
      for i:=1 to 7 do
          read(Num[i]);
      ReadLn(Num[8])
end;

Reason:

You hit 'Enter' on your last number (8th) and since it is read by the Read
statement, instead of a ReadLn, the 'Enter' Code goes to the next available
Read/ReadLn, which happens to be the 'Enter Chart Title: ' part.
Of course you can use some keyboard flushing code, or trap lastkey, etc, but
this will give you the broad idea. Hope it helps...

ju...@di-t.com
Digital Imaging & Technologies Inc.

In article <36867C45.AF4B3...@hknet.com>,
  klai <k...@hknet.com> wrote:

Quote
> The program below is for printing out a simple bar chart. I used
> 'Repeat' in the main program, but when it repeats, it doesn't wait for
> the user to input the title the print the other sentence, so it becomes
> :
> Enter chart title: Enter data:

> Pls tell me what's wrong with it. Thanks alot.
> ?

> Program Chart;
> type NumType= array[1..8] of integer;
> var title,Bar,Asterisks,Space : string;
> ??? num : NumType;
> ??? i,j,NoOfAsterisk, NoOfSpace : integer;

> Procedure GetTitle;
> begin
> ???? write('Enter chart title: ');
> ???? readln(title)
> end;

> Procedure Getdata;
> begin
> ???? write('Enter data: ');
> ???? for i:=1 to 8 do
> ???????? read(Num[i])
> end;

> Procedure PrintOut;
> begin
> ???? writeln('|---------|---------|---------|');
> ???? for i:=1 to 8 do
> ???????? begin
> ????????????? NoOfAsterisk:=round(Num[i]/2);
> ????????????? NoOfSpace:=30-NoOfAsterisk;
> ????????????? Asterisks:='';
> ????????????? Space:='';
> ????????????? if NoOfAsterisk > 0
> ???????????????? then
> ???????????????????? begin
> ????????????????????????? for j:=1 to NoOfAsterisk do
> ????????????????????????????? Asterisks := Asterisks + '*';
> ???????????????????? end;
> ????????????? if NoOfSpace > 0
> ???????????????? then
> ???????????????????? begin
> ????????????????????????? for j:=1 to NoOfSpace do
> ????????????????????????????? Space := Space + ' ';
> ???????????????????? end;
> ????????????? Bar:='|'+Asterisks+Space;
> ????????????? writeln(bar,Num[i]:4)
> ???????? end;

> ???? writeln(Title)
> end;

> begin
> ???? repeat
> ???? GetTitle;
> ???? If title<>'XXX'
> ??????? then
> ??????????? begin
> ???????????????? Getdata;
> ???????????????? PrintOut
> ??????????? end
> ???? until Title = 'XXX'
> end.
> ?
> ?

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    

Other Threads