Why does the error message invalid input 'c' keep occuring?
12 views (last 30 days)
Show older comments
Why does this invalid input error message keep occuring? I have tried it a few different ways and keep getting the same error message.
disp('This program will calculate the volume of a pyramid.');
length=input('Enter the length of the base: ');
dec=input('Is that i or c? ');
if dec==i
length=length*2.54;
else dec==c
length=length;
end
width=input('Enter the width of the base: ');
dec=input('Is that i or c? ');
if dec==i
width=width*2.54;
else
width=width;
end
height=input('Enter the height of the base: ');
dec=input('Is that i or c? ');
if dec==i
height=height*2.54;
else
height=height;
end
volume=((1/3)*(length*width)*height);
x=num2str(volume);
disp(['The volume of the pyramid is ',x,' cubic inches.']);
3 Comments
Stephen23
on 11 Sep 2014
Don't use length as the name of a variable, it is already the name of a MATLAB function... doing this can cause all kinds of difficult-to-fix bugs. This is a bad habit that will come back to bite you later...
Answers (1)
Geoff Hayes
on 11 Sep 2014
Richard - your above code should be formatted so that it is readable. Highlight the code portions and press the {} Code button to do so. As well, if there is an error being generated then please include all information concerning this error, in particular the line at which the error is being raised.
I took your code and ran it, and did not observe the error that you mentioned. I'm guessing that when the code asks Is that i or c? then what is being asked is whether the previous input is in inches or centimetres. The user then enters an "i" or "c" and continues. At this point, you should step through the code to see what is happening. If the user enters an i then that is being read in as a number and not a string, and so is being considered as the representation for the imaginary number: 0.0000 + 1.0000i. For this type of question, since you are asking the user to enter a string/character, then you should read it as such. So for example, the first time this happens
dec=input('Is that i or c? ');
if dec==i
length=length*2.54;
else
dec==c
length=length;
end
should be replaced with
decStr=input('Is that i or c? ','s');
if strcmpi(decStr,'i')
length=length*2.54;
end
The above adds the 's' option so that the input is read as a string only (without any evaluation) and then we just compare it with the character 'i'. If it evaluates to true, then we convert it from inches to centimetres. There is no need for an else statement as length is already set. Note also how I've removed dec==c as it wasn't doing anything.
This same idea should be applied to the remaining two (similar) questions for width and height.
You display the final result as
disp(['The volume of the pyramid is ',x,' cubic inches.']);
Should this be cubic centimetres instead, since you have convert all inch inputs to centimetres?
2 Comments
Geoff Hayes
on 11 Sep 2014
Glad the above helped.
Make sure you follow Stephen's comment too concerning the naming of your variable length and the built-in MATLAB function of the same name.
See Also
Categories
Find more on Animation 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!