フィルターのクリア

How to count and display the number of input?

10 ビュー (過去 30 日間)
Roxanne Esguerra
Roxanne Esguerra 2020 年 7 月 7 日
コメント済み: Roxanne Esguerra 2020 年 7 月 12 日
Hi, I need help with displaying the count of the input given in the error message.
Here is my code,
function num = posnum;
num = input('Enter a positive number: ');
n = nargin;
while num<0
fprintf('Error # %.0f : Follow Instructions!\n',n)
fprintf('Does %.2f look like a positive number to you?\n',num)
num = input('Enter a positive number: ');
if num>0
squarerootvalue = sqrt(num);
end
end
end
My code displays:
>> squarerootvalue = posnum
Enter a positive number: -8
Error # 0 : Follow Instructions!
Does -8.00 look like a positive number to you?
Enter a positive number: -7
Error # 0 : Follow Instructions!
Does -7.00 look like a positive number to you?
Enter a positive number: 4
squarerootvalue =
4
However, it should display Error #1, then Error#2, Error#3, and so on...
Also, the final answer must be the square root of the input number...
What changes must be done to code?
Thanks in advance!
  1 件のコメント
dpb
dpb 2020 年 7 月 7 日
function num = posnum;
num = input('Enter a positive number: ');
n = nargin;
while num<0
fprintf('Error # %.0f : Follow Instructions!\n',n)
...
Reread the documentation for nargin -- it provides only the number of arguments with which a function is called -- and your function has no arguments so it rightfully returns 0--and if you tried to pass an argument, the call to the function would fail for that reason.
You're not interested in how many arguments are being passed to the function at all.
You'll need to keep an internal counter and increment it in the error section of the code.
Also, the above code could never output the last line shown -- the trailing semicolon suppresses output and there's nothing else to output anything -- and it surely appears the answer this case should be 2.

サインインしてコメントする。

採用された回答

Monisha Nalluru
Monisha Nalluru 2020 年 7 月 10 日
nargin returns the number of input arguments given to a function while executing.
So, in the above problem you have passed zero input arguments. This is the reason why it is displayed as Error # 0 : Follow Instructions! Every time.
And the output of the function is just the number you passed as input. You found the square root value but not assigned as output.
You can make use of following example
function squarerootvalue = posnum
num = input('Enter a positive number: ');
i=0;
while num<0
i=i+1;
fprintf('Error # %.0f : Follow Instructions!\n',i);
fprintf('Does %.2f look like a positive number to you?\n',num);
num = input('Enter a positive number: ');
end
squarerootvalue=sqrt(num); % Output of function which is squarerrotvalue
end
Hope this helps!
  1 件のコメント
Roxanne Esguerra
Roxanne Esguerra 2020 年 7 月 12 日
This worked, thank you so much!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by