help in task arrays c# | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

help in task arrays c#

Hi all, I am new to programming and trying to write simple programs... Here I wanted a user to insert a number and get information: id, name, and salary for the number But there comes output for every declared variable. namespace ConsoleApp7array { class Program { static void Main(string[] args) { int i; Console.WriteLine("Insert a number"); int number = Convert.ToInt32(Console.ReadLine()); int[] id = new int[3]; id[0] = 1; id[1] = 2; id[2] = 3; string[] name = new string[3]; name[0] = "john"; name[1] = "anna"; name[2] = "chris"; int[] salary = new int[3]; salary[0] = 3456; salary[1] = 5678; salary[2] = 9876; for(i = 0; i <=id.Length; i++) { int id1 = id[i]; string name1 = name[i]; int salary1 = salary[i]; Console.WriteLine(id1 + " " + name1 + " " + salary1); Console.ReadLine(); } Please give me a hint what to do :P

9th Apr 2018, 7:19 PM
mar
3 Answers
+ 2
Nice code, good practice The problem is in the for-loop length of array is 3 elements for(i = 0; i <=id.Length; i++) Arrays count form 0, so to count 3 elements you count 0,1,2 you have 3 elements so you should count to 2 for(i = 0; i <=id.Length-1; i++) https://code.sololearn.com/c1d1ha9W51e7
9th Apr 2018, 9:15 PM
sneeze
sneeze - avatar
+ 4
Good start 😀
18th Mar 2019, 7:51 PM
Edwin
Edwin - avatar
+ 1
Thank you for help :)
9th Apr 2018, 9:39 PM
mar