-5

How do I convert the integer for hp as its giving me this error message: cannot convert from 'int' to 'string' [PokemonPocket]

   if (menu == '1') {
      // We, probably, want to add just once
      Console.Write("Enter Pokemon Name:");
      string name = Console.ReadLine();
      Console.Write("Enter Pokemon HP: ");
      int hp = Convert.ToInt32(Console.ReadLine());

      Console.Write("Enter Pokemon EXP: ");
      int exp = Convert.ToInt32(Console.ReadLine());
      if (myDictionary.Count <= 0) { 
        myDictionary.Add("Pokemon Name: ", name);
        myDictionary.Add("Pokemon HP: ", hp);
  • 2
    Have you tried googling exactly what you just said? It sounds like a question that should already have been asked and answered. E.g. [here](https://stackoverflow.com/questions/3081916/convert-int-to-string). If that does not answer your question, you can edit it to clarify what is different and it will be added into the reopen review queue – lucidbrot May 22 '21 at 12:05

1 Answers1

1

myDictionary appears to be a Dictionary<string, string> type. Seeing as you store "Pokemon Name:" and "name" just fine one line above.

As such, you can just call hp.ToString() to store it. Make sure to once again convert it back to int when using it though.

Alternatively, you can make a Dictionary<string, object> storage to keep track of your information and simply cast the values when you retrieve them:

string name = (string)myDictionary["Pokemon Name:"];
int hp = (int)myDictionary["Pokemon HP:"];
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Jack T. Spades
  • 986
  • 6
  • 9