?
Solved

question on looping

Posted on 2003-03-24
26
Medium Priority
?
186 Views
Last Modified: 2012-05-04
hi, i am a beginner in pascal. i wrote a program that can find the highest, lowest and average from a bunch of numbers, but when i run the program, the question (is it called a question? anyway, the line that appears before you enter the integer,character...etc.) is repeated two times. what's wrong?
my program:

program d9;
uses crt;
var m, c, h, l, t : integer;
begin
clrscr;
c := 0;
t := 0;
h := -1;
l := 9999;
write('Mark ? ');
readln(m);
while m <> -999 do
begin
c := c + 1;
t := t + m;
if m >= h then
h := m
else if m <= l then
l := m;
write('Mark ? ');
readln(m)
end;
writeln;
writeln('Average of the ', c ,' marks = ', t/c :0:0);
writeln('Highest mark = ', h);
writeln('Lowest mark = ', l);
end.

the result:

Mark ? Mark ? (entered value)
Mark ? (and so on....)
0
Comment
Question by:cassiohui
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 15
  • 8
  • 2
  • +1
26 Comments
 
LVL 1

Expert Comment

by:Newt6398
ID: 8194808
Take Mark out of your While loop


while m <> -999 do
begin
c := c + 1;
t := t + m;
  if m >= h then
   h := m
    else if m <= l then
    l := m;
   write('Mark ? ');
readln(m)
end;

if mark is supposed to be written only if the conditional is false then there is more than one line of code in the else part of the conditional so use BEGIN & END
otherwise try taking the mark out of the loop and only write it at once the loop has completed.

I am not following you program very while. But at a glance i'd guess this is a simple mistake. It doesn't seem that m is ever going to reach -999 so the loop can finish maybe this is what you want?????

Also make sure that if your variables start at 0 (zero) to minus 1 from them when nessacery.
0
 
LVL 1

Expert Comment

by:Newt6398
ID: 8194843
Take Mark out of your While loop


while m <> -999 do
begin
c := c + 1;
t := t + m;
  if m >= h then
   h := m
    else if m <= l then
    l := m;
   write('Mark ? ');
readln(m)
end;

if mark is supposed to be written only if the conditional is false then there is more than one line of code in the else part of the conditional so use BEGIN & END
otherwise try taking the mark out of the loop and only write it at once the loop has completed.

I am not following you program very while. But at a glance i'd guess this is a simple mistake. It doesn't seem that m is ever going to reach -999 so the loop can finish maybe this is what you want?????

Also make sure that if your variables start at 0 (zero) to minus 1 from them when nessacery.
0
 

Author Comment

by:cassiohui
ID: 8194855
yes, entering -999 will stop the entering data part and show the results
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:cassiohui
ID: 8194866
QUOTE
if mark is supposed to be written only if the conditional is false then there is more than one line of code in the else part of the conditional so use BEGIN & END
ENDQUOTE

sorry, but i don't understand this part of your answer
0
 
LVL 6

Expert Comment

by:MannSoft
ID: 8195033
Does it only prompt twice the very first time?  I dont see anything wrong really.  Maybe there are already keys in the input buffer, so the first ReadLn(m) is picking them up.  Try putting this code right after the l := 9999; and before the first Write() and ReadLn() calls:

while (KeyPressed) do
      ReadKey;

That will clear the input buffer of any keypresses it may contain.  If it still shows the prompt twice that obviously wasnt the problem.  But I honestly cant see what else it might be.
0
 

Author Comment

by:cassiohui
ID: 8195076
it does only prompt twice in the first time.
i use the old microsoft quick pascal software (1989) to write the program, and the debugging feature in it says that there is nothing in the input buffer of (m)
0
 

Author Comment

by:cassiohui
ID: 8195089
it does only prompt twice in the first time.
i use the old microsoft quick pascal software (1989) to write the program, and the debugging feature in it says that there is nothing in the input buffer of (m)
0
 

Author Comment

by:cassiohui
ID: 8195106
it does only prompt twice in the first time.
i use the old microsoft quick pascal software (1989) to write the program, and the debugging feature in it says that there is nothing in the input buffer of (m)
0
 

Author Comment

by:cassiohui
ID: 8195137
it does only prompt twice in the first time.
i use the old microsoft quick pascal software (1989) to write the program, and the debugging feature in it says that there is nothing in the input buffer of (m)
0
 

Author Comment

by:cassiohui
ID: 8195145
sorry about the repeated comments, hit the wrong button
0
 
LVL 6

Expert Comment

by:MannSoft
ID: 8195257
I've never used that compiler, so I dont know how good it is compared to Turbo Pascal, but you can download version TP5.5 free from www.borland.com

But anyway, I didnt mean to specifically check the m variable.  When you call any of the input functions they read from the input buffer and store the value into the variable you tell it to (m in this case).

What I think might be happening is that when you call readln() for the first time, there is already "junk" in the input buffer, and that is what is being read into the m variable, and why the prompt doesnt wait for you to type in a number.

You didnt say if you tried the code or not.  If you did, then like I said before that obviously isnt the problem.  But the fact that it only does it once at the start makes it look like that is the problem.
0
 

Author Comment

by:cassiohui
ID: 8195392
i did put the code:
l := 9999;
while (KeyPressed) do
  ReadKey;
write('Mark ? ');
But an error came out:
Invalid Vareable Reference
A constant, function or expression has been applied to a procedure expecting a VAR parameter. Subsitute a variable to continue.
0
 

