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 7987313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:06:39+00:00 2026-06-04T12:06:39+00:00

In general, I know that int32 errors mean that a string value is not

  • 0

In general, I know that int32 errors mean that a string value is not getting converted for the console program. I have seen a lot of code trying to find the answer to this including the following stackoverflow questions (seen much more but these were most useful:

  • How to sum up an array of integers in C#
  • Error CS1501: I'm not overloading a Sum() method correctly
  • CS0019 Operator cannot be applied to operands of type 'bool' and 'int'

That being said, this is also a homework assignment, titled UsingSum.cs as seen in a couple of these links. The difference in mine and these is that I am trying to make it so that the user enters however many Integers they want, then they are added up. The entire assignment is written in link 2….

The problem: I keep getting either 0 or System.Int32[] instead of the sum, despite the changes I make.

I cannot use Linq.

Here is the code:

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

namespace UsingSum
{
class Program
{
    static void Main(string[] args)
    {
        int i;
        int usrInput;
        bool running = true;

        //Enter Question Asking Loop w/ running=true
        while (running)
        {
            Console.Write("Enter a number or enter 999 to exit: ");

            int[] array1 = new int[0];
            for (i = 0; i < array1.Length; i++)
            {
                usrInput = Convert.ToInt32(Console.ReadLine());
                array1[i] = Convert.ToInt32(usrInput);
            }

                for (i = 0; i < array1.Length; i++)
                {
                    Console.WriteLine(array1[i]);
                }

            /*If the user enters 999, calls Sum() and asks user to press any key to exit.
             changes 'running' from true to false to exit the question loop*/

            int exit = Convert.ToInt32 (Console.ReadLine());
            if (exit == 999)                    
            {
                running = false;                                       
                Sum(array1);
            }                        
        }
        //Loop complete

        Console.WriteLine("Press any key to exit.");
        Console.ReadLine();
    }

    public static void Sum(int[] numbers)
    {
        int [] sum1 = new int [0];
        int sum2 = 0;

        //Program accepts user responses with or w/o this loop...Int.32 error present both ways
        //for (int a = 0; a < numbers.Length; ++a)
            //sum1[a] = a;

        //additional loop tried w/o the loop above/below; 
            //when used in the WriteLine w/ sum2 it displays 0, when used with sum1 or numbers Int.32 error
       //Array.ForEach(sum1, delegate(int i) { sum2 += i; });

        foreach (int i in numbers)
            sum2 =+ i;

        Console.WriteLine("The sum of the values in your array is: " + sum1);
        /*tried changing 'numbers' to sum1, sum2, sum1.Convert.ToString(),sum2.Convert.ToString()
         numbers.Convert.ToString(), also tried converting sum2 to a string.*/
    }
}
}

Here is my final solution!

  static void Main(string[] args)
    {
        AskUserForNumbers();
        Console.WriteLine("Press any key to exit");
        Console.ReadLine(); 
    }
public static List<Int32> AskUserForNumbers()
        {
            bool running = true;
            List<int> numbers = new List<int>();
            while (running)
            {
                Console.Write("Enter a number or enter 999 to exit: ");
                int inputValue;
                var inputString = Console.ReadLine();

                //Check for "999" which indicates we should display the numbers entered, the total and then exit our loop.
                if (inputString == "999")
                {                        
                    Console.WriteLine("The sum of the values in your array is: " + numbers.Sum());
                    running = false;
                }
                else if (Int32.TryParse(inputString, out inputValue) && inputValue > 0)
                {
                    numbers.Add(inputValue);
                }
                else
                {
                    Console.WriteLine("Please enter a whole number greater than 0");
                }
            }
            return numbers;
        }
    }
}
  • 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-04T12:06:41+00:00Added an answer on June 4, 2026 at 12:06 pm

    A few problems:

    First, you’re always declaring your arrays as int[] array1 = new int[0];. This means that your code for actually getting the user input will never hit. Maybe you should try using a different collection type (List<int> maybe).

    Second, you never perform any error checking when parsing the integer. That’s bad practice. You should be using int.TryParse(string input, out result) to verify it was a valid number before adding it to the array.

    Third, you are looping over the length of the array for inputs, meaning you will loop through however long the array is, and will continue doing so until the last input you have is the exit number (999).

    Fourth, the input you get for the exit code is discarded (not added to the array to sum).

    Just remember that programming is very procedural. There should be clear (logical) steps from point a to point b. In fact, imagine you are the program and you’re asking a friend to give you numbers to sum up for him. Give him whatever information you think might be useful (such as the exit condition). Diagram the steps, and then try to translate that to code.

    Edit: The main point is that an array (which has a fixed size) is NOT the tool for the job here. You’re not actually filling the array with any data, so that’s why the sum never happens. The culprit is here:

    int[] array1 = new int[0]; // Instantiate a zero-length array? Can't hold any values
    // Will never hit inside the loop here, because i < array1.Length (which is zero) will always be false.
    for (i = 0; i < array1.Length; i++) 
    

    You need to either increase the size of the array to begin with (and either reuse the indexes or resize the array) or use an non-fixed collection (List, for example). Finally, when you pass array1 to the Sum method, array1 is empty because you declared it as a zero element array. That is why you always get a zero printing out. Like I said before, imagine you are the program, and actually run through all these steps, LINE BY LINE.

    For example, you start in the loop. You prepare a miniature notebook to write down all the numbers your friend is telling you with no pages in it. For every page (and realize there are none) in the notebook, you ask your friend for a number. After you’ve gone through every page, you now go through every page again to read all the values he gave you (keep in mind he couldn’t give you any numbers, since the notebook was empty). Then you ask him one more time for a number, and if it’s 999 you tell him you’re done and give him the sum of all the numbers you wrote down. If he didn’t give you 999 as the number, you repeat the cycle.

    Do you understand WHY it’s not working now?

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

Sidebar

Related Questions

I know about covariance, and I know that in general it will not be
Yes, I know that general forms of this question have been asked time and
I'm just getting started with RoR (and web development in general). I know that
I know the general difference between value type and reference type, and I also
I know that general or across the board exception handling is a big no-no,
I know that in general installation via HTTPS is working but somehow it doesn't
We know that the general form of complex numbers is like this: z=a+i*b ,
I know that the general tabs vs spaces thing is as old as the
So, I know that the general idea in PHP is that the whole application
Is there a general convention about exposing members in Python classes? I know that

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.