Solved

I've got problems in this program..

Posted on 1998-12-30
7
220 Views
Last Modified: 2010-04-16
In the program below, I've some problems in the main program. I dunno why it runs in such a way that prints out what I don't expect to see. Sorry for posting so many messages, but I just want to learn coz I love programming
so much. {:^)
 

Program VendingMachine;
Var Choice, j, Num,i : integer;
    change : real;
    Response : char;
    Snack : array[1..50] of String;
    Price : array[1..50] of Real;
    Amount : REAL;
    TSnack : string;   {Temp Snack}
    TPrice : Real;     {Temp Price}

Procedure ReadItem;

begin
     write('ENTER THE NUMBER OF ITEMS: ');
     readln(Num);
     for i := 1 to Num do
         begin
              write('ENTER NAME FOR ITEM ',i,' : ');
              readln(Snack[i]);
              write('ENTER PRICE FOR ITEM ',i,' : ');
              readln(Price[i])
         end
end;

Procedure SortItem;            { Sort By Price }
var Pass : integer;
    NoExchange : boolean;
begin
    Pass := 1;
    repeat
          NoExchange := True ;
          for i := 1 to Num - Pass do
              if Price[i] > Price[i+1]
                 then begin
                           TPrice := Price[i];
                           TSnack := Snack[i];
                           Price[i] := Price[i+1];
                           Snack[i] := Snack[i+1];
                           Price[i+1] := TPrice;
                           Snack[i+1] := TSnack;
                           NoExchange := False
                      end;
          Pass := Pass + 1
    Until NoExchange = True
end;

Procedure DisplayChoices;
var ch:char;
    OutStr : string;
begin
     i := 1;
     if Amount = 0
        then begin
                  write('INSERT A $2 COIN BY ENTERING ANY CHARACTER? ');
                  readln(ch);
                  Amount := Amount + 2
             end;
     writeln;
     writeln('YOU HAVE INSERTED $',Amount:0:0);
     writeln('YOU MAY SELECT ONE OF THE FOLLOWING ITEMS:');
     while (Amount > Price[i]) and ( i <= Num) do
           begin
                Str(i, OutStr);
                OutStr := OutStr +' '+ Snack[i];
                for j:=1 to 14-Length(Snack[i]) do
                    OutStr := OutStr + ' ';
                writeln(OutStr,'$',Price[i]:0:1);
                i := i+1
           end;
     if Price[Num] > Amount
        then writeln(i,' INSERT $2 COIN')
        else i := i - 1;
     writeln(i+1,' CANCEL OPERATION AND REFUND!');
     writeln('PRESS 1-',i+1,' TO SELECT ITEM: ')
end;
 
 

