where is my fault?

6 views (last 30 days)
Irem Deniz
Irem Deniz on 24 Jan 2021
Commented: Irem Deniz on 24 Jan 2021
Age=input("Age of the person is:","s");
find (Age<16);
disp(["Sorry-You will have to wait. You are only " Age])
find (Age>=16)&(Age<18);
disp(["You may have a youth license because you are " Age])
find (Age>=18)&(Age<=70);
disp(["You may have license because you are " Age])

Answers (1)

Steven Lord
Steven Lord on 24 Jan 2021
The find function just tells you which elements of its input are true. It doesn't actually select if the next line of code should run or should be skipped.
There's no need for the find function here except the homework problem requires it. But find alone will not do the job. See the help for if as well.
Finally, in your input statement you don't want to use "s".
>> age1 = input("Enter your age: ", "s")
Enter your age: 19
age1 =
'19'
>> age2 = input("Enter your age: ")
Enter your age: 19
age2 =
19
>> whos age1 age2
Name Size Bytes Class Attributes
age1 1x2 4 char
age2 1x1 8 double
You don't want the age as text (age1) you want it as a number (age2).
  7 Comments
Steven Lord
Steven Lord on 24 Jan 2021
You're almost there. The only problem is the "s" in your input call.
find('17' < 18)
ans = 1×0 empty double row vector
Why? Because the ASCII value of the character '1' is greater than 18, as is the ASCII value of the character '7'.
double('1')
ans = 49
double('7')
ans = 55
You can accept the value the user inputs as text (by passing the "s" option to input) then convert it to a number for use in the find statements and use the text form in your disp statements. Alternately you can accept the value the user inputs as a number (no "s" option), use that number in the find statement, and convert it to text for use in disp. Since you're working with a string array the latter is IMO easier.
age = 17;
disp(["Happy birthday! You are " + age + " years old today!"])
Happy birthday! You are 17 years old today!
Irem Deniz
Irem Deniz on 24 Jan 2021
How can I make it give results by comparing between inputs I have entered?

Sign in to comment.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!