kar
kar

Reputation: 3651

Storing User Input Integers in an Array

I am trying to ask the user to input 10 numbers. After receiving the numbers, I am storing them in an array followed by printing the array. I came up with the following code to do the task but it is not printing the array.

Also feel that I may have rattled on way too much code for a simple task. Do note that I am very new to c# thus not familiar with advanced stuff or possibly even most of basic stuff. Even the "convert.toInt32", I adopted from reading around and not taught in class yet.

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

namespace test_Array
{
    class Program
    {
        static void Main(string[] args)
        {
            int a;
            int b;
            int c;
            int d;
            int e;
            int f;
            int g;
            int h;
            int i;
            int j; 

            Console.WriteLine("Please input 10 numbers. Press 'ENTER' after each number.");
            a = Convert.ToInt32(Console.ReadLine());
            b = Convert.ToInt32(Console.ReadLine());
            c = Convert.ToInt32(Console.ReadLine());
            d = Convert.ToInt32(Console.ReadLine());
            e = Convert.ToInt32(Console.ReadLine());
            f = Convert.ToInt32(Console.ReadLine());
            g = Convert.ToInt32(Console.ReadLine());
            h = Convert.ToInt32(Console.ReadLine());
            i = Convert.ToInt32(Console.ReadLine());
            j = Convert.ToInt32(Console.ReadLine());

            int[] newArray = {a,b,c,d,e,f,g,h,i,j};

            Console.WriteLine(newArray);

            Console.ReadLine();
        }
    }
}

Upvotes: 3

Views: 31904

Answers (4)

sudharsan
sudharsan

Reputation: 11

    static void Main(string[] args)
    {
        int[] rollno = new int[10];
        Console.WriteLine("Enter the 10 numbers");
        for (int s = 0; s < 9; s++)
        {
            rollno[s] = Convert.ToInt32(Console.ReadLine());
            rollno[s] +=  110;

        }
        for (int j = 0; j < 9; j++)
        {
            Console.WriteLine("The sum of first 10 numbers is : {0}", rollno[j]);
        }
        Console.ReadLine();
    }
}

}

Upvotes: 1

alc
alc

Reputation: 1557

You could simplify things a lot with the following:

static void Main(string[] args)
{
    int newArray = new int[10];

    Console.WriteLine("Please input 10 numbers. Press 'ENTER' after each number.");
    for (int i = 0; i < 10; i++) {
        newArray[i] = Convert.ToInt32(Console.ReadLine());
    }

    Console.WriteLine("The values you've entered are:");
    Console.WriteLine(String.Join(", ", newArray));

    Console.ReadLine();
}

Upvotes: 0

user2711965
user2711965

Reputation: 1825

use a for loop.

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

You can use the same loop to display as well:

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

Upvotes: 7

Servy
Servy

Reputation: 203821

The ToString method of arrays (which is what Console.WriteLine is calling in your code) isn't overloaded to print out the contents of the array. It leaves the basic object implementation of just printing the type name.

You need to manually iterate the array and print out the individual values (or use a method that will do that for you).

I.e.

foreach(var item in array)
    Console.WriteLine(item)

or

Console.WriteLine(string.Join("\n", array));

Upvotes: 3

Related Questions