0

since im new to C#, even though i have coded in C before, i still have a question on how I go about to execute the part where i ask the user to enter a set of inputs after a program has already been run.

The following program prints a calendar with the specified number of months, but i need help writing another line of code that would ask the user to enter the month, year and number of months to be printed, after the user has already inputed the values once. Do i have to do some type of looping in my main function or do i have to do it on the method above my main function ?

static void GenMonth(int month, int year)
    {
        int daycode, ndim;
        PrintHeader(month, year);
        ndim=GetNDIM(month,year);
        int day=1;
        daycode = GetDayCode(month, day, year);
        int a,i;
        for(a=1;a<=daycode;a++)
        {
             Console.Write("    ");
        }
        for (i = 1; i <= GetNDIM(month, year); i++)
        {

           Console.Write("{0,4}", i);
           if (((i + daycode) % 7) == 0)
               Console.Write("\n");

        }
       daycode = GetDayCode(month, day, year); 
        if (daycode == 6) 
        {
            Console.Write("\n");
        } 

    }
    static void Main(string[] args)
    {
        Console.WriteLine("please enter m,y,n: \n");
        string input = Console.ReadLine();
        string[] split = input.Split(' ');
        int month = Int32.Parse(split[0]);
        int year = Int32.Parse(split[1]);
        int numberOfMonths = Int32.Parse(split[2]);
        int i=0;
        for (i = 0; i < numberOfMonths; i++)
        {
            GenMonth(month, year);
            month++;
            Console.Write("\n \n");
        }
        if (month > 12)
        {
            month = 1;
            year++;
        }
        Console.ReadKey();
    }
Abner_CV
  • 25
  • 1
  • 2
  • 10
  • I'm confused about what your question is? You did the m,y,n thing. – Shahar Feb 10 '14 at 01:38
  • after the program above is executed for the first time, i want it to prompt the part where it asks the user to enter m,y,n (month, year, number of months to be print). this should repeat until 0 is entered for the number of months. i hope that is clear – Abner_CV Feb 10 '14 at 01:44
  • It seems to me the if (month > 12) should go inside the for loop. Also, you can stop asking for more when the enter key is pressed. See my answer... – Thejaka Maldeniya Feb 10 '14 at 03:17

4 Answers4

1

You'll probably get a few ways to do this - here's one possibility. Just loop continuously, then break out of the loop (and the program will terminate) when you detect a value of 0 for the month.

static void Main(string[] args)
{
    int month = -1;

    while (true)
    {
        Console.WriteLine("please enter m,y,n: \n");
        string input = Console.ReadLine();
        string[] split = input.Split(' ');
        month = Int32.Parse(split[0]);

        if (month == 0)
            break;

        int year = Int32.Parse(split[1]);
        int numberOfMonths = Int32.Parse(split[2]);

        ...
        ...
    }
}
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
0

Try this out:

    static void Main(string[] args)
    {
        while (AskForDate())
        {}
    }

    private static bool AskForDate()
    {
        Console.WriteLine("please enter m,y,n: \n");
        string input = Console.ReadLine();
        string[] split = input.Split(' ');
        int month = Int32.Parse(split[0]);
        int year = Int32.Parse(split[1]);
        int numberOfMonths = Int32.Parse(split[2]);
        int i = 0;
        for (i = 0; i < numberOfMonths; i++)
        {
            GenMonth(month, year);
            month++;
            Console.Write("\n \n");
        }
        if (month > 12)
        {
            month = 1;
            year++;
        }

        Console.Out.WriteLine("Again? [Y/n]");

        var key = Console.ReadKey();
        return key.Key != ConsoleKey.N;
    }
Chris Martin
  • 1,871
  • 16
  • 17
0

Just use a while clause:

static void Main(string[] args)
{
    Console.WriteLine("please enter m,y,n: \n");
    string input = Console.ReadLine();
    string[] split = input.Split(' ');
    int month = Int32.Parse(split[0]);
    while (month != 0)
    {
        int year = Int32.Parse(split[1]);
        int numberOfMonths = Int32.Parse(split[2]);
        int i=0;
        for (i = 0; i < numberOfMonths; i++)
        {
            GenMonth(month, year);
            month++;
            Console.Write("\n \n");
        }
        if (month > 12)
        {
            month = 1;
            year++;
        }
        Console.ReadKey();
        Console.WriteLine("please enter m,y,n: \n");
        input = Console.ReadLine();
        split = input.Split(' ');
        month = Int32.Parse(split[0]);
    }
}

Let me know if that's not what you meant.

Shahar
  • 1,687
  • 2
  • 12
  • 18
  • this works, but when i enter 0 for the number of months, it doesn't terminate/close the command prompt window, but instead asks the user for the m,y,n – Abner_CV Feb 10 '14 at 03:02
  • @Abner_CV Oh I thought you meant that if you enter the text "0" then it ends. Let me fix it. – Shahar Feb 10 '14 at 13:23
0
for(;;)
{
    Console.WriteLine("Enter: Month Year NumberOfMonths\nPress enter to stop.");
    string line = Console.ReadLine();
    if (line == "")
        break;
    string[] terms = line.Split();
    int
        month = int.Parse(terms[0]),
        year = int.Parse(terms[1]),
        numberOfMonths = int.Parse(terms[2]);
    for (int i = 0; i < numberOfMonths; i++)
    {
        GenMonth(month, year);
        if (month == 12)
        {
            month = 1;
            year++;
        }
        else
            month++;
    }
}

Console.Write("\nPress any key...");
Console.ReadKey();
Thejaka Maldeniya
  • 1,076
  • 1
  • 10
  • 20