0

Really new to C#. I want to check if an integer (input from user) is positive, negative or zero. It works with the positive number and zero but not when I input a negative number!

I've tried to change input type from user to int, float, decimal, double and so on but nothing works. Also tried a switch-statement. I´m using Visual Studio and debugging to Mac terminal, can that be the issue? I can use negative numbers when writing other code, such as math problems and so on.

Console.Write("Enter a number: ");   
int number = int.Parse(Console.ReadLine());   
if (number == 0)   
{
     Console.Write("zero");
}
else if (number < 0)
{
    Console.Write("negative");
}
else
{       
     Console.Write("positive");

(I've also tried with a switch-statement:)

Console.Write("Enter a number: ");
int number = int.Parse(Console.ReadLine());
switch(number)   
{   
     case 0: Console.Write("zero"); break;    
     case -1: Console.Write("negative"); break;    
     case 1: Console.Write("positive"); break;    
}

In both cases I get the right output when entering a positive number or 0 but when entering a negative number I get error message: "input string was not in a correct format"

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
Alexxx
  • 57
  • 1
  • 2
  • 5
  • 5
    Welcome to Stack Overflow. If the second line of your code is failing, the code after that is irrelevant - it's not the `if` part that's causing the problem, it's the call to `int.Parse`. What *exact* string are you entering? If you're using something like an em-dash (—) or an en-dash (–) instead of a hyphen (-) that could be the cause of the problem. If you copy and paste from here: -42 what happens? – Jon Skeet Apr 22 '19 at 06:28
  • 1
    I have just copy your code into visual studio and notice that code is correct, so I think should be because of your minus have issue. try to use standard input for this. – BeiBei ZHU Apr 22 '19 at 06:30
  • That you for your help! I've tried with your suggested minus sign but it still doesn't work. Starting to believe, as you also do, that there is nothing wrong with my code but something with the format av my minus sign. Any suggestions?! Thanks again! – Alexxx Apr 22 '19 at 06:52
  • From your comment in one of the answers, I see you're using `–`, which has the character code 8211 decimal, which is the [EN-DASH character](https://www.fileformat.info/info/unicode/char/2013/index.htm), not a minus sign. So that's why it's not working. What happens when you use the minus sign on the numeric keypad? – Matthew Watson Apr 22 '19 at 07:52
  • Thank you for your help, it is resolved now. Maybe I typed wrong in my earlier answer. I use the numeric keyboard minus (-) and that works in VS but not in the terminal (as an input). In the terminal I now use the minus sign that I get back as an output in the terminal by just returning a negative number from any code that outputs a minus sign. – Alexxx Apr 22 '19 at 08:07

1 Answers1

0

It should work because int here is Int32 range is -2,147,483,648 to 2,147,483,647. If you are entering any number within this range, it should work. For number outside this range you will get "value too small or too large". Please share the number you are entering

Amruta Moghe
  • 34
  • 1
  • 4
  • thank you for your help. I just tried –42, I'm starting to believe it has something to do with the minus sign on my keyboard, any suggestions? – Alexxx Apr 22 '19 at 06:55
  • Be sure not to have any blanks in your input, neither at the beginning nor at the end of your input, nor between the "-" and the number. The "-" in your post looks wider than normal, wheras the "-" in your switch statements seems OK. Maybe there's some strange culture setings on your PC. Try writing a negative number to the console: `Console.WrteLine( -42 )` and look if te minus is displayed in some strange way. – Heinz Kessler Apr 22 '19 at 07:14
  • OMG thank you!!! I copy pasted your code: Console.WriteLine( -42 ); and got the output: −42 and copied that minus sign and used it as input for my code and it worked, thank you so much! Any idea on how to change my keyboard settings? – Alexxx Apr 22 '19 at 07:35