フィルターのクリア

Weird problem in while loop: What is wrong with the input "48" compared to "47" and "49"????

1 回表示 (過去 30 日間)
magie
magie 2022 年 10 月 11 日
コメント済み: magie 2022 年 10 月 11 日
Hi everyone!
I have a weird problem in a while loop with user input:
Within the while loop, the user is asked for the input of a number to be assigned to the variable insnr:
1113 insnr = '0';
1114 while insnr == '0';
1115 insnr = input('Inselnummer [1 ... 999] ? ');
1116 answer = isempty (insnr);
1117 while answer == 1;
1118 disp('Gebe eine gültige Inselnummer ein [1 ... 999] !!!');
1119 insnr='0';
1120 answer = isempty (insnr);
1121 end
1122 end
The code works fine for any number but the number "48".
Execution when using e.g. with the input "47" is as follows:
line 1113 [insr =’0’ char] --> 1114 --> 1115 insnr=47 [insnr=47 double] --> 1116 [answer=0] --> 1122 --> proceed with code properly at line 1123
A check on the input variable insnr yields:
K>> whos(insnr)
Name Size Bytes Class Attributes
insnr 1x1 8 double
Execution when using "48" is magically as follows:
line 1113 [insr =’0’ char] --> 1114 --> 1115 insnr=47 [insnr=47 double] --> 1116 [answer=0] --> 1122 --> 1115 and keeps on returning to this line until I use a number different from 48, e.g. 49
if I check on the input variable insnr:
K>> whos(insnr)
Name Size Bytes Class Attributes
insnr 1x1 8 double
Hence, it looks o.k.
Any idea what could be the problem here with the number "48" ... maybe a Hitchhiker's Guide to the Galaxy issue???
Thanks in advance.
Best, Magie

採用された回答

Matt J
Matt J 2022 年 10 月 11 日
編集済み: Matt J 2022 年 10 月 11 日
It's because 48 is the ASCII code for '0'.
double('0')
ans = 48
char(48)
ans = '0'
48=='0'
ans = logical
1
  5 件のコメント
Matt J
Matt J 2022 年 10 月 11 日
Wow! Thanks a lot. That's the solution.
You're quite welcome, but please Accept-click the answer to indicate so.
magie
magie 2022 年 10 月 11 日
Accept-click done. Took some time to find the button.

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by