begin
     ReadItem;
     Amount := 0;
     repeat
           repeat
           SortItem;
           DisplayChoices;
           readln(Choice);
           if Price[Choice] <= Amount
              { There is a problem here }
              { For example :
                After sorting,
                Price[4] > 2,
                then if i choose 4 'Cancel operation and refund!',
                it shouldn't run these lines, however,
                whenever i chose 'Cancel ... ', it runs these lines.
                What is the problem?
                (when choice = 'Insert $2 coin' is ok }
              then begin
                   change := Amount - Price[Choice];
                   writeln;
                   writeln('YOU HAVE SELECTED: ',Snack[choice],
                           '$',Price[Choice]:0:1);
                   writeln('YOUR CHANGE IS: $',Change:0:2);
                   writeln('THANK YOU!');
                   Amount := 0
                   end
              else if Choice = i
                      then Amount := Amount + 2
                      else Amount := 0
           Until (Choice <> i) ;
           writeln;
           write('ANOTHER CUSTOMER? ');
           readln(Response)
     Until (Response <> 'Y') and (Response <> 'y')
end.
0
Comment
Question by:klai
  • 5
  • 2
7 Comments
 
LVL 1

Accepted Solution

by:
The_Brain earned 0 total points
Comment Utility
Procedure SortItem;            { Sort By Price }
var Pass : integer;
    NoExchange : boolean;
begin
    Pass := 1;
    repeat
          NoExchange := True ;
          for i := 1 to Num - Pass do   {always do begin for multiple lines}
first problem 1.->  BEGIN {HERE IS THE FIRST PROBLEM}
{it was check the > num-pass times before it did the switch}
{in effect it did not loop through the switch four times. It looped this 4 times and did the switching only once provided the last > passed.}    

{what it looped -->}  if Price[i] > Price[i+1]
                 then begin
                           TPrice := Price[i];
                           TSnack := Snack[i];
                           Price[i] := Price[i+1];
                           Snack[i] := Snack[i+1];
                           Price[i+1] := TPrice;
                           Snack[i+1] := TSnack;
                           NoExchange := False
                      end;
              END;
          Pass := Pass + 1
    Until NoExchange = True
end;


well this should solve the problem, I have done a sort like this for alphabetical order.  If you are still having trouble then e-mail me at zd13@pipex.co.za


0
 
LVL 1

Expert Comment

by:The_Brain
Comment Utility
Here is the full Working program... jsut compare the diffences I have made some comments where I have tampered.  The main problem was little logic-errors.  Not too uncommon though, so don't feel bad. :)


{In the program below, I've some problems in the main program. I dunno why it runs in such a way that prints out what I don't
 expect to see. Sorry for posting so many messages, but I just want to learn coz I love programming
so much. {:^)


 }

Program VendingMachine;
uses crt;  {my include don't worry take it out if you want to, need clrscr}
const  LIMIT=10;  {the money limit, a vending machine is only THAT big. :)}

Var Choice, j, Num,i : integer;
       change : real;
       Response : char;
       Snack : array[1..50] of String;
       Price : array[1..50] of Real;
       Amount : REAL;
       TSnack : string;   {Temp Snack}
       TPrice : Real;     {Temp Price}

Procedure ReadItem;

begin
        write('ENTER THE NUMBER OF ITEMS: ');
        readln(Num);
        for i := 1 to Num do
                  begin
                          write('ENTER NAME FOR ITEM ',i,' : ');
                          readln(Snack[i]);
                          write('ENTER PRICE FOR ITEM ',i,' : ');
                          readln(Price[i])
                  end
end;

Procedure SortItem;            { Sort By Price }
var Pass : integer;
       NoExchange : boolean;
begin
       Pass := 1;
       repeat
                   NoExchange := True ;
                   for i := 1 to Num - Pass do
                          if Price[i] > Price[i+1]
                                then begin
                                                      TPrice := Price[i];
                                                      TSnack := Snack[i];
                                                      Price[i] := Price[i+1];
                                                      Snack[i] := Snack[i+1];
                                                      Price[i+1] := TPrice;
                                                      Snack[i+1] := TSnack;
                                                      NoExchange := False
                                           end;
                   Pass := Pass + 1
       Until NoExchange = True
end;

Procedure DisplayChoices;
var
       OutStr : string;
begin
        i := 1;
        if (Amount = 0)
              then begin
                  writeln('INSERT A $2 COIN BY ENTERING ''2'' ');
                         repeat
                              if readkey='2' then
                                          begin
                                                Amount := Amount + 2;
                                                writeln('You have inserted $',Amount:0:2);
                                          end
                                    else
                                      break;
                         until Amount>=LIMIT;

        writeln('YOU HAVE INSERTED $',Amount:0:2);{set precision correctly}
        writeln('YOU MAY SELECT ONE OF THE FOLLOWING ITEMS:');

        {another prob. your prob I think  THIS is the prob, this was part of the
        if amount=0 clause  you had you change this. to end;}
                  end;
        begin
        while (Amount > Price[i]) and ( i <= Num) do
                    begin
                               Str(i, OutStr);
                               OutStr := OutStr +' '+ Snack[i];
                               for j:=1 to 14-Length(Snack[i]) do
                                      OutStr := OutStr + ' ';
                               writeln(OutStr,'$',Price[i]:0:1);
                               i := i+1
                    end;
        if Price[Num] > Amount
              then writeln(i,' INSERT $2 COIN')
        else i := i - 1;
        writeln(i+1,' CANCEL OPERATION AND REFUND!');
        writeln('PRESS 1-',i+1,' TO SELECT ITEM: ')
      end;
end;



begin
        ClrScr;
        ReadItem;
        Amount := 0;
        repeat
              repeat
                    repeat;
                          SortItem;
                          DisplayChoices;
                          readln(Choice);
                          {my lines}
                          if (Choice=i)
                               then
                                    begin
                                          Amount:=Amount+2;
                                          writeln('You have inserted $', Amount:0:2);
                                    end;
                    until (choice<>i);

                    if Price[Choice] <= Amount
                          { There is a problem here }
                          { For example :
                               After sorting,
                               Price[4] > 2,
                               then if i choose 4 'Cancel operation and refund!',
                               it shouldn't run these lines, however,
                               whenever i chose 'Cancel ... ', it runs these lines.
                               What is the problem?
                               (when choice = 'Insert $2 coin' is ok }
                          then begin
                                     change := Amount - Price[Choice];
                                     writeln;
                                     writeln('YOU HAVE SELECTED: ',Snack[choice],
                                                      '$',Price[Choice]:0:1);
                                     writeln('YOUR CHANGE IS: $',Change:0:2);
                                     writeln('THANK YOU!');
                                     Amount := 0
                                     end
{                          else if Choice = i
                                           then Amount := Amount + 2
                                           {else Amount := 0 I WONDER ABOUT THIS}
                    Until (Choice <> i) ;
                    writeln;
                    write('ANOTHER CUSTOMER? ');
                    readln(Response)
        Until (Response <> 'Y') and (Response <> 'y')
end.

Write up:  there are a few errors, for one, the putting in money is a bit
strange, you should have it put in any amount, but I have made him put as
many $2 as he needs. Max $10 dollars.

0
 
LVL 1

Expert Comment

by:The_Brain
Comment Utility
Here is the full Working program... jsut compare the diffences I have made some comments where I have tampered.  The main problem was little logic-errors.  Not too uncommon though, so don't feel bad. :)


{In the program below, I've some problems in the main program. I dunno why it runs in such a way that prints out what I don't
 expect to see. Sorry for posting so many messages, but I just want to learn coz I love programming
so much. {:^)


 }

Program VendingMachine;
uses crt;  {my include don't worry take it out if you want to, need clrscr}
const  LIMIT=10;  {the money limit, a vending machine is only THAT big. :)}

Var Choice, j, Num,i : integer;
       change : real;
       Response : char;
       Snack : array[1..50] of String;
       Price : array[1..50] of Real;
       Amount : REAL;
       TSnack : string;   {Temp Snack}
       TPrice : Real;     {Temp Price}

Procedure ReadItem;

begin
        write('ENTER THE NUMBER OF ITEMS: ');
        readln(Num);
        for i := 1 to Num do
                  begin
                          write('ENTER NAME FOR ITEM ',i,' : ');
                          readln(Snack[i]);
                          write('ENTER PRICE FOR ITEM ',i,' : ');
                          readln(Price[i])
                  end
end;

Procedure SortItem;            { Sort By Price }
var Pass : integer;
       NoExchange : boolean;
begin
       Pass := 1;
       repeat
                   NoExchange := True ;
                   for i := 1 to Num - Pass do
                          if Price[i] > Price[i+1]
                                then begin
                                                      TPrice := Price[i];
                                                      TSnack := Snack[i];
                                                      Price[i] := Price[i+1];
                                                      Snack[i] := Snack[i+1];
                                                      Price[i+1] := TPrice;
                                                      Snack[i+1] := TSnack;
                                                      NoExchange := False
                                           end;
                   Pass := Pass + 1
       Until NoExchange = True
end;

Procedure DisplayChoices;
var
       OutStr : string;
begin
        i := 1;
        if (Amount = 0)
              then begin
                  writeln('INSERT A $2 COIN BY ENTERING ''2'' ');
                         repeat
                              if readkey='2' then
                                          begin
                                                Amount := Amount + 2;
                                                writeln('You have inserted $',Amount:0:2);
                                          end
                                    else
                                      break;
                         until Amount>=LIMIT;

        writeln('YOU HAVE INSERTED $',Amount:0:2);{set precision correctly}
        writeln('YOU MAY SELECT ONE OF THE FOLLOWING ITEMS:');

        {another prob. your prob I think  THIS is the prob, this was part of the
        if amount=0 clause  you had you change this. to end;}
                  end;
        begin
        while (Amount > Price[i]) and ( i <= Num) do
                    begin
                               Str(i, OutStr);
                               OutStr := OutStr +' '+ Snack[i];
                               for j:=1 to 14-Length(Snack[i]) do
                                      OutStr := OutStr + ' ';
                               writeln(OutStr,'$',Price[i]:0:1);
                               i := i+1
                    end;
        if Price[Num] > Amount
              then writeln(i,' INSERT $2 COIN')
        else i := i - 1;
        writeln(i+1,' CANCEL OPERATION AND REFUND!');
        writeln('PRESS 1-',i+1,' TO SELECT ITEM: ')
      end;
end;



begin
        ClrScr;
        ReadItem;
        Amount := 0;
        repeat
              repeat
                    repeat;
                          SortItem;
                          DisplayChoices;
                          readln(Choice);
                          {my lines}
                          if (Choice=i)
                               then
                                    begin
                                          Amount:=Amount+2;
                                          writeln('You have inserted $', Amount:0:2);
                                    end;
                    until (choice<>i);

                    if Price[Choice] <= Amount
                          { There is a problem here }
                          { For example :
                               After sorting,
                               Price[4] > 2,
                               then if i choose 4 'Cancel operation and refund!',
                               it shouldn't run these lines, however,
                               whenever i chose 'Cancel ... ', it runs these lines.
                               What is the problem?
                               (when choice = 'Insert $2 coin' is ok }
                          then begin
                                     change := Amount - Price[Choice];
                                     writeln;
                                     writeln('YOU HAVE SELECTED: ',Snack[choice],
                                                      '$',Price[Choice]:0:1);
                                     writeln('YOUR CHANGE IS: $',Change:0:2);
                                     writeln('THANK YOU!');
                                     Amount := 0
                                     end
{                          else if Choice = i
                                           then Amount := Amount + 2
                                           {else Amount := 0 I WONDER ABOUT THIS}
                    Until (Choice <> i) ;
                    writeln;
                    write('ANOTHER CUSTOMER? ');
                    readln(Response)
        Until (Response <> 'Y') and (Response <> 'y')
end.

Write up:  there are a few errors, for one, the putting in money is a bit
strange, you should have it put in any amount, but I have made him put as
many $2 as he needs. Max $10 dollars.

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Expert Comment

by:The_Brain
Comment Utility
sorry about the tabs, I really dispise this problem.
oh well you can tab it yourself, and read the code in the mean time, it will do you a world of good anyway.
 :)
0
 
LVL 10

Expert Comment

by:viktornet
Comment Utility
THE BRAIN!

That's not a problem if you do...

for whatever do
  if something then
     dowhatever....

you can do things like this in pascal...

for whatever do
  for whatever do
    for whatever do
      for whatever do
        for whatever do
          for whatever do
            for whatever do
              for whatever do
                 ShowMessage('WHATEVER');

so your suggestion doesn't fix the problem...in that case you simply can do as the person did...

fot whatever do
  if something then begin
    dowhatever
  end;

-Viktor
--Ivanov
0
 
LVL 1

Expert Comment

by:The_Brain
Comment Utility
what ever do you mean?
0
 
LVL 10

Expert Comment

by:viktornet
Comment Utility
You're the brain... YOu should know what I'm talking about... if not go learn pascal and then come back here to discuss...

I was just telling you that that's not a problem for pascal compared to C++ compiler...

you can do as many nested for loops as you want without including a BEGIN statment as long as you keep each next block with only one statment... If you have two statments in a row that oyu wanna do if something happens then you have to include the BEGIN statment....

-Viktor
--Ivanov
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

HOW TO: Connect to the VMware vSphere Hypervisor 6.5 (ESXi 6.5) using the vSphere (HTML5 Web) Host Client 6.5, and perform a simple configuration task of adding a new VMFS 6 datastore.
Find out what Office 365 Transport Rules are, how they work and their limitations managing Office 365 signatures.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now