-1

I could use a try catch scenario here, but I'm used to saying if x = 30 or if x > 100, but what I need to say is if x != int, but I am not allowed to use that statement.

What I need is a way to say, if the input by the user does not equal an integer then...

      Console.Write("Enter number of cats:"); //cats are 121.45

     var temp = Console.ReadLine();
     int cats;
     if (int.TryParse(temp, out cats))
   {

      price = (cats* 121.45);
   }
  else
 {
    Console.Write{"Number of cats must be an integer. Please enter an integer")
 }


      Console.Write ("Enter number of dogs"); //dogs are 113.35
      int product2 = int.Parse(Console.ReadLine());
      price2 = (dogs * 113.35);
      Console.Write ("Enter number of mice:"); //mice are 23.45
      int mice = int.Parse(Console.ReadLine());
      price3= (mice * 23.45);
      Console.Write("Enter number of turtles:"); //turtles are 65.00
      int turtles = int.Parse(Console.ReadLine());
      price4 = (turtles * 65.00);
      Console.Write("Total price : $");
      grosssales = price1 + price2 + price3 + price4; //PRICE ONE IS NOT RECOGNIZED?
      Console.WriteLine(grosssales);
      Console.ReadKey();
    }
Wooble
  • 87,717
  • 12
  • 108
  • 131
Kerry G
  • 917
  • 4
  • 12
  • 21

3 Answers3

4
var temp = Console.ReadLine();
int cats;
if (int.TryParse(temp, out cats))
{
    // Yay, got the int.
}
else
{
    // Boooo, error.  Do something here to handle it.
}

.TryParse is your friend.

Gromer
  • 9,861
  • 4
  • 34
  • 55
2

You can use int.TryParse to establish if user input can be parsed as an int.

  var userInput = Console.ReadLine();     
  int cats;
  if(!int.TryParse(userInput, out cats))
  {
      //userInput could not be parsed as an int
  }
  else
  {
      //cats is good
  }
spender
  • 117,338
  • 33
  • 229
  • 351
  • I'm not a huge fan of if(!... Cleaner to invert the if statement. – Johan Larsson Sep 27 '12 at 23:56
  • @JohanLarsson I usually end up doing things this way to reduce nesting (i.e. the bad case would most likely return or continue a loop) and then have no `else`. Of course, Dyjkstra wouldn't approve because using this style leads to multiple exit points. I don't really care though! – spender Sep 27 '12 at 23:58
  • This does what I would like it to do, but the variable price becomes unrecognized in later parts of the code when it was recognized before replacing the code. – Kerry G Sep 28 '12 at 00:09
  • 1
    There's no `price` in my code, so we can only guess at your implementation. Why not update your question with more code? – spender Sep 28 '12 at 00:12
  • @user1513637 If you declare price as an int then there is no reason for it not to become recognised later on. Is the variable still in scope at that stage? – yu_ominae Sep 28 '12 at 00:15
  • It is still in scope. All of the code has been updated. – Kerry G Sep 28 '12 at 00:21
0

Use TryParse:

int cats;
if (Int32.Parse(Console.ReadLine(), out cats)) {
  price = cats * 239.99;
  // go on here
} else {
  Console.WriteLine("The number of cats must be an integer.");
}
Guffa
  • 687,336
  • 108
  • 737
  • 1,005