PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, November 7, 2022

[FIXED] When I finish my task of converting exchange rate , I want to return to the main menu that appears when I start the program

 November 07, 2022     c#, console-application, csharpcodeprovider, menu     No comments   

Issue

I want to return to the main menu not restarting the program all over again to change the type of conversion .. Any help please ? I will be thankful if somebody here helped me ..

using System;

namespace Assignment_1_Csharp
{
    internal class Program
    {
        static void Main(string[] args)
        {      int choice;
            double val,EGP;
            Console.WriteLine("Enter your Choice :\n 1- Dollar to EGP \n 2- EGP to Dollar  ");
            choice = int.Parse(Console.ReadLine());

            switch (choice)
            {
                case 1:
                    double dollar;
                    Console.Write("Enter the Dollar Amount :");
                    dollar = double.Parse(Console.ReadLine());
                    Console.Write("Enter the Dollar Exchange Rate :");
                    val = double.Parse(Console.ReadLine());
                    EGP = dollar * val;
                    Console.WriteLine("{0} Dollar Equals {1} EGP", dollar, EGP);
                    break;
                case 2:
                    Console.Write("Enter the EGP Amount :");
                    EGP = double.Parse(Console.ReadLine());
                    Console.Write("Enter the Dollar Exchange Rate :");
                    val = double.Parse(Console.ReadLine());
                    dollar = EGP / val;
                    Console.WriteLine("{0} EGP Equals {1} Dollars", EGP, dollar);
                    break;
               
            }
            Console.ReadLine();
        }
    }
}

Solution

One way to solve this is using a do-while loop. The following code will run through the choice selection and conversion and then ask user to continue. If the selection is "y" then it will ask the user to choose again otherwise exit the main method.

public static void Main(string[] args)
{
    int choice;
    double val, EGP;
    string userSelection = "y";
    do
    {
        Console.WriteLine("Enter your Choice :\n 1- Dollar to EGP \n 2- EGP to Dollar  ");
        choice = int.Parse(Console.ReadLine());

        switch (choice)
        {
            case 1:
                double dollar;
                Console.Write("Enter the Dollar Amount :");
                dollar = double.Parse(Console.ReadLine());
                Console.Write("Enter the Dollar Exchange Rate :");
                val = double.Parse(Console.ReadLine());
                EGP = dollar * val;
                Console.WriteLine("{0} Dollar Equals {1} EGP", dollar, EGP);
                break;
            case 2:
                Console.Write("Enter the EGP Amount :");
                EGP = double.Parse(Console.ReadLine());
                Console.Write("Enter the Dollar Exchange Rate :");
                val = double.Parse(Console.ReadLine());
                dollar = EGP / val;
                Console.WriteLine("{0} EGP Equals {1} Dollars", EGP, dollar);
                break;

        }
        Console.WriteLine("Enter Y to choose again...");
        userSelection = Console.ReadLine();
    }
    while (userSelection.ToLower() == "y");
}

You can change the text messages and selection to anything you want.



Answered By - Barkha Jain
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing