Hello.....
i am new to C#,

can some one please give me a simple code example for an array data structure to read Name, Age, Salary, and Address for the employee by Asking the user to enter the number of employees.


Thank you

Recommended Answers

All 18 Replies

This is the simplest code

int[] age, salary;
            String[] address, Name;
            int n=0;
            Console.WriteLine("Please enter Number of employees");
            n = Int32.Parse(Console.ReadLine());
            age=new int[n];
            salary=new int[n];
            address = new String[n];
            Name = new String[n];
            for (int i = 0; i < n; i++)
            {
                Console.WriteLine("Enter Name");
                Name[i] = Console.ReadLine();
                Console.WriteLine("Enter Age");
                age[i] = Int32.Parse(Console.ReadLine());
                Console.WriteLine("Enter Address");
                address[i] = Console.ReadLine();
                Console.WriteLine("Enter Salary");
                salary[i] = Int32.Parse(Console.ReadLine());

            }

thankx abelLazm

can i make the below example you sent in a structure? i mean an array data structure

read this for help on structures and mark this thread solved if it is

can you please show me how to include the structure with the array in your sample code?


thankx

...for the employee by Asking the user to enter the number of employees.

What does it mean to enter the number of employees?

i mean the user enters how many employees he wants at runtime,

the example is great i tried it, but i just wanted to include a structure with it.

is it to define struct Employee{
public int age[],salary;
publice string name[];
}
and then continue with the same code?

Did you think of something like this:

class Program
    {
        static void Main(string[] args)
        {
            List<Employee> list = new List<Employee>();
            //lets add some data
            list.AddRange(new Employee[]{
                new Employee {ID = 1, Name = "John", Age= 28 },
                new Employee {ID = 2, Name = "Ann", Age= 22 },
                new Employee {ID = 3, Name = "George", Age= 30 } 
            });

            Console.WriteLine("Please enter the id of the employee to get this data:");
            int id = int.Parse(Console.ReadLine());
            var userData = list.Where(w => w.ID == id).ToList();
            foreach (var data in userData)
                Console.WriteLine("Name is : {0}, Age is: {1}.", data.Name, data.Age);
            Console.ReadLine();
        }
    }

    class Employee
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
    }

actually i'm just new , i really dont know how to use get and set ....
the example abelLazm gave is simple to me to understand , but i just wanted to add a structure to it..

thank you

Instead of using array, I would STRONGLY recommend you to use generic list instead. Its a WAY better "tool" to work with. Some exercise will sure be needed, but on the end you will be greatful to me, you will see :)

Ok, here`s the code I would like you to study:

class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter number of employees:");
            Console.WriteLine("-------------------------");
            int all = Convert.ToInt32(Console.ReadLine());

            List<Employee> list = new List<Employee>();
            string value;
            for (int i = 0; i < all; i++)
            {
                Employee e = new Employee();
                Console.WriteLine("Employee No.{0}:", i + 1);
                Console.WriteLine("Enter user`s name:");
                value = Console.ReadLine();
                e.Name = value;
                Console.WriteLine("Enter user`s age:");
                value = Console.ReadLine();
                e.Age = int.Parse(value);
                list.Add(e);
            }
            Console.WriteLine("Thx for insertions...");
            Console.WriteLine(".....................");
            Console.WriteLine("Here are the results:");
            foreach (Employee e in list)
                Console.WriteLine("Employee {0} is {1} old", e.Name, e.Age);
            Console.ReadLine();
        }
    }

    class Employee
    {       
        public string Name { get; set; }
        public int Age { get; set; }
    }

Dont forget to do the checking of the numbers. In case if the user enters a string while integer expected, there will be an error.

thankx Mitja Bonca, a nice example code:)

but i still need the above example with a structure .

OK, I downgraded the fine example of Mitja:

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

namespace ConsoleApplication1
{
    class Program
    {
        struct Employee
        {
            public string Name;
            public int Age;
        }
            
        static void Main(string[] args)
        {
            Console.WriteLine("Enter number of employees:"); 
            Console.WriteLine("-------------------------"); 
            int all = Convert.ToInt32(Console.ReadLine());
            Employee[] emplist = new Employee[all]; 
            string value; 
            for (int i = 0; i < all; i++) 
            { 
                //Employee e = new Employee(); 
                Console.WriteLine("Employee No.{0}:", i + 1); 
                Console.WriteLine("Enter user`s name:"); 
                value = Console.ReadLine();
                emplist[i].Name = value; 
                Console.WriteLine("Enter user`s age:"); 
                value = Console.ReadLine();
                emplist[i].Age = int.Parse(value); 
            } 
            Console.WriteLine("Thx for insertions..."); 
            Console.WriteLine("....................."); 
            Console.WriteLine("Here are the results:");
            foreach (Employee e in emplist) 
                Console.WriteLine("Employee {0} is {1} old", e.Name, e.Age); 
            Console.ReadLine();
        }
    }
}

thank you very much

So this solves your problem? :)

can i have the above example in arrays instead of a list/
is it possible?

Could you elaborate on what you really want?
I just posted (translated)code from Mitja and only used arrays!
OK my array is called emplist , see line 21.
You could call it emparray or whatever, if you like that.

i got it

thank you very much

Please mark this thread solved if you got the solution :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.