C#: Optimizing error prevention with the input console -
i've started learning c# using rob miles' c# programming yellow book , related lab exercises website. did 1 of them , produced solution works...(there's not 1 provided). wanted not crash if user inputs nonsensical answers or string can't converted integer(i googled , found tryparse method. textbook briefly taught try/catch method, didn't seem useful user interface, i.e. let program continue after bad input). set that, in 1 of these cases, sends out alternate message. i've ended long code... do/while within do/while , do/while within while. if have tips streamlining this, i'd obliged.
best, elliot
using system; using system.io; static class cinema { static void main() { int[] selection = new int[] //array age requirements of each film { 15, 15, 12, 18, 0 }; string filmnumbertext; //console input film number int filmnumber; //input string parsed integer string agetext; //console input age int age; //parsed age string int agelimit; //age requirement selected film console.write("welcome our multiplex.\n\n"); console.writeline(@"we presently showing: 1. rush (15) 2. how live (15) 3. thor: dark world (12a) 4. filth (18) 5. planes (u)"); //loops long input not between 1 , 5 { //loops long input not integer { console.write("\nenter number of film wish see: "); filmnumbertext = console.readline(); } while (int.tryparse(filmnumbertext, out filmnumber) == false); } while (filmnumber < 1 || filmnumber > 5); filmnumber = filmnumber - 1; //changes input 1-5 0-4 agelimit = selection[filmnumber]; //selects age requirement array //loops long input not integer { console.write("\nenter age: "); agetext = console.readline(); } while (int.tryparse(agetext, out age) == false); // repeats if input not integer while (age < 0 || age > 125) //if integer is small or large... { { console.write("\ninvalid age. please enter age between 0 , 125: "); agetext = console.readline(); } while (int.tryparse(agetext, out age) == false); //check again if input integer } if (age < agelimit) //if young given film { console.writeline("\naccess denied - young"); } else { console.writeline("\nplease call our office @ 888-999-2928 reserve tickets."); } } }
i have tried remove while loop code. looking for?
public static void main() { int[] selection = new int[] //array age requirements of each film { 15, 15, 12, 18, 0 }; string filmnumbertext; //console input film number int filmnumber; //input string parsed integer string agetext; //console input age int age; //parsed age string int agelimit; //age requirement selected film console.write("welcome our multiplex.\n\n"); console.writeline(@"we presently showing: 1. rush (15) 2. how live (15) 3. thor: dark world (12a) 4. filth (18) 5. planes (u)"); //loops long input not between 1 , 5 { console.write("\nenter number of film wish see: "); filmnumbertext = console.readline(); } while (int.tryparse(filmnumbertext, out filmnumber) == false || (filmnumber < 1 || filmnumber > 5)); filmnumber = filmnumber - 1; //changes input 1-5 0-4 agelimit = selection[filmnumber]; //selects age requirement array //loops long input not integer { console.write("\nplease enter age between 0 , 125:"); agetext = console.readline(); } while (int.tryparse(agetext, out age) == false || (age < 0 || age > 125)); //check again if input integer if (age < agelimit) //if young given film { console.writeline("\naccess denied - young"); } else { console.writeline("\nplease call our office @ 888-999-2928 reserve tickets."); } console.read(); }
Comments
Post a Comment