What is wrong with my code? (Beginner's question)

1 view (last 30 days)
n
n on 29 Oct 2014
Commented: Image Analyst on 13 Nov 2014
Function 1:
function [x,y]=func1(x,y)
x = input('Enter how many tickets you would like: ');
y = input('Enter what level you would like to sit in: ');
end
Function 2:
function [z] = func2(x,y)
switch l
case y==1
z = 500*x;
case y==2
z = 350*x;
case y==3
z = 200*x;
end
Function 3:
function [x,y,z]=func3(x,y,z)
[x,y]=func1(x,y);
z=func2(x,y);
fprintf('It will cost $%.2f for %d tickets in level %d.','z','x','y');
end
I'd like to call upon these three functions in succession (this is how I tried to do it):
func1(x,y)
func2(x,y)
func3(x,y,z)
I'd like for only three lines to be printed (except I want the actual calculation to be printed rather than x,y,z):
Enter how many tickets you would like:
Enter what level you would like to sit in:
It will cost $z for x tickets in level y.
The first of these two lines come out fine, but the third prints random numbers in place of x,y,z. What am I doing wrong?

Answers (1)

James Tursa
James Tursa on 29 Oct 2014
Edited: James Tursa on 29 Oct 2014
:
x and y are outputs of this function, not inputs. So eliminate them from the calling arguments. E.g.,
function [x,y]=func1
func2:
You are not using the proper variable names. E.g., in your code you use l (a very bad choice for variable name btw because it is easily confused with 1) but this is not passed in with that name. Stick to the x and y variables that are passed in. Also, the switch syntax isn't quite right. Look at this link:
func3:
x, y, and z are outputs, not inputs. So eliminate them from the input argument list. E.g.,
function [x,y,z]=func3
[x,y]=func1;
Also, func3 calls the other two functions, so you only need one call from your part, not three:
[x,y,z]=func3;
And you will probably want to add a \n to the end of the fprintf format string to get a newline.
Finally, your current code is printing the characters 'z','x','y' as numbers, not the values of the variables z, x, and y. Remove those '. E.g.,
fprintf('It will cost $%.2f for %d tickets in level %d.\n',z,x,y);
  2 Comments
Image Analyst
Image Analyst on 13 Nov 2014
n's question (which he will probably delete like his others) saved here:
Function 1:
function [x,y]=func1(x,y)
x = input('Enter how many tickets you would like: ');
y = input('Enter what level you would like to sit in: ');
end
Function 2:
function [z] = func2(x,y)
switch l
case y==1
z = 500*x;
case y==2
z = 350*x;
case y==3
z = 200*x;
end
Function 3:
function [x,y,z]=func3(x,y,z)
[x,y]=func1(x,y);
z=func2(x,y);
fprintf('It will cost $%.2f for %d tickets in level %d.','z','x','y');
end
I'd like to call upon these three functions in succession (this is how I tried to do it):
func1(x,y)
func2(x,y)
func3(x,y,z)
I'd like for only three lines to be printed (except I want the actual calculation to be printed rather than x,y,z):
Enter how many tickets you would like:
Enter what level you would like to sit in:
It will cost $z for x tickets in level y.
The first of these two lines come out fine, but the third prints random numbers in place of x,y,z. What am I doing wrong?

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!