0

I need to generate a code where i will be getting an user input (an int) and using that i need to generate days of the week

example: user inputs - 5

I need to output something like below

  1. Today is (todays date)
  2. Tomorrow is (tomorrows date)
  3. Day after tomorrow is
  4. two days after tomorrow is
  5. three days after tomorrow is ...

and if the user enters 6 it should output 6days as above.

please help as i'm an novice in C#

Thanks alot

megazoid
  • 1,146
  • 5
  • 17
  • 28

6 Answers6

6

I won't write code for you. Would like to give some hints.

You can use DateTime class to find out the current date and time and it also provide methods which you can easily use to find what next day is and similar.

You can use While Loop as you said in Title or even For can work

You can take input from user through Console class if you are making console based app otherwise you can take input in some textbox.

For loop or while loop will run for current day plus number that user have entered minus 1

Haris Hasan
  • 29,856
  • 10
  • 92
  • 122
  • I'm already getting the input ... thats no issue ... but i have no clue how to dynamically use the AddDays in a while loop – megazoid Oct 20 '11 at 05:05
  • You can add one or more days at a time. `DateTime tomorrow = today.AddDays(1)` similarly `DateTime dayAfterTomorrow = today.AddDays(2);` I think you can use this logic inside while or for. – Haris Hasan Oct 20 '11 at 05:13
2
 static void Main(string[] args)
        {
            days();
        }
        public static void days()
        {
            Console.Write("Please enter number : ");
            int a = Convert.ToInt32(Console.ReadLine());

            for (int i = 0; i < a; i++)
            {
                string strdays;
                switch (i)
                {
                    case 0:
                        strdays = ". Todays date is : ";
                        break;
                    case 1:
                        strdays = ". Tommorrows date is : ";
                        break;
                    case 2:
                        strdays = ". Day after tommorrows date is : ";
                        break;                   
                    default:
                        strdays = ". " + (i-1) + "Days after tommorrows date is : ";
                        break;
                }                    
                Console.WriteLine((i+1)+ strdays + System.DateTime.Now.AddDays(i));                 
            }            
            Console.ReadLine();
        }

hope this help u..

Vikas
  • 36
  • 1
  • Using the Case statement was the easiest ! thanks for this !!! but since mine was a web service, i had to modify of cause. but thanks for the concept !!! – megazoid Oct 31 '11 at 09:10
1

The pieces of code you will need:

  • Console.Write
  • Console.ReadLine
  • int.Parse (to convert the input to an integer)
  • DateTime.Today
  • DateTime.AddDays(zeroOrMoreDays)
  • DateTime.DayOfWeek (if you want Wednesday instead of 10/19/2011 12:00:00 AM)

Here is some pseudo-code:

Ask the user for the number of days (as a string)
Parse numberOfDays into an int
for i = 0 to numberOfDays:
    write line: today.AddDays(i).DayOfWeek

You'll also need to do some tricks to get the natural language stuff to work, e.g. three days after tomorrow is.

I'd use a few extra if i == 0 else if i == 1 type statements to solve this, and fall back on a general <number> days after tomorrow after a certain point.

See this (closed) question for links on how to get that number: converting numbers in to words C#

Edit due to your comments on the question

@ sblom ... nope for a web service i'm planning

Don't use Console stuff then.

Make this a method instead, and don't do the string/int.Parse stuff. Just take an int directly.

Build a result with a StringBuilder, and return a string, rather than printing out the result directly.

Community
  • 1
  • 1
Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183
0
  int diff=0;
  while (diff < 5)
  {
      Console.WriteLine(DateTime.Now.AddDays(diff));
      diff++;
  }
sblom
  • 26,911
  • 4
  • 71
  • 95
rahularyansharma
  • 11,156
  • 18
  • 79
  • 135
0
// num is number if days we'll loop through
int num = 5;

// gets the current date time - save it off.  even though incrementing days, in other
// date incrementing functions (min, sec etc...) it's important to save off
DateTime now = DateTime.Now;

// init a countervar
int count = 0;

// loop until num times
while (count < num)
{
    // add the current count number of days and print
    // ++ after the variable increments it after calculating the new date
    DateTime curr = now.AddDays(count++);

    // output the value - it ends up calling .ToString() on date.
    Console.WriteLine(curr);
}
bryanmac
  • 38,941
  • 11
  • 91
  • 99
0
Int32 numberOfDays = 5;
DateTime dt = DateTime.Now;
for (int i = 0; i < numberOfDays ; i++)
{
    Console.WriteLine(dt.AddDays(i).DayOfWeek);
}

Hope it helps! =)

Renato Gama
  • 16,431
  • 12
  • 58
  • 92