Click here to Skip to main content
15,917,329 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi, Im Trying to get my 2D array to fill with values and output them to a textbox.
It should look like this when i output it: http://i.stack.imgur.com/LZu7u.jpg

So far this is my code:
C#
txtOutput.Text += "Filling the array with user input..." + "\r\n";
            txtOutput.Text += "The product allocation is as follows:" + "\r\n\r\n";

            int[,] toys = new int[5, 4];
            for (int week = 0; week <= 3; week++)
            {
                for (int day = 0; day <= 4; day++)
                {
                    int tempVal = 0;
                    if (int.TryParse(Microsoft.VisualBasic.Interaction.InputBox("Please enter value for Day " + (day + 1) + " in week " + (week + 1) + "."), out tempVal))
                    {
                        toys[day, week] = tempVal;
                    }
                    else
                    {
                        MessageBox.Show("Invalid entry");
                        day--;
                        continue;
                    }

                    //the first line must be separate because the headings are not part of the array
                    txtOutput.Text += "\t\t" + "Mon" + "\t" + "Tue" + "\t" + "Wed" + "\t" + "Thu" + "\t" + "Fri" + "\t" + "\r\n";
                    for (week = 0; week <= 3; week++)//foreach week
                    {
                        //construct the line of text which represents the week's data
                        txtOutput.Text += "\tWeek " + (week + 1) + "\t";
                        for (day = 0; day <= 4; day++)
                        {
                            txtOutput.Text += toys[day, week];
                            if (day != 4)
                            {
                                //so long as it is not the last day, then it tabs over
                                txtOutput.Text += "\t";
                            }
                        }
                        //moving over to the next line
                        txtOutput.Text += "\r\n\r\n";
                    }


My question is, right now if i run this code, it asks me 1 day, then outputs all the data straight away, how do i get it to ask me for all the values?
Posted
Updated 23-Jan-14 19:02pm
v4
Comments
BillWoodruff 24-Jan-14 2:20am    
Have you set a break-point on entering the for-loop where you ask user for input and examined what's going on with each execution of the loop ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900