My Pascal Program wont read the data from stocks.txt. How do I have it read the data from data.txt

90 Views Asked by At

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.
1

There are 1 best solutions below

0
Scooter On

The answer that Tom Brunberg linked to in a comment shows that if the string is the first variable in the line it shoves the whole text line into the (in this case) name field of the record. It worked for me in Free Pascal putting the string at the end of your data file and adjusting the Readln accordingly.

Readln(infile,stocks[i].volume, stocks[i].price,  stocks[i].name );
    100 125.50 Apple
    75 250.00 Microsoft
    50 1900.25 Amazon
    25 800.50 Tesla
    125 1500.75 Google

If the data file has to remain as it was with the name first, that will apparently require reading a file line into a string, and looping through the string to pull out the different fields into 3 strings. And then using StrToInt and StrToFloat to convert the string fields representing volume and price into Integer and Real values. The answer linked above also had another method where you insert a special character like "ctrl-Z" after the name field in the text file.