How can I fix this code?

1 vue (au cours des 30 derniers jours)
Christian Baldie
Christian Baldie le 17 Oct 2019
I am trying to make a program where it askes the user to input the price and quantity of a first item in one line, then the price and quanitity of the second item in one line. It then wants us to display the results in a table form using fprintf. The price column should be take up 15 spaces from the left margin and the Quantity column should take up 12 spaces starting from where the Price column ends. The values should begin below the first letter in each column.
I have no idea how to get the spacing between the numbers inputted. Is there anyway I can do this utilizing fprintf only?
This is my code:
disp('Please input the price and quantity of the first item');
pq1=input('', 's');
disp('Please input the price and quantity of the second item');
pq2=input('', 's');
fprintf('% 15s', 'PRICE');fprintf('% 12s\n', 'QUANTITY');
fprintf('% 15s\n', pq1);
fprintf('% 15s\n', pq2);

Réponses (1)

Walter Roberson
Walter Roberson le 17 Oct 2019
fprintf('%-15s%-12s\n', 'PRICE', 'QUANTITY');
fprintf('%-15.2f%-12f', pq1, pq2);
This likely to be disconcerting to the users, as it will not align decimal points on the price, because of the requirement that the value start under the first letter of the header.
PRICE QUANTITY
0.37 123.000000
15.95 5000.000000
Notice that the first characters of the numbers align as is required by the problem, but that this is not how people would prefer to see information.

Catégories

En savoir plus sur Standard File Formats dans Help Center et File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by