c# - Can I use tryParse output parameters to populate an array in a for-loop? -


i've got homework stickler need set of eyes. assignment create program prompts student's name, iterates through array of days (string[] days = {"sunday","monday","tuesday","wednesday","thursday","friday","saturday"};) prompting number of hours studied each day. finally, program display daily average number of hours studied week.

i'm stuck data entry method:

    public void enterhours()     {          // entry area header         console.writeline("enter study hours {0} ", name);         (int = 0; < days.length; i++)          {             console.write("{0}'s study hours: ", days[i]);             string dailyhours = console.readline();             int.tryparse(dailyhours, out hours[i]);    // problematic statement         }         sumhours(hours);     } 

currently, name variable property that's been set; days string[] above, , i've instantiated hours int[] hours; same scope days. sumhours method accepts int[] hours parameter , iterates through array summing values.

when run program, console displays

enter study hours john doe

sunday's study hours:

but no matter enter, end null reference exception. i'm getting following warning message:

warning 1   field 'midterm.studenthour.hours' never assigned to, , have default value null c:\users\dan\dropbox\_matc\itdev115\assignments\midterm\studenthour.cs  11  15  midterm 

i've tried instantiating hours int[] hours = new int[7]; same error, suspect it's way i'm outputting parsed integer, i'm not sure @ beyond solve problem. ideas? hints?

i think out:

        string[] days = { "sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday" };         dictionary<string, int> hours = new dictionary<string, int>();         (int = 0; < days.length; i++)         {             int dailyhours;             console.write("{0}'s study hours: ", days[i]);             while (int.tryparse(console.readline(), out dailyhours) != true)             {                 console.writeline("wrong input,must numbers!!!");                 console.write("{0}'s study hours: ", days[i]);             }             //if(int.tryparse(console.readline(),out dailyhours))             hours.add(days[i], dailyhours);         } 

instead of string array use enum values.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

php - Accessing static methods using newly created $obj or using class Name -