fprintf is outputting the wrong number, what's wrong with my code?

so to quickly summarize im trying to figure out how to make a budgeting code using the function program. the problem is that fprintf is outputting the wrong number. ill put the code below. what am i doing wrong?
function budget(x,y);
x = input('please give the budget for today:');
if isnumeric(x)
else
fprintf('Error: Input must be numeric.')
end
if isfinite(x)
else
fprintf('Error: Input must be a finite value.')
end
y = input('how much did you spend')
z = x-y;
if (y>x)
fprintf('the shopper went $%g over the budget.','z')
elseif (y>200)
fprintf('try to scale back the costs next week')
elseif (y<x)
fprintf('the shopper is $%g under budget','y')
elseif (x<=0)
fprintf('thats a great job budgeting')
elseif (y==x)
fprintf('the shopper is exactly on budget')
end

5 个评论

What is the purpose in defining a function with two input arguments, and then ignoring them both inside the function?
idk, its just what they wanted me to do for my homework
"its just what they wanted me to do for my homework"
So your homework specifically asked for you to define a function with two totally pointless input arguments? Very ... unusual.
Or did it actually ask you to define a function which accepts two input arguments, to be used in the rest of the function?
It just asked to right a function that puts out a fprintf based off the value of the input. I attached the homework. I may have read it wrong, which is a my bad.
"Create a function named budget that has one input and no outputs. The input will be the amount spent at the grocery store."

请先登录,再进行评论。

 采纳的回答

"what am i doing wrong?"
You are printing a character code, not the value of a numeric variable. Replace this:
fprintf('the shopper went $%g over the budget.','z')
% ^ ^ wrong: defines the character 'z'
with this:
fprintf('the shopper went $%g over the budget.',z)
% ^ refer to variable z
Ditto for y a few lines later.

2 个评论

@jacob parente: I am glad that it helped.
If you are new on this forum, please remember to accept the answer that helped you most. That is a simple, polite way to show your thanks to the volunteers who help you on this forum.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Matrix Indexing 的更多信息

产品

标签

Community Treasure Hunt

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

Start Hunting!

Translated by