user15166060
user15166060

Reputation:

C# - Multiple Errors

After writing my second program in C#, I receive an error:

Error in Compiler

The error might pertain to the library or it might be on the Conversion or the namespace

   using System;
     namespace MagpantayUserInfoProg {
    class UserInfo {
      static void Main() {
  
        string name, gender; // Variables for storing words
        int contact, age; // Variables for storing integers

        Console.Write("Name: "); // Let the user input his name
        name = Console.ReadLine(); // System reads data

        Console.Write("Gender: "); // Let the user input his gender
        gender = Console.ReadLine();// System reads data


        Console.Write("Age: "); // Let the user input his age
        age = Int32.Parse(Console.ReadLine()); // System reads data


        Console.Write("Mobile Number: "); // Let the user input his contact number
        contact = Int32.Parse(Console.ReadLine()); // System reads data

        // Display Output
        Console.Write("Name: {0}\n", name); 
        Console.Write("Gender: {0}\n", gender);
        Console.Write("Age: {0}\n", age);
        Console.Write("Mobile Number: {0}\n", contact);


        Console.ReadLine(); 

        }
    }
}

Upvotes: 0

Views: 169

Answers (3)

jason.kaisersmith
jason.kaisersmith

Reputation: 9650

As others have pointed out, the number is too large to be stored as an Int (Int32) value which can handle any number between

-2,147,483,648 and 2,147,483,647

Generally, you should not store a phone number as a numeric value anyway, because many phone numbers include a leading zero which is critical but cannot be stored if you store the value as a number. But as not all phone numbers do include a leading zero, then you can't assume that there always is one.

So then you need to store this as a string value instead, and then include some validation to ensure that only numbers are entered. There are many different ways to do this, so you can investigate further to see which suits you, but as an example you can try this, which will scan the string to check that each character is a digit.

Console.Write("Mobile Number: "); // Let the user input his contact number
contact = Console.ReadLine(); // System reads data
if (contact.All(char.IsDigit)) 
{
  // String only contains numbers
}
else
{
  //Handle error here
}

Edit: Of course this solution only allows digit. So if the user wishes to prefix a country code using a '+', then this will not be supported. So then you need to change the solution to support this.

Also, FYI: You don't have a compiler error, you have a runtime exception.

Upvotes: 0

MindSwipe
MindSwipe

Reputation: 7950

Let's dissect the error message:

Value was either too large or too small for an Int32

So, that tells us the problem is somewhere where we're creating an Int32, and Int32.Parse(Console.ReadLine()); fits the bill, not only are we creating an Int32 here, but this is also the point in the program where the error (exception) is encountered (thrown). But why? Well, because computers don't have infinite memory, so a while ago people (read: software developers) decided on a bunch of standards on how to represent data inside a computer, and those standards contain limits, and the limit for an Int32 (which by the way is the standard int in C#) is no smaller than -2,147,483,648 and no larger than 2,147,483,647 (source), but your phone number is 09,563,977,528, larger than the maximum allowed, and hence the error.

So what do we do now? Well, we could use a larger integral type, like long and Convert.ToInt64, but that's just a band aid solution, instead we should use a string (or a more specialized data structure*). Think about it, is a phone number really just a number? No, it isn't, for one the phone numbers 09563977528 and 9563977528 aren't the same right? But if they're were regular old numbers, they would be, 02 and 2 are the exact same number. Additionally, it doesn't really make sense to use arithmetic operations on phone numbers, there's never a need to multiply or subtract or add or whatever 2 phone numbers together. So for those reasons, I'd suggest we just leave phone numbers as strings.


* A good exercise for when you learn about classes and structs would be to implement a custom class representing a phone number

Upvotes: 1

Buğra Demirtaş
Buğra Demirtaş

Reputation: 94

mobile number too large for int

https://learn.microsoft.com/tr-tr/dotnet/csharp/language-reference/builtin-types/integral-numeric-types

check int range

also use string for storing mobile number

Upvotes: 1

Related Questions