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

Stephen23
Stephen23 2021년 3월 8일
편집: Stephen23 2021년 3월 8일
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
Stephen23
Stephen23 2021년 3월 8일
편집: Stephen23 2021년 3월 8일
"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."

댓글을 달려면 로그인하십시오.

 채택된 답변

Stephen23
Stephen23 2021년 3월 8일
편집: Stephen23 2021년 3월 8일
"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

this worked perfectly, thanks
@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에 대해 자세히 알아보기

제품

태그

질문:

2021년 3월 8일

댓글:

2021년 3월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by