Solved

I've got problems in this program..

Posted on 1998-12-30
7
227 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
[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
  • 5
  • 2
7 Comments
 
LVL 1

Accepted Solution

by:
The_Brain earned 0 total points
ID: 1216723
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
ID: 1216724
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
ID: 1216725
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
Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

 
LVL 1

Expert Comment

by:The_Brain
ID: 1216726
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
ID: 1216727
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
ID: 1216728
what ever do you mean?
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1216729
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

If you troubleshoot Outlook for clients, you may want to know a bit more about the OST file before doing your next job. IMAP can cause a lot of drama if removed in the accounts without backing up.
Let’s face it: one of the reasons your organization chose a SaaS solution (whether Microsoft Dynamics 365, Netsuite or SAP) is that it is subscription-based. The upkeep is done. Or so you think.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

728 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