1

I am currently learning Pascal. Using Pascal XE for IDE. This program I wrote runs but wouldn't read the data from stocks.txt

The Current output when the program is reading a .txt file with no data

The Current output when the program is reading a .txt file with data

program StockBroker;
uses crt, sysutils;

type
  Stock = record
    name: string;
    volume: integer;
    price: real;
  end;

var
  stocks: array [1..5] of Stock;
  cash: real;
  choice: integer;
  quantity: integer;
  filename: string;
  infile: text;

procedure LoadStocksFromFile(filename: string);
var
  i: integer;
begin
  ASSIGN(infile, 'C:\Academic_Development\infile.txt');
  reset(infile);
  for i := 1 to 5 do
  begin
    readln(infile, stocks[i].name, stocks[i].volume, stocks[i].price);
  end;
  close(infile);
end;

procedure DisplayStocks();
var
  i: integer;
begin
  writeln('Stocks available:');
  for i := 1 to 5 do
  begin
    writeln('Name: ', stocks[i].name, ' (', stocks[i].volume, '): $', stocks[i].price:0:2);
  end;
end;

procedure BuyStock();
begin
  writeln('Which stock would you like to buy?');
  DisplayStocks();
  readln(choice);
  writeln('How many shares would you like to buy?');
  readln(quantity);
  if (stocks[choice].volume >= quantity) and (stocks[choice].price * quantity <= cash) then
  begin
    stocks[choice].volume := stocks[choice].volume - quantity;
    cash := cash - stocks[choice].price * quantity;
    writeln('Bought ', quantity, ' shares of ', stocks[choice].name, ' for $', stocks[choice].price * quantity:0:2);
  end
  else
  begin
    writeln('Not enough shares or cash available');
  end;
end;

procedure SellStock();
begin
  writeln('Which stock would you like to sell?');
  DisplayStocks();
  readln(choice);
  writeln('How many shares would you like to sell?');
  readln(quantity);
  if (stocks[choice].volume + quantity <= 10000) then
  begin
    stocks[choice].volume := stocks[choice].volume + quantity;
    cash := cash + stocks[choice].price * quantity;
    writeln('Sold ', quantity, ' shares of ', stocks[choice].name, ' for $', stocks[choice].price * quantity:0:2);
  end
  else
  begin
    writeln('Cannot sell more shares than currently available');
  end;
end;

begin
  cash := 10000;
  filename := 'C:\Academic_Development\stocks.txt';
  LoadStocksFromFile(filename);
  repeat
    writeln('What would you like to do?');
    writeln('1. View current stocks and prices');
    writeln('2. Buy stocks');
    writeln('3. Sell stocks');
    writeln('4. Exit');
    readln(choice);
    case choice of
      1: DisplayStocks();
      2: BuyStock();
      3: SellStock();
            4: exit;
    end;
    writeln('Cash in hand: $', cash:0:2);
    writeln;
  until choice = 4;
    halt;
end.

This is stocks.txt

Apple 100 125.50 
Microsoft 75 250.00 
Amazon 50 1900.25 
Tesla 25 800.50 
Google 125 1500.75 

This Pascal program is supposed to display a menu. The user has $10,000 and that user has the choices of

  1. Buying Stocks
  2. Selling Stocks
  3. View the current state - of all the stocks - volume and price, and total value of the stock Then prints the current portfolio of the broker. The stocks held by broker, and cash in hand.
  4. Exiting the program.
Pickles91
  • 21
  • 6
  • 1
    Did you notice that your `LoadStocksFromFile` procedure takes a `filename` argument but there isn't any reference to `filename` in the body of the procedure? – hobbs Mar 26 '23 at 01:50
  • Yeah, I did have ASSIGN(infile, filename); -But the program wont run if there is a file with data in it. – Pickles91 Mar 26 '23 at 01:52
  • I think it is a runtime error but Pascal XE wont show the issue. – Pickles91 Mar 26 '23 at 02:53
  • Read documentation about IOResult. Then turn off I/O checking with "(*$I-*)" (compiler directive) and check IOResult after every I/O. – linuxfan says Reinstate Monica Mar 26 '23 at 07:52
  • Does this answer your question? [Pascal ReadLn multiple var types](https://stackoverflow.com/questions/8250924/pascal-readln-multiple-var-types) – Tom Brunberg Mar 27 '23 at 12:25

0 Answers0