Author Comment

by:cassiohui
ID: 8195706
i did put the code:
l := 9999;
while (KeyPressed) do
  ReadKey;
write('Mark ? ');
But an error came out:
Invalid Vareable Reference
A constant, function or expression has been applied to a procedure expecting a VAR parameter. Subsitute a variable to continue.
0
 
LVL 6

Expert Comment

by:MannSoft
ID: 8195828
Yeah that compiler is obviously not compatable with Borland's Turbo Pascal.  In TP ReadKey is defined as:

function ReadKey: Char;

but from the error description it sounds like your compiler has it defined as:

procedure ReadKey(var Ch: Char);

In that case, you'll need to define another variable at the top of your program.  Make it Ch: Char, and change the code to:

while (KeyPressed) do
      ReadKey(Ch);

If that doesnt work you'll have to look up the function in the help file to see how exactly it works.
0
 

Author Comment

by:cassiohui
ID: 8195916
..............
i still don't understand, but thanks anyway, MannSoft. i'll go ask my teacher, coz this is a homework
0
 
LVL 6

Expert Comment

by:MannSoft
ID: 8195965
I havent read the rules lately, but I would assume helping with homework is allowed here, so long as the person isnt saying "write this whole program for me".  You've done the assignment and are just stuck on what I think is probably a compiler issue.  I dont have Turbo Pascal in front of me right now so I cant test, but I'll bet it would compile and run that program without the multiple "Mark ? " prompts.

Anyway, here is the modified code of what I mean:

program d9;
uses crt;
var
  m, c, h, l, t : integer;
  Ch: Char;
begin
clrscr;
c := 0;
t := 0;
h := -1;
l := 9999;
while (KeyPressed) do
  ReadKey(Ch);
write('Mark ? ');
readln(m);
while m <> -999 do
begin
c := c + 1;
t := t + m;
if m >= h then
h := m
else if m <= l then
l := m;
write('Mark ? ');
readln(m)
end;
writeln;
writeln('Average of the ', c ,' marks = ', t/c :0:0);
writeln('Highest mark = ', h);
writeln('Lowest mark = ', l);
end.
0
 

Author Comment

by:cassiohui
ID: 8196080
thanks, but i still can't run it on the quick pascal compiler that i have. the same error message pops up. anyway, thanks for everything!!
0
 
LVL 6

Expert Comment

by:MannSoft
ID: 8196215
I just found some reference material that indicates ReadKey works the same in Quick Pascal as it does in Turbo Pascal.  So the last thing I can think of is that Quick Pascal doesnt like you to ignore the function result, like in VB.  In that case you should change this:

while (KeyPressed) do
 ReadKey(Ch);

to

while (KeyPressed) do
  Ch := ReadKey;

And if that still doesnt work, then sorry but I am at a loss :-)
0
 
LVL 101

Expert Comment

by:mlmcc
ID: 8200241
I don't see anything wrong with your original code. Did you cut and paste it in? or retype it?

One thought
Add
writeln (m); after each readln to see what you input.

mlmcc
0
 

Author Comment

by:cassiohui
ID: 8200396
thank you MannSoft, but there's another problem i forgot to say. Then it prints the result (average, high and low) the low does not change (stays at 9999).

eg. :
Average mark of 5 marks = XX
Highest Mark = YY
Lowest Mark = 9999

Can you please solve this problem for me also, thank you
0
 

Author Comment

by:cassiohui
ID: 8202462
thank you MannSoft, but there's another problem i forgot to say. If you run the program, you enter the integers and stuff, and type in -999 to end the program. Then it prints the result (average, high and low). The problem is that the low does not change (stays at 9999).

eg. :
Average mark of 5 marks = XX
Highest Mark = YY
Lowest Mark = 9999

Can you please solve this problem for me also, thank you
0
 
LVL 6

Expert Comment

by:MannSoft
ID: 8202550
You need to change this code:

if m >= h then
h := m
else if m <= l then
l := m;

to this:

if m >= h then
h := m;

if m <= l then
l := m;

As you can see the only change was to remove the else.  The original code should have worked if you had typed in these grades in this order:

5, 4, 3, 2, 1

But if you typed them in this order:

1, 2, 3, 4, 5

then m would always be greater than h, and so the else portion where it checks if m is less than l never gets run.  It's also possible that you only enter one score, so it is both the highest and lowest, so you definitely need two distinct IF THEN's, and not an IF THEN ELSE
0
 
LVL 6

Accepted Solution

by:
MannSoft earned 400 total points
ID: 8202552
BTW, did that other code finally work and stop the double prompt?
0
 

Author Comment

by:cassiohui
ID: 8202705
thanks a lot!!
both new codes worked.
thank you very much!
0
 

Author Comment

by:cassiohui
ID: 8202753
MannSoft, if you don't mind and if it's not against the rules, can you give me your email address? i have something to ask you.
0
 
LVL 6

Expert Comment

by:MannSoft
ID: 8202835
To be honest I have no idea what the rules are here.  I dont think people are allowed to take questions into private messages, but this question has been answered, so that doesnt apply here.

So you can reach me via this web-form at http://www.mannsoft.ca/contact.asp 
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The well known Cerber ransomware continues to spread this summer through spear phishing email campaigns targeting enterprises. Learn how it easily bypasses traditional defenses - and what you can do to protect your data.
Introducing Priority Question, our latest feature.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question