Answered How to store bigger values in variables.

Fremy12

New member
Joined
Oct 8, 2020
Messages
1
Programming Experience
Beginner
So I've this very simple program:

int number;
number = Convert.ToInt32(Console.ReadLine());

I want to be able to store a password in this variable. But when I try to add more than 10 digit numbers the program throws an Exception "Value is either too large or too small for an int32".
How can I make it so I can fit like 30 digit numbers and more into this variable ?
 
Just make it simple for yourself and use an array if you have superduper long passwords.

I assumed you only allow numeric values since you are using an integer for your value. In which case, the following example does not allow letters. Code :
C#:
        static void Main(string[] args)
        {
            Console.WriteLine("Give me your password?");
            string user_input = Console.ReadLine();
            bool parsed = Is_Number(user_input.ToCharArray());
            if (parsed)
                Console.WriteLine($"Password typed is : { user_input }");
            else
            {
                Console.WriteLine("Your password is not all numbers, try again.");
                Console.WriteLine("Give me your password?");
            }

            Console.ReadLine();
        }

        private static bool Is_Number(char[] value)
        {
            foreach (char chr in value)
            {
                if (char.IsNumber(chr))
                {
                    Console.WriteLine($"We got the number { chr }");
                }
                else
                {
                    Console.WriteLine($"{ chr } is not a number");
                    return false;
                }
            }
            return true;
        }
Another way to to write that Is_Number function is :
C#:
        private static bool Is_Number(char[] value)
        {
            foreach (char chr in value)
            {
                if (chr >= 0 && chr <= 9)
                {
                    Console.WriteLine($"We got the number { chr }");
                }
                else
                {
                    Console.WriteLine($"{ chr } is not a number");
                    return false;
                }
            }
            return true;
        }
This :
number = Convert.ToInt32(Console.ReadLine());
is something you want to try to avoid. Never take the users input as validated without actually validating it first. Run this code and type pass for your password :
C#:
        static void Main(string[] args)
        {
            Console.WriteLine("Give me your password?");
            int user_input = Convert.ToInt32(Console.ReadLine());
            

            Console.ReadLine();
        }
See that format exception that's being thrown? That's why we parse our users input to ensure the values given by the user are actually numeric. Anyway, my example code above handles this for you. Hope it helps.
 
Why won't a simple string suffice, and just validate that all the characters in the string are numbers using a regex or simple lambda?
 
Could do that. After all, there is more than enough memory allocated to strings. However, when checking any char is a number, you will likely be dealing with an array of chars anyway or iterating or enumerating that string as a collection of chars, so why not just use an array of chars...
 
Calling ToCharArray() allocates a new array and copies the data out of the string into the array:

The following two iterations over the strings characters work without having to allocate a new copy of the string data:
C#:
foreach(char ch in input)
{
    // do something with ch
}

or

C#:
for(int i = 0; i < input.Length; i++)
{
    // do something with input[i]
}
 
Data type int has max value of 2147483647 (10 digits), for bigger number there is long data type. It seems though that you don't really need to convert the password string into a numeric data type, you only need to validate that the string only has numbers, so you should just store the string variable and validate as suggested already. An alternative is to use Linq for this:
C#:
var password = Console.ReadLine();
var valid = password.All(char.IsNumber);
 
Here's a version based on Skydivers suggestion. (Without array)
C#:
        static void Main(string[] args)
        {
            Console.WriteLine("Give me your password?");
            string user_input = Console.ReadLine();
            bool parsed = Is_Number(user_input);
            if (parsed)
                Console.WriteLine($"Password typed is : { user_input }");
            else
            {
                Console.WriteLine("Your password is not all numbers, try again.");
                Console.WriteLine("Give me your password?");
            }

            Console.ReadLine();
        }

        private static bool Is_Number(string value)
        {
            foreach (char chr in value)
            {
                if (char.IsNumber(chr))
                {
                    Console.WriteLine($"We got the number { chr }");
                }
                else
                {
                    Console.WriteLine($"{ chr } is not a number");
                    return false;
                }
            }
            return true;
        }

Screenshot : Screenshot

With John's bit of linq thrown in there, really allows you to compact the amount of code required, but you lose out on what printed values are what :
C#:
        static void Main(string[] args)
        {
            Console.WriteLine("Give me your password?");
            string user_input = Console.ReadLine();
            bool parsed = Is_Number(user_input);
            if (parsed)
                Console.WriteLine($"Password typed is : { user_input }");
            else
            {
                Console.WriteLine("Your password is not all numbers, try again.");
                Console.WriteLine("Give me your password?");
            }
            Console.ReadLine();
        }

        private static bool Is_Number(string value)
        {
            return value.All(char.IsNumber);
        }

Screenshot : Screenshot

re -
using a regex or simple lambda
Poster is a beginner, so both are likely outside of his understanding.
 
It seems though that you don't really need to convert the password string into a numeric data type
It's not really a case of not needing to but, rather, needing not to. If you store a password as a number then any leading zeroes will be lost.
 
Back
Top Bottom