2

Okay so, I'm trying to make a program that basically uses a for loop to display the days of the week, my code seems fine, runs fine, but when I happen to run it.. it comes up "The day of the week is System.String[]".. Whereas I want it to display The day of the week is Monday... The day of the week is Tuesday... Wednesday.. Etc.

Here is the code I have written for this so far:

        //Declare variables
        int iDays;

        //Declare array
        const int iWEEK = 7;
        string[] sDays = new string[iWEEK] { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };

        //Display the days of the week
        for (iDays = 0; iDays < iWEEK; iDays++)
        {
            Console.WriteLine("The day of the week is " + sDays);
        }

        //Prevent program from closing
        Console.WriteLine();
        Console.WriteLine("Press any key to close");
        Console.ReadKey();

3 Answers 3

6

You have to print a value inside the array, not the array itself.

use sDays[iDays] instead. This will retrieve the value at location iDays in the array sDays.

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

5 Comments

Thanks a lot, I tried pretty much everything other than this.. I feel rather dumb now, I was trying sDays[iWEEK] which came up a bad error too, thanks tho!
We've all been there, no problem. You had an error with sDays[iWEEK] because that would try to access index 7. Your array only has indices 0 to 6.
sDays[iWEEK] would give you Index was outside the bounds of the array. because you would supply it with 7 which is a higher index than what exists in your array. Arrays start at index 0 so 6 would be the highest index you could use, it's still the 7th value though!
Ah okay, Yeah I understand that now, Thank you for your help, appreciated :)
@zespri yeah don't worry, have every intention on doing so, just says I need to wait 6 minutes before I can do so
0

You absolutely don't need array to display weekdays' names. They are already present in system:

for (int i = 1; i <= 7; i++)
{
    DateTime dt = DateTime.Now;
    dt = dt.AddDays(i - (int)DateTime.Now.DayOfWeek);
    Console.WriteLine(dt.ToString("dddd", System.Globalization.CultureInfo.CreateSpecificCulture("en-US")));
}

Comments

0

//Declare variables int iDays;

    //Declare array
    const int iWEEK = 7;
    string[] sDays = new string[iWEEK] { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };

    //Display the days of the week
    for (iDays = 0; iDays < iWEEK; iDays++)
    {
        Console.WriteLine("The day of the week is " + sDays[iDays]);
    }

    //Prevent program from closing
    Console.WriteLine();
    Console.WriteLine("Press any key to close");
    Console.ReadKey();

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.