title: switch statement
body: <p>I'm practising the "Switch Loop" in a program. And I'm making a code where a user can input the integer and after the user will input the integer it will also display what the user just typed in. Now I'm trying to implement where the program will ask the user to input the number again by selecting the Y/N.</p>
  
  <p>I already included it here in my code but if I type characters the first time the program asks me to type an integer, the program will execute the catch part. How can I make it that if the user will type a character it will also display the message again, "please enter the integer:"</p>
  
  <pre><code>          int enterYourNumber;
            char shortLetter;
  
              try
              {
                  Console.WriteLine("Please enter the integer: ");
                  enterYourNumber = Convert.ToInt32(Console.ReadLine());
                  WriteNumber(enterYourNumber);
  
                  Console.WriteLine("Do you still want to enter a number? Y/N");
                  shortLetter = Convert.ToChar(Console.ReadLine());
  
                  while (shortLetter == 'y' || shortLetter == 'Y')
                  {
                      Console.WriteLine("Please enter the integer: ");
                      enterYourNumber = Convert.ToInt32(Console.ReadLine());
                      WriteNumber(enterYourNumber);
  
                      Console.WriteLine("Do you still want to enter a number? Y/N");
                      shortLetter = Convert.ToChar(Console.ReadLine());
                  }
  
              }
              catch
              {                                        
                  Console.WriteLine("Please enter an integer not a character");
              }
          }
  
              public static void WriteNumber(int wordValue)
              {
  
              switch (wordValue)
              {
                  case 1:
                      Console.WriteLine("You have entered number one");
                      break;
                  case 2:
                      Console.WriteLine("You have entered number two");
                      break;
                  case 3:
                      Console.WriteLine("You have entered number three");
                      break;
                  default:
                      Console.WriteLine("You have exceeded the range of number 1-3 ");
                      break;
              }
  </code></pre>
  
  <p>This is what I did; I don't know why I'm getting an error. The new method seems not to work:</p>
  
  <pre><code>        int enterYourNumber;
          char shortLetter;
  
  
            do
          {
              enterYourNumber = GetNumber();
              WriteNumber(enterYourNumber);                
              Console.WriteLine("Do you still want to enter a number? Y/N");
              shortLetter = Convert.ToChar(Console.ReadLine());
           }
         while (shortLetter == 'y' || shortLetter == 'Y')
          {
              Console.WriteLine("Please enter the integer: ");
              enterYourNumber = Convert.ToInt32(Console.ReadLine());
              WriteNumber(enterYourNumber);
  
              Console.WriteLine("Do you still want to enter a number? Y/N");
              shortLetter = Convert.ToChar(Console.ReadLine());
          }
      }
  
  
  
         public static int GetNumber() 
         {
         bool done = false;
         int value;
         while ( !done ) 
          {
              Console.WriteLine("Please enter the integer: ");
          try 
          {
              value = Convert.ToInt32(Console.ReadLine());
              done = true;
          }
         catch 
          {
              Console.WriteLine("Please enter an integer not a character");
          }
          }
         }
  </code></pre>
  
  <h2>Update</h2>
  
  <p>Bill, this is the example you gave and it seems I'm still getting an error: </p>
  
  <pre><code>    public static void Main(string[] args)
      {
          int enterYourNumber;
          char shortLetter;
  
  
  
          do
          {
              enteryourNumber = GetNumber();
              WriteNumber(enteryourNumber);
              Console.WriteLine("Do you still want to enter a number? Y/N");
              shortLetter = Convert.ToChar(Console.ReadLine());
          } 
          while (shortLetter == 'y' || shortLetter == 'Y');
      }
  
  
      public static int GetNumber()
      {
          bool done = false;
          int value;
          while (!done)
          {
              Console.WriteLine("Please enter the integer: ");
              try
              {
                  value = Convert.ToInt32(Console.ReadLine());
                  done = true;
              }
              catch
              {
                  Console.WriteLine("Please enter an integer not a character");
              }
  
              Console.WriteLine("Please enter the integer: ");
              enterYourNumber = Convert.ToInt32(Console.ReadLine());
              WriteNumber(enterYourNumber);
              Console.WriteLine("Do you still want to enter a number? Y/N");
              shortLetter = Convert.ToChar(Console.ReadLine());   
          }
      }
  
      public static void WriteNumber(int wordValue)
      {
  
          switch (wordValue)
          {
              case 1:
                  Console.WriteLine("You have entered number one");
                  break;
  
              case 2:
                  Console.WriteLine("You have entered number two");
                  break;
  
              case 3:
                  Console.WriteLine("You have entered number three");
                  break;
  
              default:
                  Console.WriteLine("You have exceeded the range of number 1-3 ");
                  break;
          }
      }
  }
  }
  </code></pre>
  
  <h2>Update</h2>
  
  <p>This response is for Robert. This is what I did now, but if I enter "N" it will not exit the program. It still asked the same question.</p>
  
  <pre><code>    public static void Main(string[] args)
      {
          int enterYourNumber;
          char shortLetter;
          bool validEntry;
  
  
  
          while (true)
          {
              do
              {
                  Console.WriteLine("Please enter an integer: ");
                  string numberString = Console.ReadLine();
                  validEntry = int.TryParse(numberString, out enterYourNumber);
                  WriteNumber(enterYourNumber);
                  if (!validEntry)
                  {
                      Console.WriteLine("Entry must be an integer");
                  }
              } while (!validEntry);
  
              Console.WriteLine("Do you still want to enter a number? Y/N");
              shortLetter = Convert.ToChar(Console.ReadLine());
  
  
          }
       }
  
  
  
  
      public static void WriteNumber(int wordValue)
      {
          switch (wordValue)
          {
              case 1:
                  Console.WriteLine("You have entered number one");
                  break;
              case 2:
                  Console.WriteLine("You have entered numbered two");
                  break;
              case 3:
                  Console.WriteLine("You have entered numbered three");
                  break;
              default:
                  Console.WriteLine("You have exceeded the range of number 1-3");
                  break;
          }
      }
  }
  }
  </code></pre>
  
  <h2>Update</h2>
  
  <p>Robert and Bill, here is what I got now. How can I improve this?</p>
  
  <pre><code>     public static void Main(string[] args)
       {
          int intEnterYourNumber;
          char charShortLetter;
          string strUserInput;
  
          do
          {
              do
              {
                  Console.WriteLine("Please enter the integer: ");
                  strUserInput = Console.ReadLine();
              } while (!int.TryParse(strUserInput, out intEnterYourNumber));
              WriteNumber(intEnterYourNumber);
              Console.WriteLine("Do you still want to enter a number? Y/N");
              charShortLetter = Convert.ToChar(Console.ReadLine().ToUpper());
          } while (charShortLetter == 'Y');
      }
  
  
  
      public static void WriteNumber(int wordValue)
      {
          switch (wordValue)
          {
              case 1:
                  Console.WriteLine("You have entered number one");
                  break;
              case 2:
                  Console.WriteLine("You have entered numbered two");
                  break;
              case 3:
                  Console.WriteLine("You have entered numbered three");
                  break;
              default:
                  Console.WriteLine("You have exceeded the range of number 1-3");
                  break;
          }
      }
  </code></pre>