8 Replies - 498 Views - Last Post: 12 September 2012 - 06:04 PM Rate Topic: -----

#1 Chirpyboy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-September 12

Question on Console Project

Posted 12 September 2012 - 01:52 PM

So it has been a while since i have done basic C# programming, and just got back into school and got my first assignment. Now im slowly remembering what i learned in previous classes but still stuck on getting it to be a complete work. Pretty much its create a console app with menu system to teach someone how to make a paper airplane.

Now this isnt me saying im done more or less just stuck on what to put for my Setup portion along with some suggestions maybe to clean it up some more.


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

namespace PaperAirplane1
{
    class Program
    {
        private string menuInput = "";
        private string exitInput = "";
        private int menuSelection = 0;

        private bool run = true;
        bool numTest = false;

        public void Run()
        {
            Header();
           // SetUp();
            while (run == true)
            {
                Menu();
            }
        }

        static void Main()
        {
            Program Airplanes = new Program();
            Airplanes.Run();
            Console.ReadLine();
            
        }

        private void Header()
        {
            Console.WriteLine("===================\n");
            Console.WriteLine("How To Make A Paper Airplane\n");
            Console.WriteLine("===================\n");
        }

        private void Setup()
        {
            
        }

        private void Menu()
        {
            if (run)
            {
                printMenu();
                menuInput = Console.ReadLine();
                numTest = IsNumber(menuInput);

                if (menuInput != null)
                {
                    if (numTest)
                    {
                        try
                        {
                            menuSelection = Convert.ToInt32(menuInput);
                        }
                        finally
                        {
                        }
                    }
                }
            }

            switch (menuSelection)
            {
                //1)The Arrow 2)The Moth 3)The Dart 4)Exit
                case 1:
                    MakeArrow();
                    break;
                case 2:
                    MakeMoth();
                    break;
                case 3:
                    MakeDart();
                    break;
                case 4:
                    Console.WriteLine("Are you sure you want to Exit? 1) Yes or 2) No");
                    exitInput = Console.ReadLine();
                    try 
                    {
                        menuSelection = Convert.ToInt32(menuInput);
                        if (menuSelection == 1)
                        {
                            Environment.Exit(0);

                        }
                    }

                        finally
                    {
                    }
                    break;

                default:
                    printMenu();
                    break;
                    }
            

                }

            }



        }
        


    



Is This A Good Question/Topic? 0
  • +

Replies To: Question on Console Project

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9579
  • View blog
  • Posts: 36,298
  • Joined: 12-June 08

Re: Question on Console Project

Posted 12 September 2012 - 01:56 PM

I fail to see what your question is..
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5677
  • View blog
  • Posts: 12,212
  • Joined: 02-June 10

Re: Question on Console Project

Posted 12 September 2012 - 01:56 PM

I'm sorry... What exactly is the question?
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3665
  • View blog
  • Posts: 11,493
  • Joined: 05-May 12

Re: Question on Console Project

Posted 12 September 2012 - 02:06 PM

I didn't use to believe this when I first heard it, but I've grown a tiny bit wiser since: "Only write the code that you need now."

Right now, with the code you've posted, it doesn't look you need anything to be in your Setup() method, so you as well not have it.

A quick tip about getting input and trying to see if you got a number. int.TryParse() will cover about 90% of what you need and get rid of your multiple lines of reading from the console and then checking to see if you got a valid number.

Also, calling Environment.Exit(0) is really a poor way to exit your program. Yes, people who write C code do it all the time, but this is C# and there are particular ways doing things. In this case setting run to false will probably get the effect that you need.
Was This Post Helpful? 0
  • +
  • -

#5 Chirpyboy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-September 12

Re: Question on Console Project

Posted 12 September 2012 - 02:54 PM

And to the first two, sorry i guess im just slow and didnt really read what i wrote hah...So i messed with my code more and figured some things out..But now i actually have a real question.

What the question is, is that i would like to know what code to look into when i want my script to recall the main menu after i get done pressing 1. When i do it i do not get any response (which i know its because im missing the code) but after its pressed i can press 1, 2, 3 ,4(exit) and it will show that response but i need to see the option after i press it. If im making sense

