Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8536479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:40:33+00:00 2026-06-11T10:40:33+00:00

I am hoping this is something small and not a big issue. I am

  • 0

I am hoping this is something small and not a big issue. I am coding a program (Self teaching via book) which will ask for an operator, then an array of numbers then apply the operator to get a result. The program isn’t prompting me for numbers and assumes an empty array. I think I understand what is going wrong, just not how to recode it so that it will ask me to enter the numbers into the array.

The other thing I am curious about is can I force the program to exit in the first switch of the class code if the Operator is not valid? I found Application.Exit() but that only seems to work for WinForms. Is there a C# code equivalent?

My Main method is as follows:

    static void Main(string[] args)
    {
        MathMethodClass mathMethods = new MathMethodClass();

        int[] intArray = new int[] { };

        Console.Write("Choose an Operator + or *: ");
        string whichOp = Console.ReadLine();

        Console.WriteLine("Thank you.  You chose {0}.", mathMethods.OperatorName(whichOp));

        Console.WriteLine("Please enter an array of numbers: ");

        for (int i=0;i<intArray.Length;i++)
        {
            intArray[i] = Int32.Parse(Console.ReadLine());
        }

        Console.WriteLine("Thank you.  You entered the numbers {0}", intArray);

        Console.WriteLine("The answer is: {0}.", mathMethods.MathMethod(whichOp, intArray));

        Console.ReadLine();
    }

And my Class is as follows:

class MathMethodClass
{
    public string OperatorName(string whichOp)
    {
        switch (whichOp)
        {
            case "+":
                whichOp = "addition";
                break;
            case "*":
                whichOp = "multiplication";
                break;
            default:
                Console.WriteLine("Error: Unknown Operator. Exiting ...");
                Console.ReadLine();
                break;
        }

        return whichOp;
    }

    public int MathMethod(string whichOp, params int[] theNums)
    {
        int theAnswer = 0;

        switch (whichOp)
        {
            case "+":
                for (int ct = 0; ct < theNums.Length; ct++)
                {
                    theAnswer += theNums[ct];
                }
                break;

            case "*":
                for (int ct = 0; ct < theNums.Length; ct++)
                {
                    theAnswer *= theNums[ct];
                }
                break;

            default:
                Console.WriteLine("Error. Something went wrong in the MathMethod. Exiting ...");
                Console.ReadLine();
                break;
        }

        return theAnswer;
    }
}

Any assistance in letting me know where I am going wrong would be great.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T10:40:34+00:00Added an answer on June 11, 2026 at 10:40 am

    You need to specify an array size, you are currently creating an empty array. Define it with some size like:

        int[] intArray = new int[5];
    

    Or the better approach would be to ask the user for the array size. Add the following code before your for loop

    Console.Write("Enter Number of elements in Array: ");
    arraySize = int.Parse(Console.ReadLine());
    intArray = new int[arraySize];
    
    Console.WriteLine("Please enter an array of numbers: ");
    
    for (int i = 0; i < intArray.Length; i++)
    {
        intArray[i] = Int32.Parse(Console.ReadLine());
    }
    

    In your original code you are specifying the array of 0 size with int[] intArray = new int[] { }; and later in your loop, you are checking it against the length and thus not getting into the loop. That is why you are having problem. Other than that code seems to be fine. A much better approach would be to use int.TryParse instead of int.Parse, so that you can check for valid int input.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm hoping this is something small. After combing through this for a couple hours
I'm hoping this is something silly I've done. I got a function unigref near
Hoping that using something like this demo it is possible to drag items within
I'm hoping this will be an easy one :) I've been stuffing around for
I'm hoping this is just a bug on Facebook's end that will be fixed
I've build a small web app for the iPad which I was hoping to
Hope this is not a stupid question. For Regression testing, I wrote a small
Hoping this is just a case of syntax. I'm writing a custom search function
Good afternoon I'm hoping this is going to be nice and a simple f**k
I'm hoping this just needs a new pair of eyes casting over it. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.