10 Replies - 11104 Views - Last Post: 06 September 2011 - 02:30 PM Rate Topic: -----

#1 Goff256  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 93
  • Joined: 04-July 11

How do you create a menu in a console?

Posted 05 September 2011 - 04:44 PM

I will admit that my schooling has been a little limited. This is likely my fault, likely partially my teacher's, but whatever. No time to spout of blame. I am supposed to be making something in the console application, which I have only done windows applications. I have no clue how to do the first part. Everything after that, I know how to do. It is only the first part that I have no clue about.

Please help.
Is This A Good Question/Topic? 1
  • +

Replies To: How do you create a menu in a console?

#2 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 464
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: How do you create a menu in a console?

Posted 05 September 2011 - 06:31 PM

please Hadean Fall dont just spam the topic. about the menu thing here is a quick example:

        static void Main(string[] args)
        {
            Console.WriteLine("1. option1" +
                Environment.NewLine + "2. optoin2"+
                Environment.NewLine + "3. optoin3");
            var ans = Console.ReadLine();
            int choice=0;
            if (int.TryParse(ans, out choice))
            {
                switch (choice)
                {
                    case 1:
                        //something for option 1
                    case 2:
                        //something for option 2
                    case 3:
                        //something for option 3
                    default:
                        Console.WriteLine("Wrong selection!!!" +
                            Environment.NewLine + "Press any kay for exit");
                        Console.ReadKey();
                        break;
                }
            }
            else
            {
                Console.WriteLine("You must type numeric value only!!!"+
                    Environment.NewLine + "Press any kay for exit");
                Console.ReadKey();
            }
        }


This post has been edited by NoBrain: 05 September 2011 - 06:31 PM

Was This Post Helpful? 0
  • +
  • -

#3 Goff256  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 93
  • Joined: 04-July 11

Re: How do you create a menu in a console?

Posted 05 September 2011 - 06:59 PM

Thank you.

I'll end up writing the code to the project tomorrow and I'll end up putting it up here. Who knows? It might be able to help somebody else. And isn't that the best part of learning? :)
Was This Post Helpful? 0
  • +
  • -

#4 Goff256  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 93
  • Joined: 04-July 11

Re: How do you create a menu in a console?

Posted 06 September 2011 - 10:30 AM

I thought I had it, but apparently I don't.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IT_274__Unit_2__Michael_Goff
{
    class Program
    {

        static void Main(string[] args)
        {
            Console.WriteLine("1. Cylinder" +
                Environment.NewLine + "2. Sphere" +
                Environment.NewLine + "3. Cone" +
                Environment.NewLine + "4. Exit");
            var ans = Console.ReadLine();
            int choice = 0;
            int radius;
            Console.WriteLine("Enter the radius: ");
            radius = Convert.ToInt32(Console.ReadLine());
            int height;
            Console.WriteLine("Enter the height: ");
            height = Convert.ToInt32(Console.ReadLine());
            if (int.TryParse(ans, out choice))
            {
                switch (choice)
                {
                    case 1:
                        decimal cylvolume;
                        cylvolume = ((22/7) * (radius * radius) * height);
                        Console.WriteLine("The volume is" + cylvolume);
                        break;
                    case 2:
                        decimal sphvolume;
                        sphvolume = ((4/3) * (22/7) * (radius*radius*radius));
                        Console.WriteLine("The volume is" + sphvolume);
                        break;
                    
                    case 3:
                        decimal convolume;
                        convolume = ((1/3) * (22/7) * (radius*radius) * height);
                        Console.WriteLine("The volume is" + convolume);
                        break;
                    case 4:
                        this.Close();
                        break;
                    default:
                        Console.WriteLine("Wrong selection!!!");
                        Console.ReadKey();
                        break;
                }
            }
            else
            {
                Console.WriteLine("You must type numeric value only!!!" +
                    Environment.NewLine + "Press any kay for exit");
                Console.ReadKey();
            }
        }
    }
}


You see, I'm getting two great errors.

Control cannot fall through from one case label ('case 3:') to another (line 40)

Keyword 'this' is not valid in a static property, static method, or static field initializer (line 46)

Please and thank you.
Was This Post Helpful? 0
  • +
  • -

#5 sithius92  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 36
  • View blog
  • Posts: 160
  • Joined: 01-August 08

Re: How do you create a menu in a console?

Posted 06 September 2011 - 10:49 AM

First of all, you cannot use the 'this' keyword when creating a console application. Use the 'return' keyword to exit your application.
Was This Post Helpful? 0
  • +
  • -

#6 Goff256  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 93
  • Joined: 04-July 11

Re: How do you create a menu in a console?

Posted 06 September 2011 - 10:59 AM

Wow, that actually made it work somewhat.

But the exit, or return, still has the user input a radius and height. The ending number is also only up for a second before it closes.
Was This Post Helpful? 0
  • +
  • -

#7 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4419
  • View blog
  • Posts: 7,687
  • Joined: 08-June 10

Re: How do you create a menu in a console?

Posted 06 September 2011 - 11:19 AM

View Postsithius92, on 06 September 2011 - 12:49 PM, said:

First of all, you cannot use the 'this' keyword when creating a console application. Use the 'return' keyword to exit your application.


That's not entirely true. this is a reserved word that can only be used in the context of a class, to refer to the instance of the class itself.

Now, because it's tied to instances, it can't be used in a static context. The Main method is static. But it's entirely possible (and some recommend it) to make a class that contains all your console application's logic, and then in the Main method instantiate it and run it. In that case, you could use it all you want.

The important thing to take away from this is that this cannot be used in a static context. If you're writing code in your main method, that's static.

And anyway, this.Close() is a way to close a Windows Form. The console doesn't have a Close method.

