Solved

I've got problems in this program..

Posted on 1998-12-30
7
221 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
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Knowledge base software has turned out to be a quite reliable method for storing information, promoting collaborative work and for sharing valuable input and solutions.However, some organizations are trying to develop a knowledge base that works wit…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

895 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

11 Experts available now in Live!

Get 1:1 Help Now