turbo pascal 7 - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# turbo pascal 7

Posts: 16Member
alright guys, here's the deal.

highschool, takin pascal (already took vb), started working on one of the projects from the book, and i've gotta present it along with the rest of the class on tuesday. i won't have much oppurtunity to work on it on monday. so i'm turning to you guys for help.

we've gotta make a program that will calculate the amount of fines for over due librbary books, but i can't get the do/while loop to work. i know this isn't much info, so if anyone thinks they can help, just say so and i can be able to put up ALL my illogical, totaly unorganized, most likely un-needed code up for all to see...thanks in advance for all help.

## Comments

• Posts: 16Member
[code]
program library_fines;
{12/05/02, Library Fines program, chapter 6 problem # 5
This program will calculate the amount due for overdue books.}

var
days_late : integer;
yes_or_no, choice, choice2 : string;
overall_tot, total, paper_tot, other_tot, maga_tot, ency_tot, other2_tot : real;

begin
writeln('What class of books are late?');
writeln('General Magazines Reference');
writeln('paperbacks ---------- encyclpedias');
writeln('other general books ---------- other reference books');
writeln;
writeln('Your selection: ');
readln(choice);
if choice = 'General' then
begin
writeln('What type of general book is late?');
writeln('1. Paperback book');
writeln('2. Other general book');
writeln;
writeln('Your selection: ');
readln(choice2);
if choice2 = '1' then
begin
yes_or_no = 'yes' do
begin
writeln('How many days are the paperback books late?');
writeln;
writeln('Your selection: ');
readln(days_late);
paper_tot:= paper_tot + (days_late * 0.15);
writeln('The amount due for paperback books is \$', paper_tot:4:2);
writeln('Are there any more fines to calculate? ');
readln('yes_or_no);
end; {do while loop}
end; {choice2 if then statement}
if choice2 = '2' then
begin
while yes_or_no = 'yes' do
begin
writeln('How many days are the books late?');
writeln;
writeln('Your selection: ');
readln(days_late);
paper_tot:= paper_tot + (days_late * 0.20);
writeln('The amount due for books is \$', other_tot:4:2);
writeln('Are there any more fines to calculate? ');
readln(yes_or_no);
end; {do while loop}
end; {choice2 if then statement}
end; {general book if then statement}

if choice = 'Magazines' then
begin
while yes_or_no = 'yes' do
begin
writeln('How many days late are the magazines?');
writeln;
writeln('Your selection: ');
readln(days_late);
maga_tot:= maga_tot + (days_late * 0.25);
writeln('The amount due for magazines is \$', maga_tot:4:2);
writeln('Are there any more fines to calculate? ');
readln('yes_or_no);
end; {do while loop}
end; {magazine if then staement}
if choice = 'Reference' then
begin
writeln('What type of reference book is late?');
writeln('1. Encyclopedia');
writeln('2. Other reference book');
writeln;
writeln('Your selection: ');
readln(choice2);
if choice2 = '1' then
begin
while yes_or_no = 'yes' do
begin
writeln('How many days late are the encyclopedias?');
writeln;
writeln('Your selection: ');
readln(days_late);
ency_tot:= ency_tot + (days_late * 0.50);
writeln('The amount due for encyclopedias is \$', ency_tot:4:2);
writeln('Are there any more fines to calculate? ');
readln(yes_or_no);
end; {do while loop}
if choice2 = '2' then
begin
while yes_or_no = 'yes' do
begin
writeln('How many days late are the books?');
writeln;
writeln('Your selection: ');
readln(days_late);
other2_tot:= other2_tot + (days_late * 0.35);
writeln('The amount due for books is \$', other2_tot:4:2);
writeln('Are there any more fines to calculate? ');
readln(yes_or_no);
end; {do while loop}
end; {reference book if then statement}
overall_tot:= paper_tot + other_tot + maga_tot + ency_tot + other2_tot;
writeln('The overall total is \$', overall_tot:4:2);
readln;
end.
[/code]
• Posts: 1,621Member
[b][red]This message was edited by Moderator at 2002-12-6 18:24:35[/red][/b][hr]
In pascal (and probably most any other language) there are two styles of loops. One which does the evaluation first and then executes the block of code if necessary, and then one that always executes the block of code at least once and does the evaluation at the end. Here are two examples (from the TP7 IDE)

Evaluation is done at the beginning. So for instance, if you were already at the end of InFile before this loop, this block of code would never be executed:

[code]
while not Eof(InFile) do
begin
ReadLn(InFile, Line);
WriteLn(OutFile, Line);
Inc(LineCount);
end;
[/code]

Evaluation is done at the end. So for instance if I was set to 5 before this loop, the block of code would still get executed.

[code]
repeat
Write('Enter value: ');
ReadLn(I);
until (I >= 0) and (I <= '9');
[/code]

From what I can see from your code you want to use the latter method. Something like:

[code]
repeat
// do stuff here

Write('More Fines? [Y/n]: ');
repeat
Ch := UpCase(ReadKey);
until (Ch in ['Y', 'N', #13]);
until (Ch = 'N');
[/code]
• Posts: 16Member
[b][red]This message was edited by nova_strife at 2002-12-6 19:1:44[/red][/b][hr]
*scratches his head* ummm....say what???

• Posts: 1,621Member
: *scratches his head* ummm....say what???

Im not sure what kind of reply you expect to that. You said in your original message that you were having problems with the do..while loop, so I gave you two examples showing the two different looping styles, and then an example that applies to your program.
• Posts: 16Member
: : *scratches his head* ummm....say what???
:
: Im not sure what kind of reply you expect to that. You said in your original message that you were having problems with the do..while loop, so I gave you two examples showing the two different looping styles, and then an example that applies to your program.
:

heh...alright...umm...what i don't get is that infile, outfile, eOF stuff...what is all that??
• Posts: 1,621Member
: : : *scratches his head* ummm....say what???
: :
: : Im not sure what kind of reply you expect to that. You said in your original message that you were having problems with the do..while loop, so I gave you two examples showing the two different looping styles, and then an example that applies to your program.
: :
:
:
: heh...alright...umm...what i don't get is that infile, outfile, eOF stuff...what is all that??
:

EOF you can lookup in the help file, InFile and OutFile are just variable names. Here are some more basic examples:

[code]
I := 1;
while (I <= 10) do
begin
WriteLn(I);
I := I + 1;
end;
[/code]

[code]
I := 1;
repeat
WriteLn(I);
I := I + 1;
until (I > 10);
[/code]

You'll see both of these loops do the exact same thing. Another example to see how they differ would be:

[code]
I := 100;
while (I <= 10) do
begin
WriteLn(I);
I := I + 1;
end;
[/code]

[code]
I := 100;
repeat
WriteLn(I);
I := I + 1;
until (I > 10);
[/code]

Nothing has changed here except the starting value of I, but this time you will see the first loop does not print anything because the evaluation happens up front, while the second loop prints the number 100 and then stops because the evaluation is made at the end.
• Posts: 16Member
ummm..k...thanks...i think i may be able to decipher all this in my head and make this dumb project work...thanks alot man!!
Sign In or Register to comment.