private void MakeArrow()
        {
            Console.WriteLine("The Arrow");
            Console.WriteLine("[Instructions....]");
            Console.WriteLine("1)Back to Menu 2)Exit \n");
            menuInput = Console.ReadLine();
            try
            {
                menuSelection = Convert.ToInt32(menuInput);
               
            }
            finally
            {
 
            }
            switch (menuSelection)
            {
                case 1:
                    break;
                case 2:
                    Environment.Exit(0);
                    break;
                                
                default:
                    Console.WriteLine("Sorry Wrong Key \n");
                    Console.WriteLine("The Arrow");
                    Console.WriteLine("[Instructions....]");
                    Console.WriteLine("1)Back to Menu 2)Exit \n");
                    menuInput = Console.ReadLine();
                    break;
            }


Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3665
  • View blog
  • Posts: 11,493
  • Joined: 05-May 12

Re: Question on Console Project

Posted 12 September 2012 - 03:53 PM

As it stands right now based on your post #1 and #5, with the way your MakeArrow() is written, program execution should correctly go back up to Menu(). From Menu() it should go back up to Run() which will loop again because run is true, and call Menu() once again thereby giving you the behavior you were expecting.

Since you messed with your code, though, you should post your new code in its entirety.
Was This Post Helpful? 0
  • +
  • -

#7 Chirpyboy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-September 12

Re: Question on Console Project

Posted 12 September 2012 - 04:27 PM

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

namespace PaperAirplane1
{
    class Program
    {
        private string menuInput = "";
        private string exitInput = "";
        private int menuSelection = 0;

        private bool run = true;
        bool numTest = false;

        public void Run()
        {
            Header();
            SetUp();
            while (run == true)
            {
                Menu();
            }
        }

        static void Main()
        {
            Program Airplanes = new Program();
            Airplanes.Run();
            Console.ReadLine();
            
        }

        private void Header()
        {
            Console.WriteLine("===========================\n");
            Console.WriteLine("How To Make A Paper Airplane\n");
            Console.WriteLine("===========================\n");
            
        }

        private void SetUp()
        {
            Console.WriteLine("So Which Type Would You Like To Learn?\n");
            Console.WriteLine("1)The Arrow 2)The Moth 3)The Dart 4)Exit\n"); 
        }

        private void Menu()
        {
            if (run)
            {
                printMenu();
                menuInput = Console.ReadLine();
                numTest = IsNumber(menuInput);

                if (menuInput != null)
                {
                    if (numTest)
                    {
                        try
                        {
                            menuSelection = Convert.ToInt32(menuInput);
                        }
                        finally
                        {
                        }
                    }
                }
            }

            switch (menuSelection)
            {
                //1)The Arrow 2)The Moth 3)The Dart 4)Exit
                case 1:
                    MakeArrow();
                    break;
                case 2:
                    MakeMoth();
                    break;
                case 3:
                    MakeDart();
                    break;
                case 4:
                    Console.WriteLine("Are you sure you want to Exit? 1) Yes or 2) No");
                    menuInput = Console.ReadLine();
                    try 
                    {
                        menuSelection = Convert.ToInt32(menuInput);
                        if (menuSelection == 1)
                        {
                            Environment.Exit(0);

                        }
                    }

                        finally
                    {
                    }
                    break;

                default:
                    printMenu();
                    break;
                    }
            

                }
        private void printMenu()
{
   // Overview();
    //Console.WriteLine("1)The Arrow 2)The Moth 3)The Dart 4)Exit\n");
}
       // private void Overview();
    
   
        private void MakeArrow()
        {
            Console.WriteLine("The Arrow");
            Console.WriteLine("[Instructions....]");
            Console.WriteLine("1)Back to Menu 2)Exit \n");
            menuInput = Console.ReadLine();
            try
            {
                menuSelection = Convert.ToInt32(menuInput);
               
            }
            finally
            {
 
            }
            switch (menuSelection)
            {
                case 1:
                    break;
                case 2:
                    Environment.Exit(0);
                    break;
                                              
                default:
                    Console.WriteLine("Sorry Wrong Key \n");
                    Console.WriteLine("The Arrow");
                    Console.WriteLine("[Instructions....]");
                    Console.WriteLine("1)Back to Menu 2)Exit \n");
                    menuInput = Console.ReadLine();
                    break;
                    
            }


        }
        private void MakeDart()
        {
            Console.WriteLine("The Dart");
            Console.WriteLine("[Instructions....]");
            Console.WriteLine("1)Back to Menu 2)Exit \n");
            menuInput = Console.ReadLine();
            try
            {
                menuSelection = Convert.ToInt32(menuInput);

            }
            finally
            {

            }
            switch (menuSelection)
            {
                case 1:
                    break;
                case 2:
                    Environment.Exit(0);
                    break;

                default:
                    Console.WriteLine("Sorry Wrong Key \n");
                    break;
            }

        }
        private void MakeMoth()
        {
            Console.WriteLine("The Moth");
            Console.WriteLine("[Instructions....]");
            Console.WriteLine("1)Back to Menu 2)Exit \n");
            menuInput = Console.ReadLine();
            try
            {
                menuSelection = Convert.ToInt32(menuInput);

            }
            finally
            {

            }
            switch (menuSelection)
            {
                case 1:
                    break;
                case 2:
                    Environment.Exit(0);
                    break;

                default:
                    Console.WriteLine("Sorry Wrong Key \n");
                    break;
            }

        }

        static bool IsNumber(string value)
        {
            int number1;
                return int.TryParse(value, out number1);
        }

            }
            


        }
   

Is the updated code at the moment.

And yes it does go back to it, but the problem is that i would like for it to WriteLine the original options again. Cause technically it does work, just dont know exactly where i would put the code in s that when i press my menu option that it reposts the number options
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3665
  • View blog
  • Posts: 11,493
  • Joined: 05-May 12

Re: Question on Console Project

Posted 12 September 2012 - 04:54 PM

You aren't printing the menu because you have all the code commented out in printMenu().
Was This Post Helpful? 1
  • +
  • -

#9 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5677
  • View blog
  • Posts: 12,212
  • Joined: 02-June 10

Re: Question on Console Project

Posted 12 September 2012 - 06:04 PM

That's just too damned funny
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1