Matlab share algorithm

I was given the task of matlab. but my function cannot run and gives an error of "Undefined function or variable 'b'." can someone help me ?

Cindy uses the services of a brokerage firm to buy and sell stocks. The firm charges 1.5% service
charges on the total amount for each transaction, buy or sell. When Cindy sells stocks, she would
like to know if she gained or lost on a particular investment. Write a program that allows Cindy
to input the number of shares sold, the purchase price of each share, and the selling price of
each share. The program outputs the amount invested, the total service charges, amount gained
or lost, and the amount received after selling the stock.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  share = input('What is your share price? ');
option = input('Do you wish to buy or sell? <b/s> ');

if (option == 'b' || option == 'B')
    buy = input('How many units do you wish to purchase? ');
    totbuy = share*buy;
    netbuy = totbuy - 0.015*totbuy;
    fprintf('Your total share purchased is %f, totbuy');
    fprintf('Your net share purchased is %f, netbuy');
elseif(option == 's' || option == 'S')
    sell = input('How many units do you wish to sell? ');
    totsell = share*sell;
    netsell = totsell - 0.015*totsell;
    fprintf('Your total share sold is %f, totsell');
    fprintf('Your net share sold is %f, netsell');
else
    disp('Transaction invalid');
end
Last edited on
okay, I have already settled the problem. But now I have a new problem. Whatever value I enter for shareprice, it will be decimal. How can I make it to be float ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

count = 'y';

while(count == 'y')

currentshare = input('\nWhat is your current share price?: RM');
option = input('\nDo you wish to buy or sell? <b/s>: ', 's');

switch (option)
    
    case 'b'
        buy = input('\nHow many units do you wish to purchase?: ');
        totbuy = share*buy;
        netbuy = totbuy - 0.015*totbuy;
        fprintf('Your total share purchased is RM%.2f', totbuy);
        fprintf('\nYour net share purchased is RM%.2f', netbuy);
    case 's'
        sell = input('\nHow many units do you wish to sell?: ');
        totsell = share*sell;
        initialshare = input('What is your initial share value?: ');
        netsell = (totsell - initialshare) - 0.015*(totsell - initialshare);
        fprintf('Your total share sold is RM%.2f', totsell);
        fprintf('\nYour net share sold is RM%.2f', netsell);
    otherwise
    disp('Transaction invalid');
end

count = input('\n\nDo you have another transaction? <y/n> ', 's');

end
Last edited on
closed account (48T7M4Gy)
float is decimal in c++
okay. then what is %d ? i thought it was supposed to stand for decimal ? what should I change so that my currentshare can read float number ?
Topic archived. No new replies allowed.