2 Replies - 166 Views - Last Post: 05 August 2019 - 10:10 AM

#1 357mag   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 07-August 07

One little thing wrong with my Array program

Posted 04 August 2019 - 03:05 PM

My program works good but for one problem. Instead of my program showing:

Enter element value 0:
Enter element value 1:
Enter element value 2:

...and so on until loop is over...

It does this (it always shows 0):

Enter element value 0:
Enter element value 0:
Enter element value 0:

Don't understand why so I come here.


using System;

class InputArrayValues
{
    static void Main()
    {
        int[] nums = new int[5];

        for (int i = 0; i < 5; i++)
        {
            Console.Write("Enter element value " + nums[i] + ": ");

                nums[i] = Convert.ToInt32(Console.ReadLine());
        }

        Console.WriteLine();
        Console.Write("The array values you entered are: ");


        for (int i = 0; i < 5; i++)
            Console.Write(nums[i] + " ");

        Console.WriteLine();
        Console.WriteLine();
    }
}

This post has been edited by andrewsw: 04 August 2019 - 11:42 PM
Reason for edit:: addded missing code tags


Is This A Good Question/Topic? 0
  • +

Replies To: One little thing wrong with my Array program

#2 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6822
  • View blog
  • Posts: 28,266
  • Joined: 12-December 12

Re: One little thing wrong with my Array program

Posted 04 August 2019 - 11:42 PM

Please learn how to wrap your code in code tags, I have added them for you on this occasion.
Was This Post Helpful? 0
  • +
  • -

#3 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 981
  • View blog
  • Posts: 3,869
  • Joined: 13-June 14

Re: One little thing wrong with my Array program

Posted 05 August 2019 - 10:10 AM

Seems pretty obvious to me: on line 11, you're using nums[i], the array has been declared on 7 and between those lines you don't set any of the elements, so they're defaulted to 0. Hint: did you really intend to use nums[i] on that line?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1