CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2011
    Posts
    1

    Cant figure out simple console app

    I have created a very simple console app that
    1. Displays menu with list of options to choose
    2. Should be able to add/substract/multiply/divide based on the option choosen
    3. once option is chosen, should prompt the user to enter the inputs and provide the output.

    Now i need to change to code in order to follow the below guidlines:
    - Use one class per method.
    - Common base class for all the operations.

    Can someone please help me out with this?

    The code is below:

    using System;

    namespace ConsoleApp
    {


    public class Choices
    {

    public static void Main()
    {
    int num1;
    int num2;

    string myChoice;


    Choices chs = new Choices();

    do
    {
    myChoice = chs.getChoice();
    Console.Write("press Enter key to continue...");
    Console.ReadLine();
    Console.WriteLine();

    // Make a decision based on the user's choice
    switch (myChoice)
    {
    case "1":
    Console.Write("Please enter the first integer: ");
    num1 = Convert.ToInt32(Console.ReadLine());

    Console.Write("Please enter the second integer: ");
    num2 = Convert.ToInt32(Console.ReadLine());

    Console.Write(num1);
    Console.Write(" + ");
    Console.Write(num2);
    Console.Write(" = ");
    Console.Write(num1 + num2);

    break;

    case "2":
    Console.Write("Please enter the first integer: ");
    num1 = Convert.ToInt32(Console.ReadLine());

    Console.Write("Please enter the second integer: ");
    num2 = Convert.ToInt32(Console.ReadLine());

    Console.Write(num1);
    Console.Write(" - ");
    Console.Write(num2);
    Console.Write(" = ");
    Console.Write(num1 - num2);
    break;

    case "3":
    Console.Write("Please enter the first integer: ");
    num1 = Convert.ToInt32(Console.ReadLine());

    Console.Write("Please enter the second integer: ");
    num2 = Convert.ToInt32(Console.ReadLine());

    Console.Write(num1);
    Console.Write(" / ");
    Console.Write(num2);
    Console.Write(" = ");
    Console.Write(num1 / num2);
    break;

    case "4":
    Console.Write("Please enter the first integer: ");
    num1 = Convert.ToInt32(Console.ReadLine());

    Console.Write("Please enter the second integer: ");
    num2 = Convert.ToInt32(Console.ReadLine());
    Console.Write(num1);
    Console.Write(" * ");
    Console.Write(num2);
    Console.Write(" = ");
    Console.Write(num1 * num2);
    break;

    case "5":
    Console.WriteLine("Bye.");
    break;
    default:
    Console.WriteLine("{0} is not a valid choice", myChoice);
    break;
    }


    Console.ReadLine();

    Console.ReadLine();
    Console.WriteLine();

    } while (myChoice != "5"); // Keep going until the user wants to quit
    }



    string myChoice;


    string getChoice()
    {


    // Print A Menu
    Console.WriteLine("What would you like to do?");

    Console.WriteLine("1 - Do you want to add two numbers?");
    Console.WriteLine("2 - Do you want to subtract two numbers?");
    Console.WriteLine("3 - Do you want to divide two numbers?");
    Console.WriteLine("4 - Do you want to multiply two numbers?");
    Console.WriteLine("5 - Quit\n");

    Console.Write("Choice (1,2,3,4,or 5): ");


    // Retrieve the user's choice
    myChoice = Console.ReadLine();
    Console.WriteLine();

    return myChoice;
    }
    }
    }

  2. #2
    Join Date
    Jul 2005
    Location
    Louisville, KY
    Posts
    201

    Re: Cant figure out simple console app

    Please edit your post, after reading the following, in particular regarding posting code. Thanks.

    Regards,
    Quinn

  3. #3
    Join Date
    Dec 2008
    Posts
    144

    Re: Cant figure out simple console app

    Yeah, no one reads posts with long sections of code that aren't encased in code tags - [ code ] [ /code ] (remove spaces). they're often just too hard to read.

    Also, this looks like homework. Tell us what you have tried or what conecpts you are having trouble with. There are a ton of articles that explain classes, so google may be a good first stop.
    Code:
    if (Issue.Resolved)
    {
         ThreadTools.Click();
         MarkThreadResolved();
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured