[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
Solved

# I've got problems in this program..

Posted on 1998-12-30
Medium Priority
235 Views
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}

begin
write('ENTER THE NUMBER OF ITEMS: ');
for i := 1 to Num do
begin
write('ENTER NAME FOR ITEM ',i,' : ');
write('ENTER PRICE FOR ITEM ',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? ');
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
Amount := 0;
repeat
repeat
SortItem;
DisplayChoices;
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? ');
Until (Response <> 'Y') and (Response <> 'y')
end.
0
Question by:klai
• 5
• 2

LVL 1

Accepted Solution

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

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}

begin
write('ENTER THE NUMBER OF ITEMS: ');
for i := 1 to Num do
begin
write('ENTER NAME FOR ITEM ',i,' : ');
write('ENTER PRICE FOR ITEM ',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
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;
Amount := 0;
repeat
repeat
repeat;
SortItem;
DisplayChoices;
{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
Until (Choice <> i) ;
writeln;
write('ANOTHER CUSTOMER? ');
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

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}

begin
write('ENTER THE NUMBER OF ITEMS: ');
for i := 1 to Num do
begin
write('ENTER NAME FOR ITEM ',i,' : ');
write('ENTER PRICE FOR ITEM ',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
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;
Amount := 0;
repeat
repeat
repeat;
SortItem;
DisplayChoices;
{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
Until (Choice <> i) ;
writeln;
write('ANOTHER CUSTOMER? ');
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

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

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

ID: 1216728
what ever do you mean?
0

LVL 10

Expert Comment

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

Question has a verified solution.

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

Moore’s Law has proven itself time and time again since it was first introduced. So what’s next? Will Moore’s law continue to remain relevant, or will new technology take over and bring us the next big advancement in computing?
Tech giants such as Amazon and Google have sold Alexa and Echo to such an extent that they have become household names. And soon they are expected to be used by commoners in their homes, ordering takeout, picking out a song, answering trivia questio…
Integration Management Part 2
Screencast - Getting to Know the Pipeline
###### Suggested Courses
Course of the Month19 days, 20 hours left to enroll

#### 872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.