Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4095
Number of posts: 14004

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
turbo pascal 7 Posted by nova_strife on 6 Dec 2002 at 4:25 PM
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.
Report
Re: turbo pascal 7 Posted by nova_strife on 6 Dec 2002 at 4:32 PM
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.

Report
Re: turbo pascal 7 Posted by Manning on 6 Dec 2002 at 6:23 PM
This message was edited by Moderator at 2002-12-6 18:24:35

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:

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


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.

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


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

repeat
      // do stuff here

      Write('More Fines? [Y/n]: ');
      repeat
            Ch := UpCase(ReadKey);
      until (Ch in ['Y', 'N', #13]);
until (Ch = 'N');

Report
Re: turbo pascal 7 Posted by nova_strife on 6 Dec 2002 at 7:01 PM
This message was edited by nova_strife at 2002-12-6 19:1:44

*scratches his head* ummm....say what???


Report
Re: turbo pascal 7 Posted by Manning on 6 Dec 2002 at 9:32 PM
: *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.
Report
Re: turbo pascal 7 Posted by nova_strife on 7 Dec 2002 at 7:33 AM
: : *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??
Report
Re: turbo pascal 7 Posted by Manning on 7 Dec 2002 at 9:01 AM
: : : *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:

I := 1;
while (I <= 10) do
begin
     WriteLn(I);
     I := I + 1;
end;


I := 1;
repeat
      WriteLn(I);
      I := I + 1;
until (I > 10);


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

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


I := 100;
repeat
      WriteLn(I);
      I := I + 1;
until (I > 10);


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.
Report
Re: turbo pascal 7 Posted by nova_strife on 7 Dec 2002 at 10:57 AM
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!!



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.