0

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(); 

        }
    }
}
5
  • 2
    The error tells you exactly what the problem is... The mobile number is too large for an int Commented Nov 23, 2021 at 7:08
  • 1
    Usually phone numbers are better stored as strings than numbers. This also makes easier to perform digit verification such as first digit should be 0, third digit should be 3 etc. Commented Nov 23, 2021 at 7:10
  • 1
    You have to change the data type of contact because it's too large for an Int32 Commented Nov 23, 2021 at 7:10
  • Fundamentally; what would you do with a mobile number that would mean it's sensible to store it in a numeric type? We do not add mobile numbers together, or carry out any kind of maths operations on them.. about the only time I can think it being necessary to use something numeric is if you're making some spamming app that eg sends SMS in sequence - but even then a huge portion of the number range doesn't exist as a dial label number so it would be a costly exe raise. Deciding what data type to use in programming goes beyond "what does the data look like" and into "what will I do with it" Commented Nov 23, 2021 at 7:22
  • Is that your real phone number in that image? Commented Nov 23, 2021 at 8:08

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

No problem @potatoostudies, be sure to mark the answer that helped you out (be it mine, Buğra Demirtaş's or jason.kaisersmith's)
0

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.

3 Comments

But then what if a user enters they're country code? For example +1 202 588 6500, I get what you're trying to convey to OP, but the entire notion of "Phone numbers are just a string of numbers" is false
@MindSwipe A perfectly valid point. But as the OP is currently only trying to use an Int, then I assumed that this is not a requirement. But I will update the answer to make this more obvious.
A further aside, a lot of people (me included) enter phone numbers not as a contiguous string, in the USA it is common for people to insert dashes (-) between block, i.e 202-588-6500 and elsewhere in the world people use spaces to separate blocks, i.e in France: 06 86 57 90 14, but that is definitely out of scope for an answer to this question and I just want to share my pain having to implement a phone number library recently

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.