how to make if accept letters?

5 vues (au cours des 30 derniers jours)
JJD
JJD le 28 Nov 2020
Commenté : Ameer Hamza le 28 Nov 2020
i know about 'if' code, but what i dont understand is why it tells me error?
error: 'y' undefined near line 33 column 16
error: called from
Code_6 at line 33 column 5
nam = input('Enter name: ', 's');
adrs = input('Enter address: ', 's');
num = input('Enter amount of purchase: $');
pur = input('Enter type of purchase (L for Laptop / D for Desktop): ', 's');
fprintf('\n')
switch pur
case {'L' 'l'}
disp('You have choosen Laptop as your type of puchase')
if num <250
disc = (0 / 100) * num;
net = num - disc;
elseif num <570
disc = (5 / 100) * num;
net = num - disc;
elseif num <1000
disc = (7.5 / 100) * num;
net = num - disc;
elseif num >1000
disc = (10 / 100) * num;
net = num - disc;
endif
fprintf('Name: %s\n', nam)
fprintf('Address: %s\n', adrs)
fprintf('Net amount: $%.2f \n', net)
#promt user to continue or not
yesno = input('Do you wish to continue purchase? (y/n)', 's')
fprintf('\n')
if yesno = y;
Code_6
else
disp('You have exit purchase screen, Goodbye.')
endif
  1 commentaire
per isakson
per isakson le 28 Nov 2020
Modifié(e) : per isakson le 28 Nov 2020
Firstly, replace endif by end. And it's a good habit to have an else-clause in every if-end statement.

Connectez-vous pour commenter.

Réponse acceptée

Ameer Hamza
Ameer Hamza le 28 Nov 2020
Several problem:
1. = is an operator for assignment in MATLAB. For comparison, you need to use ==
if yesno == y
2. You want to compare the value to character 'y', just writing y means that you are referring to a variable
if yesno == 'y'
3. It is more of a suggestion, but it us better to use strcmp() for char arrays
if strcmp(yesno,'y')
  2 commentaires
JJD
JJD le 28 Nov 2020
thank you, this helps so much
Ameer Hamza
Ameer Hamza le 28 Nov 2020
I am glad to be of help!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by