Continue Yes Or No

This is a discussion on Continue Yes Or No within the C# Programming forums, part of the General Programming Boards category; I need to add a Continue yes or no command so that it returns the user to the menu if ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Location
    New Zealand
    Posts
    3

    Question Continue Yes Or No

    I need to add a Continue yes or no command so that it returns the user to the menu if they type yes or says thanks and exits if you type no to this Code

    Code:
    Using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace ConsoleApplication9
    {
        class Program
        {
            static ScreenHold()
            {
                Console.WriteLine("Press anykey to continue");
                Console.ReadKey();
            }
    
            static string Menu()
            {
                string choice = null;
    
                Console.WriteLine("\n1...Convert Miles to Kilometres\n");
                Console.WriteLine("\n2...Convert Kilometres to Miles\n");
                Console.WriteLine("\n3...Exit\n");
    
                ScreenHold();
    
                Console.WriteLine("\nEnter a choice(1,2 or 3)\n");
                choice = Console.ReadLine();
    
                return choice;
            }
    
            static double MilesToKilos(double M)
            {
                double Kilometres = M * 1.6093;
                return Kilometres;
            }
    
            static double KilosToMiles(double K)
            {
                double Miles = K * 0.62137;
                return Miles;
            }
    
            static void Main(string[] args)
            {
                string ch = null;
    
                do
                {
                    //Clear the screen
                    Console.Clear();
                    //Display the menu, menu choice stored in ch
                    ch = Menu();
    
                    //Test the users choice
                    switch (ch)
                    {
                        case "1":
                            {
                                Console.WriteLine("\nInput  Miles to be converted\n");
                                double M = double.Parse(Console.ReadLine());
    
                                //Call the function MilesToKilos from main
                                double answer = MilesToKilos(M * 1.6093);
                                Console.WriteLine("Miles converted to Kilometres is: " + answer + '\n');
    
                                Console.WriteLine("\nPress any key to continue\n");
                                Console.ReadKey();
                                break;
                            }
    
                        case "2":
                            {
                                Console.WriteLine("\nInput kilometres to be converted \n");
                                double K = double.Parse(Console.ReadLine());
    
                                //Call the function KilosToMiles from main
                                double answer = KilosToMiles(K * 0.62137);
                                Console.WriteLine("Kilometres converted to Miles is: " + answer + '\n');
    
                                Console.WriteLine("\nPress any key to continue\n");
                                Console.ReadKey();
                                break;
                            }
    
                        case "3":
                            {
                                Console.WriteLine("\nProgram exiting...\n");
                                Console.WriteLine("\nPress any key to continue\n");
                                Console.ReadKey();
    
                                System.Environment.Exit(0);
                                break;
                            }
    
                    }
    
                } while (ch != "3");
    
            }
        }
    }

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by Batzilla View Post
    I need to add a Continue yes or no command so that it returns the user to the menu if they type yes or says thanks and exits if you type no to this Code
    if this is your code you already know how to read input from user and how to compare it to string and how to select the action based on the different user inputs.

    What exactly is the problem? you do not know how to write "yes" or "no"?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Location
    New Zealand
    Posts
    3
    i didn't write most of that hence why i am asking

  4. #4
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    Code:
    static void Main()
    {
        Console.Clear();
        String ch = Menu();
    
        switch (ch)
        {
            case "1":
                Console.WriteLine("\nInput  Miles to be converted\n");
                double M = double.Parse(Console.ReadLine());
    
                //Call the function MilesToKilos from main
                double answer = MilesToKilos(M * 1.6093);
                Console.WriteLine("Miles converted to Kilometres is: " + answer + '\n');
    
                if (DoContinue)
                    Main();
                else
                    goto case "3";
    
                break;
    
            case "2":
                Console.WriteLine("\nInput kilometres to be converted \n");
                double K = double.Parse(Console.ReadLine());
    
                //Call the function KilosToMiles from main
                double answer = KilosToMiles(K * 0.62137);
                Console.WriteLine("Kilometres converted to Miles is: " + answer + '\n');
    
                if (DoContinue)
                    Main();
                else
                    goto case "3";
    
                break;
    
            case "3":
                Console.WriteLine("\nProgram exiting...\n");
                Console.WriteLine("\nPress any key to continue\n");
                Console.ReadKey();
    
                System.Environment.Exit(0);
                break;
        }
    }
    
    static bool DoContinue
    {
        get
        {
            Console.WriteLine("\nContinue? (y/n)\n");
            char choice = 'y';
    
            while (true)
            {
                choice = Console.ReadKey().KeyChar;
    
                if (choice == 'y' || choice == 'n')
                    break;
            }
    
            return choice == 'y';
        }
    }
    Code is self explanatory really. If you choose options 1 or 2, it will execute DoContinue which returns true if the user presses y to continue, false if the user presses n. I used goto because although a lot of programmers don't like to use it, I find it perfect for moving around inside a switch.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Location
    New Zealand
    Posts
    3
    Thank you for your help theoobe

  6. #6
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    We prefer not to dole out answers but rather guide the OP towards figuring out the solution.

  7. #7
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Edit............... Hey batzilla. Look into something like Double.TryParse so your code doesn't throw a crap load of exceptions depending on what the user inputs.
    Last edited by stumon; 04-02-2010 at 07:06 PM.
    The keyboard is the standard device used to cause computer errors!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 11-03-2008, 08:48 PM
  2. Continue and switch
    By camzio in forum C Programming
    Replies: 10
    Last Post: 10-04-2008, 08:31 AM
  3. switch - continue
    By DavidP in forum C Programming
    Replies: 2
    Last Post: 06-24-2004, 10:09 AM
  4. Press enter to continue... beginner code
    By rox in forum C Programming
    Replies: 17
    Last Post: 12-02-2003, 04:32 PM
  5. continue in while loops
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 03-04-2002, 09:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21