c# - Cannot implicitly convert type 'string' to 'int' -


i'm doing athlete program , want make int salarycounter = athletesalary.tostring when user enters salary of hired professional can subtract salary of athlete , print out how athlete have left when try program tells me "cannot implicitly convert type 'string' 'int' ". can me?

{     console.writeline("please enter first name of athlete");     string athletestring = console.readline();     console.writeline("please enter second name of athlete");     string athletestring2 = console.readline();     console.writeline("did type {0} {1}? press y yess n no.", athletestring.tostring(), athletestring2.tostring());     consolekeyinfo keyinfo = console.readkey();      if (keyinfo.keychar == 'y')     {         console.writeline("enter salary of {0} {1}", athletestring.tostring(), athletestring2.tostring());         string athletesalary = console.readline();         console.writeline("{0} right?", athletesalary.tostring());         consolekeyinfo rightathletesalary = console.readkey();         int salarycounter = athletesalary.tostring();          if (rightathletesalary.keychar == 'y')         {              console.writeline("ok. lets contiune.");              athletesalary = convert.tostring(salarycounter);              console.writeline(salarycounter);               int counter = 0;               console.writeline("{0} {1} {2}", athletestring.tostring(), athletestring2.tostring(), salarycounter.tostring());              console.writeline("enter hired help. max number of people five. press key start.");               while (counter < 5)              {                  console.readkey();                  console.writeline("please enter first name of hired help");                  string hiredhelpstring = console.readline();                  console.writeline("please enter last name of hired help");                  string hiredhelpstring2 = console.readline();                  console.writeline("did type {0} {1}? press y yess n no.", hiredhelpstring.tostring(), hiredhelpstring2.tostring());                  consolekeyinfo keyinfo5 = console.readkey();                   if (keyinfo5.keychar == 'y')                  {                      console.writeline("enter salary of {0} {1}", hiredhelpstring.tostring(), hiredhelpstring2.tostring());                      string hiredhelpsalary = console.readline();                      console.writeline("{0} right?", hiredhelpsalary.tostring());                      consolekeyinfo rightsalary = console.readkey();                       if (rightsalary.keychar == 'y')                      {                          console.writeline("ok. lets contiune.");                      }                      console.writeline("record proffesional? press y yess n no.");                      consolekeyinfo recordkey = console.readkey();                       if (recordkey.keychar == 'y')                      {                          counter = counter + 1;                          console.writeline("number of hired {0} paid {1}", counter, hiredhelpsalary);                          console.writeline("press key contiune.");                      }                      else                      {                          if (recordkey.keychar == 'n')                          {                              counter = counter - 1;                              console.writeline(" ok. lets try again. press key contiune.");                              console.readkey();                              console.writeline("please enter first name of hired help");                              string hiredhelpstring3 = console.readline();                              console.writeline(" please enter last name of hired help");                              string hiredhelpstring4 = console.readline();                              console.writeline("did type {0} {1}? press y yess n no.", hiredhelpstring.tostring(), hiredhelpstring2.tostring());                              consolekeyinfo keyinfo6 = console.readkey();                               if (keyinfo6.keychar == 'y')                              {                                  console.writeline("record proffesional? press y yess n no.");                                  consolekeyinfo recordkey1 = console.readkey();                                  if (recordkey.keychar == 'y')                                  {                                      counter = counter + 1;                                      console.writeline("number of hired {0} press key contiune", counter);                                      console.writeline("press key contiune.");                                  }                              }                          }                      }                             /*******************************************************************************************************/                             /************************************************************************************************************************************************************************************************************************************************/                         }                         else                         {                             if (keyinfo5.keychar == 'n')                             {                                 console.writeline(" ok. lets try again. press key contiune.");                                 console.readkey();                              }                         }                     }                     /*************************************************************************************************************************************************************************************************************************************/                     console.writeline("");                     console.writeline("athlete's name: {0} {1} number of hired {2}", athletestring.tostring(), athletestring2.tostring(), counter);                     console.readkey();               } 

it looks you're getting confused variable types. briefly;

string - stores information character character. compiler can't read numbers stored in strings.

int - stores whole numeric values can used in calculations.

your immediate compile problem comes line;

int salarycounter = athletesalary.tostring(); 

you're telling compiler take althetesalary string, call tostring() method gets string representation (this unnecessary when source string) , store result in integer.

you need parse string read out numeric value this;

int salarycounter = int.parse(athletesalary) 

though, whenever receive input directly user should code defensively, instead of int.parse, use tryparse. way if user enters 'bob' salary, can display appropriate error;

int salarycounter; while(!int.tryparse(athletesalary, out salarycounter) {     console.writeline("the salary should number, try again");     athletesalary = console.readline(); } 

also, can remove of calls .tostring(), variable string.


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -