Jump to content

Press Y to re-run program and E to exit. How do I do this in C#

Mackie
Go to solution Solved by neystro,

you need to do a verification in 2 step:

first you check until the user enter a valid letter (y or e in your case) and then with the valid letter you receive, you can chose to restart or to exit.

ex: 

 

do

{

    // your code...

 

    Console.WriteLine("Press 'e' to exit or 'y' to perform another conversion");

      yesNo = Console.ReadLine().ToLower();

    while(!yesNo.Equals("y") && !yesNo.Equals("e")){

         Console.WriteLine("Invalid!");

         Console.WriteLine("Press 'e' to exit or 'y' to perform another conversion");

         yesNo = Console.ReadLine().ToLower();

    } 

}while (yesNo.Equals("y"));

// if your app arrive here: it mean that the user enter e

Application.Exit();

Im writing a basic currency convertor for college I can make it repeat if the user presses Y but I cant figure out how to assign only the E key to exit. I want to display an error message if the user enters any other key.

 

Here is the code:


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

namespace CurrencyConvertor
{
    class Program
    {
        static void Main(string[] args)
        {

            //Declaration of Variables
            int Conversion;
            string yesNo;
            double EUR, USD = 1.27, GBP = 0.79, CHF = 1.2, AUD = 1.83, CAD = 1.43;

            do
            {
                //Ask user for input
                Console.WriteLine("Enter amount in Euro");
                EUR = double.Parse(Console.ReadLine());

                Console.WriteLine("Choose Currency (1 - 5): \n1: USD \n2: GBP \n3: CHF \n4: AUD \n5: CAD");
                Conversion = int.Parse(Console.ReadLine());


                //Assigning user input to selected conversion
                switch (Conversion)
                {                      
                    case 1:
                        //Assigning green colour to result
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.WriteLine("You have entered {0}EUR which converts to {1}USD", EUR, EUR * USD);
                        break;
                    case 2:
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.WriteLine("You have entered {0}EUR which converts to {1}GBP", EUR, EUR * GBP);
                        break;
                    case 3:
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.WriteLine("You have entered {0}EUR which converts to {1}CHF", EUR, EUR * CHF);
                        break;
                    case 4:
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.WriteLine("You have entered {0}EUR which converts to {1}AUD", EUR, EUR * AUD);
                        break;
                    case 5:
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.WriteLine("You have entered {0}EUR which converts to {1}CAD", EUR, EUR * CAD);
                        break;
                    default:
                        //Red colour for error message
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("Invalid currency selection");
                        break;
                }
                
                //Removing colour from text
                Console.ResetColor();

                //Requesting user input to run program again or exit
                Console.WriteLine("Press 'e' to exit or 'y' to perform another conversion");
                yesNo = Console.ReadLine().ToLower();

            } while (yesNo.Equals("y"));
            Console.ReadLine();
        }
    }
}

Link to comment
Share on other sites

Link to post
Share on other sites

Application.Exit(); ?

mY sYsTeM iS Not pErfoRmInG aS gOOd As I sAW oN yOuTuBe. WhA t IS a GoOd FaN CuRVe??!!? wHat aRe tEh GoOd OvERclok SeTTinGS FoR My CaRd??  HoW CaN I foRcE my GpU to uSe 1o0%? BuT WiLL i HaVE Bo0tllEnEcKs? RyZEN dOeS NoT peRfORm BetTer wItH HiGhER sPEED RaM!!dId i WiN teH SiLiCON LotTerrYyOu ShoUlD dEsHrOuD uR GPUmy SYstEm iS UNDerPerforMiNg iN WarzONEcan mY Pc Run WiNdOwS 11 ?woUld BaKInG MY GRaPHics card fIX it? MultimETeR TeSTiNG!! aMd'S GpU DrIvErS aRe as goOD aS NviDia's YOU SHoUlD oVERCloCk yOUR ramS To 5000C18

 

Link to comment
Share on other sites

Link to post
Share on other sites

this maybe?

http://msdn.microsoft.com/en-us/library/0yd65esw.aspx (try and catch c#) 

CPU: Intel 3570 GPUs: Nvidia GTX 660Ti Case: Fractal design Define R4  Storage: 1TB WD Caviar Black & 240GB Hyper X 3k SSD Sound: Custom One Pros Keyboard: Ducky Shine 4 Mouse: Logitech G500

 

Link to comment
Share on other sites

Link to post
Share on other sites

you need to do a verification in 2 step:

first you check until the user enter a valid letter (y or e in your case) and then with the valid letter you receive, you can chose to restart or to exit.

ex: 

 

do

{

    // your code...

 

    Console.WriteLine("Press 'e' to exit or 'y' to perform another conversion");

      yesNo = Console.ReadLine().ToLower();

    while(!yesNo.Equals("y") && !yesNo.Equals("e")){

         Console.WriteLine("Invalid!");

         Console.WriteLine("Press 'e' to exit or 'y' to perform another conversion");

         yesNo = Console.ReadLine().ToLower();

    } 

}while (yesNo.Equals("y"));

// if your app arrive here: it mean that the user enter e

Application.Exit();

Link to comment
Share on other sites

Link to post
Share on other sites

you need to do a verification in 2 step:

first you check until the user enter a valid letter (y or e in your case) and then with the valid letter you receive, you can chose to restart or to exit.

ex: 

 

do

{

    // your code...

 

    Console.WriteLine("Press 'e' to exit or 'y' to perform another conversion");

      yesNo = Console.ReadLine().ToLower();

    while(!yesNo.Equals("y") && !yesNo.Equals("e")){

         Console.WriteLine("Invalid!");

         Console.WriteLine("Press 'e' to exit or 'y' to perform another conversion");

         yesNo = Console.ReadLine().ToLower();

    } 

}while (yesNo.Equals("y"));

// if your app arrive here: it mean that the user enter e

Application.Exit();

You sir are a GOD amongst men!!

Thank you so much!

Link to comment
Share on other sites

Link to post
Share on other sites

I would do it using a switch case as shown here:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace CurrencyConvertor{    class Program    {        static void Main(string[] args)        {            //Declaration of Variables            int Conversion;            double EUR, USD = 1.27, GBP = 0.79, CHF = 1.2, AUD = 1.83, CAD = 1.43;			//This will be what the user's input 			string userContinue = "";			//Used a switch case to change this			string yesNo;            do            {                //Ask user for input                Console.WriteLine("Enter amount in Euro");                EUR = double.Parse(Console.ReadLine());                Console.WriteLine("Choose Currency (1 - 5): \n1: USD \n2: GBP \n3: CHF \n4: AUD \n5: CAD");                Conversion = int.Parse(Console.ReadLine());                //Assigning user input to selected conversion                switch (Conversion)                {                                          case 1:                        //Assigning green colour to result                        Console.ForegroundColor = ConsoleColor.Green;                        Console.WriteLine("You have entered {0}EUR which converts to {1}USD", EUR, EUR * USD);                        break;                    case 2:                        Console.ForegroundColor = ConsoleColor.Green;                        Console.WriteLine("You have entered {0}EUR which converts to {1}GBP", EUR, EUR * GBP);                        break;                    case 3:                        Console.ForegroundColor = ConsoleColor.Green;                        Console.WriteLine("You have entered {0}EUR which converts to {1}CHF", EUR, EUR * CHF);                        break;                    case 4:                        Console.ForegroundColor = ConsoleColor.Green;                        Console.WriteLine("You have entered {0}EUR which converts to {1}AUD", EUR, EUR * AUD);                        break;                    case 5:                        Console.ForegroundColor = ConsoleColor.Green;                        Console.WriteLine("You have entered {0}EUR which converts to {1}CAD", EUR, EUR * CAD);                        break;                    default:                        //Red colour for error message                        Console.ForegroundColor = ConsoleColor.Red;                        Console.WriteLine("Invalid currency selection");                        break;                }                                //Removing colour from text                Console.ResetColor();                //Requesting user input to run program again or exit                Console.WriteLine("Press 'e' to exit or 'y' to perform another conversion");                userContinue = Console.ReadLine().ToLower();								switch (userContinue)				{					case "e":						yesNo = "e";					case "y":						yesNo = "y";					default:						Console.WriteLine("Please enter \"e\" or \"y\"");						return;				}            } while (yesNo.Equals("y"));            Console.ReadLine();        }    }}

Forgive me if there are any errors, edited it using Notepad++ and not VS

Link to comment
Share on other sites

Link to post
Share on other sites

It's better to let your program end naturally instead of using one of the Exit methods (some sources: 12, and 3) although there may be situations where those methods are needed. Some people think the Exit methods are fine to use, others think you should never use them if they can be avoided. In your case, instead of Application.Exit() you should use return 0;

 

A return statement in the Main method of a console application will exit it. The integer value being returned is a status code and zero indicates success. Note that this also requires you to change static void Main to static int Main since you're now returning a value.

 

However, if you must use Exit, at least use Environment.Exit() instead of Application.Exit() as it's the proper method for a Console app (some sources: 1 and 2). Application.Exit is for a WinForms app, and again there are better ways to end those as well.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×