crash - Preventing C# crashing with invalid user input -


this question has answer here:

brand new coding c#, ~2 weeks learning language. i've had idea mobile game rattling around in head on year , 1 of resolutions learn code year. i'm working way through intro c# book , having gotten basics down i'm toying coding old school text based adventure skills reinforcer. guess 3 part question 1) i'm trying code system user chooses value between 1-10 attribute types can't figure out how stop program crashing if user enters other int. 2) i'm trying code way game subtract original attribute value values of attributes assigned ensure user can't end more 25. 3) want include code @ bottom allows user start on if don't setup. below code:

        console.writeline("now must choose basic attributes!. have total of 25 points distribute on 5 core skills:");         console.writeline("strength, speed, intelligence, personality, , ingenuity.");         console.writeline("choose carefully! values high or low may impact ability escape world!");         int attributes = 25;         int strength = -1;         int speed = -1;         int intelligence = -1;         int personality = -1;         int ingenuity = -1;         console.writeline("attributes remaining: " + (attributes));                 {              console.write("please choose value strength (0-10): ");             string strengthastext = console.readline();             strength = convert.toint32(strengthastext);             if(strength > 10)             {                 console.writeline("you can't fast!");             }             else             {                 console.writeline("good choice, please choose next attribute.");                 attributes = 25 - strength;                 console.writeline("attributes remaining: " + (attributes));             }        }         while (strength < 0 || strength > 10 && strength <= attributes);          int attributes2 = (attributes - strength);                 {              console.write("please choose value speed (0-10): ");             string speedastext = console.readline();             speed = convert.toint32(speedastext);             if (speed > 10)             {                 console.writeline("you can't fast!");             }             else if (speed < attributes2)             {                 console.writeline("you not have enough remaining attributes fast. please choose again.");             }             else             {                 console.writeline("good choice, please choose next attribute.");                 attributes2 = (attributes2 - speed);                 console.writeline("attributes remaining: " + (attributes2));             }         }         while (speed < 0 || speed > 10 && speed <= attributes2);                  {             console.write("please choose value intelligence (0-10): ");             string intelligenceastext = console.readline();             intelligence = convert.toint32(intelligenceastext);             if (intelligence > 10)             {                 console.writeline("you can't smart!");             }             else             {                 console.writeline("good choice, please choose next attribute.");                 console.writeline("attributes remaining: " + (attributes - strength - speed - intelligence));             }         }         while (intelligence < 0 || intelligence > 10 && intelligence <= attributes);                  {             console.write("please choose value personality (0-10): ");             string personalityastext = console.readline();             personality = convert.toint32(personalityastext);             if (personality > 10)             {                 console.writeline("you can't charismatic!");             }             else             {                 console.writeline("good choice, please choose next attribute.");                 console.writeline("attributes remaining: " + (attributes - strength - speed - intelligence - personality));             }         }         while (personality < 0 || personality > 10 && personality <= attributes);                  {             console.write("please choose value ingenuity (0-10): ");             string ingenuityastext = console.readline();             ingenuity = convert.toint32(ingenuityastext);             if (ingenuity > 10)             {                 console.writeline("you can't creative!");             }             else             {                 console.writeline("good choice, please choose next attribute.");                 console.writeline("attributes remaining: " + (attributes - strength - speed - intelligence - personality - ingenuity));             }         }         while (ingenuity < 0 || ingenuity > 10 && ingenuity <= attributes);          console.writeline("congratulations, have chosen wisely.");         console.writeline();         console.writeline("your final attribute values are: ");         console.writeline();         console.writeline("strength     " + (strength));         console.writeline("speed        " + (speed));         console.writeline("intelligence " + (intelligence));         console.writeline("personaility " + (personality));         console.writeline("ingenuity    " + (ingenuity));         console.writeline();         console.writeline("please press key continue...");          console.readkey();         console.writeline("you begin journey awakening in pile of looks starship debris.");         console.readkey();     } } 

}

i apologize if messy, said i'm new gets. thank help!

  1. to stop program crashing when value other int use tryparse method:

    bool result = int32.tryparse(value,out number) //value input, number output if (result) {     //value converted  } else {     //value failed convert } 
  2. no need create second variable attributes2. need use first 1 , keep decrementing it. ensure can keep track of total of 25 attributes.

  3. wrap routine in method , call again if user wants start on this:

    string s = console.readline(); if (s == "start over") {     startover(); //this method contains whole routine } else {     //exit application or whatever want done here } 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -