Abner_CV
Abner_CV

Reputation: 25

need help asking the user for inputs for the second time in C#

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();
    }

Upvotes: 0

Views: 985

Answers (4)

Shahar
Shahar

Reputation: 1677

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.

Upvotes: 0

Thejaka Maldeniya
Thejaka Maldeniya

Reputation: 1076

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();

Upvotes: 0

Grant Winney
Grant Winney

Reputation: 66469

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]);

        ...
        ...
    }
}

Upvotes: 1

Chris Martin
Chris Martin

Reputation: 1889

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;
    }

Upvotes: 0

Related Questions