Ok, so I got a program to do in TurboPascal but it's not doing the one of the requirements and I'm not sure how to get it so the question states: Write a program to read in the names of a sequence of products and their price. The program terminates when a 0 price is encountered for a product. The program should print the number of items purchased and a grand total. The data will be entered in the following order: Product,Price,Product,Price...

This is what I have so far:

program sequence;

uses wincrt;

var price,count,pre_sum,new_sum,sum,new_price:integer;product:string;

Begin

 count:=1;
 readln(Product);
 readln(Price);
 While price<>0 do
 Begin
      readln(Product);
      readln(Price);
      sum:=Price+Price;
      count:=count+1;
      Writeln('Grand Total: ',sum);
      Writeln('The number of items purchased is ',count);
 End;

End.

Dani AI

Generated

Quick diagnosis: the loop/control flow is the real problem, not TurboPascal magic. Your program was reading product/price pairs in a way that skipped or re-read values and it never used a persistent accumulator correctly; that produced wrong totals and an off-by-one item count. was on the right track — treat the grand total as an accumulator that you update for each nonzero price, and keep the item counter starting at zero.

A simple, robust pattern (repeat/until makes the sentinel check straightforward):

program Purchases;
uses crt;
var
  prod: string;
  price, total: Real;
  items: Integer;
begin
  total := 0.0;
  items := 0;
  repeat
    write('Product: '); readln(prod);
    write('Price: ');   readln(price);
    if price <> 0.0 then
    begin
      items := items + 1;
      total := total + price;    { update accumulator }
      writeln('Running total: ', total:0:2);
    end;
  until price = 0.0;
  writeln('Items purchased: ', items);
  writeln('Grand total: ', total:0:2);
end.

Troubleshooting tips and notes: initialize the accumulator and counter before the loop; avoid doing arithmetic that replaces the accumulator with the current price or doubles the same price; use a floating type (Real) for money and format with two decimals when printing; decide whether you want a running total printed each iteration (inside the loop) or only the final total (after the loop). Test with the exact sequences you mentioned (e.g., Ham 500, Chicken 500 => 2 items, 1000.00) and with a price of 0 as the sentinel to confirm the loop exits cleanly. Thanks to and for the pointers that led to this clearer pattern.

Recommended Answers

All 6 Replies

What's the problem?

It's suppose to add all the prices together, but once i go more than 2 prices for 2 products, it gives me incorrect numbers, im trying to get a grand total each time a product is entered for eg. Ham-500,Chicen-500 Grand Total=1000 or Ham-500,Chicken,200,Rice-300...Grand Total=1000 but it just wont add the third price with the previously entered prices

I should init sum to zero before the while and line 16 should read sum := sum + Price;
EDIT: ho, count should also be initialised to zero.

Ok, i'll try that thanks.

Your idea worked!!! Just that it needed a bit of adjusting :D(initializing sum:=Price;before while) thanks much man thanks!!! If you have the time could you check out my other post? Title is "This doesn't make sense"... Thanks again.

Nevermind, you guys already did :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.