Quote

The ending number is also only up for a second before it closes.


That's because for cases 1-3, you don't have a Console.ReadKey() call.

I suggest you rework your logic entirely, using methods. One to display the menu, one to deal with the input (with a switch statement), then one for each case in the switch.

Afterwards, you can display "Press any key to exit..." and do a Console.ReadKey();
Was This Post Helpful? 2
  • +
  • -

#8 Goff256  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 93
  • Joined: 04-July 11

Re: How do you create a menu in a console?

Posted 06 September 2011 - 12:11 PM

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IT_274__Unit_2__Michael_Goff
{
    class Program
    {

            static void Main(string[] args)
            {
                public void Menu()
            {
                Console.WriteLine("1. Cylinder" +
                Environment.NewLine + "2. Sphere" +
                Environment.NewLine + "3. Cone" +
                Environment.NewLine + "4. Exit");

            }

            public void input()
            {
                var ans = Console.ReadLine();
                int choice = 0;
                Console.ReadKey();
            }
            public void Switch() { 
                switch(choice){
                    case 1:
                        Cylinder;
                        Console.ReadKey();
                        break;
                    case 2:
                        Sphere;
                        Console.ReadKey();
                        break;
                    case 3:
                        Cone;
                        Console.ReadKey();
                        break;
                    case 4:
                        return;
            }
        }
            public Cylinder(){
                
               Console.WriteLine("Enter the radius: ");
               int radius;
               radius = Convert.ToInt32(Console.ReadLine());
               int height;  
               Console.WriteLine("Enter the height: ");  
               height = Convert.ToInt32(Console.ReadLine());
               decimal cylvolume;
               cylvolume = ((22/7) * (radius * radius) * height); 
               Console.WriteLine("The volume is" + cylvolume);  

 }
            public Sphere(){
               Console.WriteLine("Enter the radius: ");
               int radius;
               radius = Convert.ToInt32(Console.ReadLine());
               int height;  
               Console.WriteLine("Enter the height: ");  
               height = Convert.ToInt32(Console.ReadLine());
               decimal sphvolume;
               sphvolume = ((4/3) * (22/7) * (radius*radius*radius));
               Console.WriteLine("The volume is" + sphvolume); 


            }

            public Cone(){
            Console.WriteLine("Enter the radius: ");
               int radius;
               radius = Convert.ToInt32(Console.ReadLine());
               int height;  
               Console.WriteLine("Enter the height: ");  
               height = Convert.ToInt32(Console.ReadLine());
               decimal convolume;
               convolume = ((1/3) * (22/7) * (radius*radius) * height);
               Console.WriteLine("The volume is" + convolume); 
            }
        }
    }
}


And now I'm screwing it up even more

} expected Line 12
Method must have a return type Line 46
Method must have a return type Line 59
Method must have a return type Line 73
The name 'choice' does not exist in the current context Line 29
Type or namespace definition, or end-of-file expected Line 86
Was This Post Helpful? 0
  • +
  • -

#9 CodingSup3rnatur@l-360  Icon User is online

  • D.I.C Addict
  • member icon

Reputation: 990
  • View blog
  • Posts: 971
  • Joined: 30-September 10

Re: How do you create a menu in a console?

Posted 06 September 2011 - 01:39 PM

Hi,

The first and last errors are because you have quite badly screwed up your curly braces. The Main() method is just one example - where's its closing brace? (you cannot have named methods inside other methods, just in case you though that was legal :)).

The next 3 errors are telling you that the three methods on those lines don't have a return type. Your methods have to have a return type, like this, for example:

public void Cylinder(){...}


Notice the void.

The second to last error is because you haven't defined the variable 'choice' in the Switch() method. It is defined in input(), which is a completely different scope to the Switch() method, and thus is invisible to the Switch() method.

:)

This post has been edited by CodingSup3rnatur@l-360: 06 September 2011 - 01:41 PM
Reason for edit:: Missed an error

Was This Post Helpful? 0
  • +
  • -

#10 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4419
  • View blog
  • Posts: 7,687
  • Joined: 08-June 10

Re: How do you create a menu in a console?

Posted 06 September 2011 - 02:02 PM

Well, it's because you can't put methods inside methods. Main is a method. You can't put other methods inside of it. You can call methods in it. Also, your methods need return types, etc. Here, I'll put together a skeleton for you, and you can fill in the blanks:

public class Program {
    public static void Main(string[] args) {
        PrintMenu();
        var input = Console.ReadKey();
        var key = input.KeyChar;
        int value;
        if (int.TryParse(key.ToString(), out value)) {
            //just to move to a new line
            Console.WriteLine();
            RouteChoice(value);
        }
        else {
            Console.WriteLine("Invalid Entry.");
        }
        Console.Write("Press any key to exit...");
        Console.ReadKey(false);
    }

    private static void PrintMenu() {
        throw new NotImplementedException();
    }

    private static void RouteChoice(int menuChoice) {
        switch(menuChoice) {
            case 1:
                GetCylinderRadius();
                break;
            case 2:
                GetSphereRadius();
                break;
            case 3:
                GetConeRadius();
                break;
            default:
                Console.WriteLine("Invalid Entry!");
                break;
        }
    }

    private static void GetCylinderRadius() {
        throw new NotImplementedException();
    }

    private static void GetSphereRadius() {
        throw new NotImplementedException();
    }

    private static void GetConeRadius() {
        throw new NotImplementedException();
    }
}



Replace the throw new NotImplementedException(); with code.
Was This Post Helpful? 1
  • +
  • -

#11 Goff256  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 93
  • Joined: 04-July 11

Re: How do you create a menu in a console?

Posted 06 September 2011 - 02:30 PM

Thank you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1