User-Input Validation

classic Classic list List threaded Threaded
2 messages Options
kalyansg kalyansg
Reply | Threaded
Open this post in threaded view
|

User-Input Validation

Hello,
I have written a simple code for User-Input-Validation. It seems to work. But for a wrong string input there is the error 4 (Undefined variable:).

Q1. Can this error be hidden?
Q2. Kindly let me know if I should use a better validation code.

Thanks for your help
Kalyan

CODE:

clear
clc
month = 0;


while (month<1 | month>6)
 
    month = input('Please chose your option between 1 and 6: '); if (type(month)==10) then month=0;end;//10 - represent string
   
end

select month
        case 1
            disp("Month is January")            
        case 2
            disp("Month is February")
        case 3
            disp("Month is March")
        case 4
            disp("Month is April")
        case 5
            disp("Month is May")
        case 6
            disp("Month is June")  
        else
            disp("Wrong Month number")              
           
end

disp(month)

_______________________________________________
dev mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/dev
kalyansg kalyansg
Reply | Threaded
Open this post in threaded view
|

Re: User-Input Validation

I think a better code would be:

clear
clc
month = 0;

while (month<1 | month>6)
 
    month = input('Please chose your option between 1 and 6: ', "string")
    month=strtod(month)
    if (isnan(month)) then month = 0;end
end

select month
        case 1
            disp("Month is January")            
        case 2
            disp("Month is February")
        case 3
            disp("Month is March")
        case 4
            disp("Month is April")
        case 5
            disp("Month is May")
        case 6
            disp("Month is June")  
        else
            disp("Wrong Month number")            
           
end

disp(month)




--
Sent from: http://mailinglists.scilab.org/Scilab-developers-Mailing-Lists-Archives-f2574944.html
_______________________________________________
dev mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/dev