Click here to Skip to main content
14,883,651 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
C#
namespace Exercise3
{
    class Program
    {
        static void Main(string[] args)
        {
            {
            int monthNumber;

            //Prompt for number
            Console.Write("Enter a number between 1 and 12");
            monthNumber = int.Parse(Console.ReadLine());

            //Calculations
            if (monthNumber == 1)
            {
                Console.Write("This is the 1st month...January");
            }
            else
            {
                if (monthNumber == 2)
                {
                    Console.Write("This is the 2nd month...February");
                }
                else
                {
                    if (monthNumber == 3)
                    {
                        Console.Write("This is the 3rd month...March");
                    }
                    else
                    {
                        if (monthNumber == 4)
                        {
                            Console.Write("This is the 4th month...April");
                        }
                        else
                        {
                            if (monthNumber == 5)
                            {
                                Console.Write("This is the 5th month...May");
                            }
                            else
                            {
                                if (monthNumber == 6)
                                {
                                    Console.Write("This is the 6th month...June");
                                }
                                else
                                {
                                    if (monthNumber == 7)
                                    {
                                        Console.Write("This is the 7th month...July");
                                    }
                                    else
                                    {
                                        if (monthNumber == 8)
                                        {
                                            Console.Write("This is the 8th month...August");
                                        }
                                        else
                                        {
                                            if (monthNumber == 9)
                                            {
                                                Console.Write("This is the 9th month...September");
                                            }
                                            else
                                            {
                                                if (monthNumber == 10)
                                                {
                                                    Console.Write("This is the 10th month...October");
                                                }
                                                else
                                                {
                                                    if (monthNumber == 11)
                                                    {
                                                        Console.Write("This is the 11th month...November");
                                                    }
                                                    else
                                                    {
                                                        if (monthNumber == 12)
                                                        {
                                                            Console.Write("This is the 12th month...December");
                                                        }



        }
    }
}
Posted
Updated 24-Jan-16 18:14pm
v2
Comments
Philippe Mori 25-Jan-16 22:26pm
   
Are you blind? Obviously, there are many more { than }. If the compiler complain, then probably, it is you that don't know how to count... because the compiler is usually very good at counting { and }.

In that case, you miss one } per else and an extra } for the extra { at the top of the function.

And by the way, in a case like this, you can probably add on } at the location the compiler complains until it stop to complain and have the desired result.

SA Kryukov's comments notwithstanding, it may be that you have not been told about system namespaces yet. But he is quite correct in his comments about your code structure. you need to think in terms of breaking things down, reuse of code, common elements etc. A much better way to do what you want is to use an array for your month names, and a common template for your message; something like:
C#
class Program
{
    static string months[] = { "January", "February", "March", "April", ...
    static string suffixes[] = { "st", "nd", "rd", "th", ...
    static void Main(string[] args)
    {
        int monthNumber = 0;

        //Prompt for number
        while (monthNumber == 0)
        {
            Console.Write("Enter a number between 1 and 12");
            if (!int.TryParse(Console.ReadLine(), monthNumber)
                monthNumber = 0;
        }
        Console.WriteLine(string.Format("This is the {0}{1} month...{2}", monthNumber, suffixes[monthNumber-1], months[monthNumber-1]));
    }
}
   
Every left bracket has to have a matching right bracket. All your else-statements are lacking the closing right bracket.

The left bracket right before int monthNumber; is unneccessary.

Instead of this if-else-monstrosity you should use a switch-statement or a string array. Even if you didn't, you could make this much more readable like this:

C#
if (monthNumber == 1)
{
    Console.Write("This is the 1st month...January");
}
else if (monthNumber == 2)
{
    Console.Write("This is the 2nd month...February");
}
else if (monthNumber == 3)
{
    Console.Write("This is the 3rd month...March");
}
else if // and so on..
   
v2
Comments
Sergey Alexandrovich Kryukov 24-Jan-16 19:41pm
   
Better than in inqurer's code, but this is not good, too. A separate "if" for each month? Hard-coded immediate constants per each month, especially of string type? Do be serious.

Please see Solution 2.

—SA
Philippe Mori 25-Jan-16 22:33pm
   
Well since the namespace is Exercise3, that solution might be adequate if user is learning and has only study that at that point... Otherwise, effectively, your answer would be far better.
Sergey Alexandrovich Kryukov 25-Jan-16 23:58pm
   
I understand that. It's important to keep "if" structure as flat is possible, but at least it's important to note that long "if" periods is a sign of wrong code design.

Thank you.

—SA
PIEBALDconsult 24-Jan-16 20:51pm
   
Dictionary?! Array!
Sascha Lefèvre 25-Jan-16 3:42am
   
Of course! Brain freeze :)
Sergey Alexandrovich Kryukov 25-Jan-16 23:59pm
   
It's called K.I.S.S. Hot KISS for brain unfreeze. :-)
—SA
Sascha Lefèvre 26-Jan-16 4:41am
   
hrhr :-)
You can fix it by not writing anything similar, ever. Nested "if", hard-coded immediate constants — not only this is totally non-maintainable, but is pain to see.

All the month names are already in the system. Each of the months is not the separate case, it's just one short fragment of code, not depending on month value. Here is the simple idea: create an instance of System.DateTime and set a month property to required value, them format it using System.DateTime.ToString(string format). Please see:
DateTime Constructor (Int32, Int32, Int32) (System)[^] (year and day don't matter, use any, with specified month),
DateTime.ToString Method (String) (System)[^].

For format, use "full name of the month" format, which is "MMMM". Custom formats are explained here: Custom Date and Time Format Strings[^].

This way, you won't need any "if" or "switch-case" statements, or perhaps just one, checking up if the value is withing the range 1 to 12.

That's all. Please, before asking me about further detail, try to do it all by yourself; it's way too simple.

—SA
   
Error line itself describe solution. You have not complete all curly braces, please check in your code and fix it
After this problem get resolved It looks you need to refine your code as you can optimize it.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900