Home > front end >  Input from user to array and Display in Console in C#
Input from user to array and Display in Console in C#

Time:10-06

I am a Beginner in C# and I was wondering what I got wrong with this code.

  1. I want the user to input the amount of numbers
  2. Then an array with that amount gets created
  3. Then finally I want to display all the numbers in the array.

The Code:

using System;
using System.Threading;
using System.Collections.Generic;

namespace Console_Project_alpha
{
    
class Program 
{
    
    static void Main(string[] args)
    {
            Console.Write("Enter the amount of numbers: ");
            int amount = Convert.ToInt32(Console.ReadLine());
            
            int[] numbers = new int[amount];
            string nth = "";

            
            
            for( int i = 1; i <= amount ; i  )
            {
                        if(i == 1)
                        {
                            nth = i   "st";    
                        }
                        else if( i == 2)
                        {
                           nth = i   "nd";
                        }
                        else if( i == 3)
                        {
                            nth = i   "rd";   
                        }else{
                            nth = i   "th";
                        }
                
                Console.Write("\nEnter "   nth   " Number:");
                int num = Convert.ToInt32(Console.ReadLine());
               
               for(int j = 0; j <= numbers.Length; j  )
                {
                    numbers[j] = num;
                    
                }
                
            }
            System.Console.WriteLine(numbers);

            

    }

   }
}

Any help is highly appreciated. Thanks in advance

CodePudding user response:

  1. In your code you all time overwriting the same value to all index in array.
  2. If you want to display values from array in console just iterate after array

Properly worked example based on your code:

class Program
{

    static void Main(string[] args)
    {
        Console.Write("Enter the amount of numbers: ");
        int amount = Convert.ToInt32(Console.ReadLine());

        int[] numbers = new int[amount];
        string nth = "";
        int index = 0;

        for (int i = 1; i <= amount; i  )
        {
            if (i == 1)
            {
                nth = i   "st";
            }
            else if (i == 2)
            {
                nth = i   "nd";
            }
            else if (i == 3)
            {
                nth = i   "rd";
            }
            else
            {
                nth = i   "th";
            }

            Console.Write("\nEnter "   nth   " Number:");
            int num = Convert.ToInt32(Console.ReadLine());

            numbers[index] = num;
            index  ;
        }

        for (int i = 0; i <= numbers.Length - 1; i  )
        {
            Console.WriteLine(numbers[i]);
        }

        Console.ReadLine();
    }

}

CodePudding user response:

Okay so lets do this all from the start...

// To Input the array and save the array as "arr".
var arr = Array.ConvertAll(Console.ReadLine().Trim().Split(' '),Convert.ToInt32);
// To display the array.
static void DisplayArray(int[] arr) => Console.WriteLine(string.Join(" ", arr));
DisplayArray(arr);

Incase you don't understand the code I added comments.

Example:

Input...

1 2 3 4 5

Output...

1 2 3 4 5

Sources:

To Display an Array:

Microsoft Documentation

To Input an Array:

Stackoverflow Question

  •  Tags:  
  • c#
  • Related