Click here to Skip to main content
15,888,301 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Menu
    {
        public void go()
        {
            int choice = -1;

            while (choice != 0)
            {
                WriteMenuText();
                choice = int.Parse(Console.ReadLine());
                switch (choice)
                {
                    case 1:
                        {

                        }
                    case 2:
                        {

                        }
                    case 3:
                        {

                        }
                }
            }
        }
        
             
        public void WriteMenuText()
        {

Console.WriteLine("-------------------------------------------------------");
            Console.WriteLine("      week                                    :1");
            Console.WriteLine("      Time                                    :2");
            Console.WriteLine("      Currency                                :3");
            Console.WriteLine("      calculater                              :4");
            Console.WriteLine("      Exit                                    :0");
            Console.WriteLine("-------------------------------------------------------");
            Console.WriteLine("\nYour choice");
        }
		
    }

}


I have one problem white it when im going to pick a number over 4 and under 0 its going to say wrong nummber try again and the menu will com up one moore time. but i dont get it to work.
Posted
Comments
Sridhar Patnayak 2-Feb-12 6:10am    
Tommy you placed WriteMenuText();
choice = int.Parse(Console.ReadLine());
in while loop, so it is displaying more times
for answer check my solution.

In switch case better to use
default :


In your case,it is like this
default   :Console.WriteLine("Entered wrong choice");


and change your code some thing like this
C#
WriteMenuText();
choice = int.Parse(Console.ReadLine());
while (choice != 0)
          {
              
              
              switch (choice)
              {
                  case 1:
                      {

                      }
                  case 2:
                      {

                      }
                  case 3:
                      {

                      }
                  case 4:
                      {

                      }
                 default: { }


              }
          }



Thanks
SP
 
Share this answer
 
v3
Comments
Tommy116 2-Feb-12 6:31am    
thx
Your while is checking for 0 only. Anything else keeps the loop running.
 
Share this answer
 
modify your go function and change the switch case to add the default case.
code is given below.

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

namespace ConsoleApplication1
{
    class Menu
    {
        public void go()
        {
            int choice = -1;

            while (choice != 0)
            {
                WriteMenuText();
                choice = int.Parse(Console.ReadLine());
                switch (choice)
                {
                    case 1:
                        {

                        }
                    case 2:
                        {

                        }
                    case 3:
                        {

                        }
                   default:
                        {
                            Console.WriteLine("Wrong Number");
                        }  

                }
            }
        }


        public void WriteMenuText()
        {

Console.WriteLine("-------------------------------------------------------");
            Console.WriteLine("      week                                    :1");
            Console.WriteLine("      Time                                    :2");
            Console.WriteLine("      Currency                                :3");
            Console.WriteLine("      calculater                              :4");
            Console.WriteLine("      Exit                                     ");
            Console.WriteLine("-------------------------------------------------------");
            Console.WriteLine("\nYour choice");
        }

    }

}



in case of any problem revert back.
 
Share this answer
 
Comments
Tommy116 2-Feb-12 6:31am    
thx
thx for the help bur when i enter the default:
i get a error like this Control cannot fall through from one case label default to another
 
Share this answer
 
v2

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