Reputation: 3
I have searched and looked for the answer but have not been able to get the answer that works.
To the question: I have created an array called backpack that can hold 5 items. I want the user to fill those 5 items. The project is supposed to be able to get back at a later time to fill in more (max 5).
This code are going to be implemented to another code that uses a menue and the user are going to be able to go from adding to the list and read the list. For example: The user goes to option 1 (to add) and adds shoes to the array. The user then goes back and in to option 2 to read whats in the list. Then the user goes back again and in to option 1 to add more items.
I have used a for loop but doesnt seems to work anyway, the error message i got is: System.IndexOutOfRangeException: 'Index was outside the bounds of the array.' Here is the code:
string[] backPack = new string[5];
string answer;
for (int i = backPack.Length; i <= 5; i++)
{
Console.Write("Vad vill du lägga till? "); // What do you want to add.
backPack[i] = Console.ReadLine();
Console.Write("Vill du lägga till fler produkter i din ryggsäck?(j/n): "); (Do you want to add more items to the backpack?
answer = Console.ReadLine().ToLower();
if (answer == "n")
{
break;
}
}
Note that it is in Swedish (the Write lines) but i have added a comment to translate this.
Can u please help me find whats the problem?
Edited code:
string[] backPack = new string[5];
string answer;
for (int i =0; i <= backPack.Length; i++)
{
Console.Write("Vad vill du lägga till? ");
backPack[i] = Console.ReadLine();
Console.Write("Vill du lägga till fler produkter i din ryggsäck?(j/n): ");
answer = Console.ReadLine().ToLower();
if (answer.Equals("n"))
{
break;
}
}
Now the earlier error does not happen directly but it seems the code wants to try and add a 6th item to the array. And then we get the same error again: System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'
Please help.
Upvotes: 0
Views: 386
Reputation: 286
Change your for loop to be
for (int i = 0; i < backPack.Length; i++)
because you already initialized your list with size of 5, so you will count from 0 to 4 or until the user breaks the loop
[EDIT] Regarding the next part of your question, If you want to revisit the list from the last index you filled, you can use an arbitrary integer to track the last filled index, see the following example
string[] backPack = new string[5];
int lastIndex = 0;
int addItem(string[] myList, int index) {
while (index < myList.Length) {
Console.Write("Vad vill du lägga till? "); // What do you want to add.
myList[index] = Console.ReadLine();
index += 1;
Console.Write("Vill du lägga till fler produkter i din ryggsäck?(j/n): "); //(Do you want to add more items to the backpack?
answer = Console.ReadLine().ToLower();
if (answer == "n") {
break;
}
}
return index;
}
// Now you can call lastIndex = addItem(backPack, lastIndex) at each time you want to add a new item
Upvotes: 1
Reputation: 4647
answer == "n" is incorrect. Use a String method to compare strings.
answer.equals("n")
Your loop is also wrong because you are starting at index 5 and the array goes from index 0 to index 4. There is no index 5.
Upvotes: 1