C
C#3mo ago
Dread

Valid Inputs

can someone help me understand what i’m doing wrong? I can’t figure out how to get the user input to work..
No description
47 Replies
Jimmacle
Jimmacle3mo ago
what's not working and what would "working" look like?
Dread
Dread3mo ago
working would look like this
Dread
Dread3mo ago
No description
Jimmacle
Jimmacle3mo ago
and what's the current problem?
Dread
Dread3mo ago
the problem is that i can’t figure out how to make the user input values like i don’t know what is wrong in my coding that prevents the the function getValue to operate
Pobiega
Pobiega3mo ago
Passing 10 to your list constructor doesn't do what you think it does
Jimmacle
Jimmacle3mo ago
yeah i was gonna get to that eventually, still trying to work out what the current bug is are you getting an error message?
Angius
Angius3mo ago
(pssst, use .Add() on the list)
Jimmacle
Jimmacle3mo ago
it's better to guide them through diagnosing the issues instead of just telling them how to fix stuff :when:
Dread
Dread3mo ago
do i leave the constructor blank?
Pobiega
Pobiega3mo ago
Yes.
Dread
Dread3mo ago
even if the total amount of inputs you want is 10?
Pobiega
Pobiega3mo ago
But read up on what both of those alternatives do
Jimmacle
Jimmacle3mo ago
lists work differently than arrays in terms of how they're sized and how you put new values in
Dread
Dread3mo ago
my instructor teaches more by like examples and following along. i struggle when i don’t understand concepts
Pobiega
Pobiega3mo ago
Your code looks like what I would expect for an array, not for a list.
Dread
Dread3mo ago
do i use foreach?
Pobiega
Pobiega3mo ago
No. If you want to use a list, read up on how lists work
Jimmacle
Jimmacle3mo ago
(i still don't know what "prevents the function getValue to operate" means in terms of what the code is actually doing wrong)
Dread
Dread3mo ago
like getValue is supposed to prompt the user to enter “inputs” to determine whether they’re considered valid or invalid but i guess there’s something wrong with my main method @Jimmacle
Angius
Angius3mo ago
Well, the loop won't run The list has no elements in it The count is 0
Dread
Dread3mo ago
so the loop isn’t even relevant to the list? should i comment it out?
Jimmacle
Jimmacle3mo ago
loops are relevant when you want to do something multiple times so your next step is to look at your loop conditions to see why it's running 0 times, which goes back to pob's comment about looking into how lists should be used
Dread
Dread3mo ago
right and i want the user input to loop.
Dread
Dread3mo ago
No description
Angius
Angius3mo ago
No description
Angius
Angius3mo ago
Fix that
Jimmacle
Jimmacle3mo ago
we're getting to that
Pobiega
Pobiega3mo ago
so @Dread , how do we add an item to a list?
Dread
Dread3mo ago
would i put inputValues.Add
Pobiega
Pobiega3mo ago
Thats a good start so, if I wanted to add... 5 things?
Dread
Dread3mo ago
List<String> inputValues = new List<String>() inputValues.Add() inputValues.Add() inputValues.Add() inputValues.Add() inputValues.Add() ? List<String> inputValues = new List<String>(5?) inputValues.Add() inputValues.Add() inputValues.Add() inputValues.Add() inputValues.Add()
Angius
Angius3mo ago
And how would you do that without repeating the same line 5 times?
Dread
Dread3mo ago
loop?
Angius
Angius3mo ago
Yes
Dread
Dread3mo ago
No description
Dread
Dread3mo ago
what does that 5 in the parentheses mean? because there’s 7 things?
Angius
Angius3mo ago
It initializes the list with the capacity of 5 See, a list, underneath, is an array of a constant length Whenever that length gets exceeded when you .Add() to that list, a new array of double that length gets created and all the elements are copied over To avoid having to do that, if you know beforehand how big your list will be, you can initialize the list to a certain capacity That will set the length of the internal array Creating a new List<T>(5) creates a list backed by a 5-long array When adding a 6th element to it, the list resizes the array to one of length 10 And so on
daniel2
daniel23mo ago
U should change something in the for loop as zzzzzz mentioned the list is empty so for loop is not executing Think about what conditions should the for loop be
Dread
Dread3mo ago
this is where i’m at and i’m still unsure that ive even made much progress. is it my main method that needs fixing? or is it my getValue loop?
No description
Jimmacle
Jimmacle3mo ago
there is a red squiggle under the most immediate issue how many times do you want the loop to run?
Christian Dale
Christian Dale3mo ago
Where is 'inputvalues' defined?
daniel2
daniel23mo ago
Have a think, the for loop adds number to the list, so how many time you want to add to the list, and what would the condition of the for loop be? Your original code is almost fine, with a slightly change to the for loop
Dread
Dread3mo ago
@daniel2 how do i loop inputValues.Add?
daniel2
daniel23mo ago
originally your code is:
List<int> inputValues = new List<int>();
for (int i = 0; i < inputValues.Count; i++)
{
// add user's input to list
}
List<int> inputValues = new List<int>();
for (int i = 0; i < inputValues.Count; i++)
{
// add user's input to list
}
but currently inputValues.Count is 0 because no element is inside the list so the for loop is not executing so you need to change the condition of the for loop instead of inputValues.Count think how many time you need to loop
Dread
Dread3mo ago
@daniel2 10 loops because i want 10 inputs right?
daniel2
daniel23mo ago
yeahhhhh
for (int i = 0; i < 10; i++)
{

}
for (int i = 0; i < 10; i++)
{

}