Joe Grace
Joe Grace

Reputation: 13

How to populate a 2d array via user input?

I'm trying to create a 5,4 array in C# where each cell is made up of user data. The code I have below works sometimes, but not consistently. I can input data into cell [4,5] or [3,4] for example. However, sometimes when I run the code It does not accept the data and displays an empty cell. and Any tips?

        decimal[,] departments = new decimal[4, 5];

        //int count = 0;
        Console.WriteLine("If you would like to run the program, type enter");
        string exit = Console.ReadLine();       
        while (exit != "yes")
        {
            Console.WriteLine("What is the department number that you would like to enter sales for? Enter 9 to exit");
            int departmentNo = int.Parse(Console.ReadLine());
            Console.WriteLine("What day would you like to enter sales for?");
            int input = int.Parse(Console.ReadLine());

            Console.WriteLine("Enter sales");
            departments[departmentNo, input] = decimal.Parse(Console.ReadLine());
            Console.WriteLine("If you are finished, enter yes");
            exit = Console.ReadLine();

        }

Example output

0 0 0 0
0 0 0 0
0 0 500 0
500 0 0 0

Upvotes: 1

Views: 1257

Answers (2)

ProgrammingLlama
ProgrammingLlama

Reputation: 38880

You can use .GetUpperBound(dimension) to obtain the maximum bound (in the example below, it will return 1 for each dimension) for a specific dimension. Once you've got that, you just need to go through each position (I'm assuming you just need to fill data at each position).

Here's a sample:

int[,] items = new int[2, 2];
int width = items.GetUpperBound(0) + 1;
int height = items.GetUpperBound(1) + 1;
for (int x = 0; x < width; ++x)
{
    for (int y = 0; y < height; ++y)
    {
        string input;
        int inputValue;
        do
        {
            Console.WriteLine($"Please input value for ({x},{y}): ");
        }
        while (!int.TryParse(input = Console.ReadLine(), out inputValue));
        items[x, y] = inputValue;
    }
}

Or if you do need the user to be able to specify where the sales value goes, you can update your code to add some validation checks:

while (exit != "yes")
{
    Console.WriteLine("What is the department number that you would like to enter sales for? Enter 9 to exit");
    int departmentNo = int.Parse(Console.ReadLine());
    if (departmentNo > departments.GetUpperBound(0) || departmentNo < 0)
    {
        Console.WriteLine("Please enter a valid department number.");
        continue;
    }
    Console.WriteLine("What day would you like to enter sales for?");
    int input = int.Parse(Console.ReadLine());
    if (input > departments.GetUpperBound(1) || input < 0)
    {
        Console.WriteLine("Please enter a valid input.");
        continue;
    }

    Console.WriteLine("Enter sales");
    if (!decimal.TryParse(Console.ReadLine(), out decimal value))
    {
        Console.WriteLine("Please enter a valid sales figure.");
        continue;
    }
    departments[departmentNo, input] = value;
    Console.WriteLine("If you are finished, enter yes");
    exit = Console.ReadLine();
}

You probably want to change it to be similar to my example above so that you don't have to restart the entire loop again if the input is bad.

Upvotes: 2

Ha Bom
Ha Bom

Reputation: 2927

Because the departments has a fixed length of [4,5], it means the departmentNo can only be in range 0-3 and the input can only be in range 0-4. In addition, decimal keyword indicates a 128-bit data type, it's mean the value for a cell is in range ±1.0 x 10^28 to ±7.9228 x 10^28, which means 28-29 significant digits approximately. You should make some if conditions to make sure input values are in available range.

Upvotes: 0

Related Questions