C
C#9mo 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
Jimmacle9mo ago
what's not working and what would "working" look like?
Dread
DreadOP9mo ago
working would look like this
Dread
DreadOP9mo ago
No description
Jimmacle
Jimmacle9mo ago
and what's the current problem?
Dread
DreadOP9mo 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
Pobiega9mo ago
Passing 10 to your list constructor doesn't do what you think it does
Jimmacle
Jimmacle9mo 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
Angius9mo ago
(pssst, use .Add() on the list)
Jimmacle
Jimmacle9mo ago
it's better to guide them through diagnosing the issues instead of just telling them how to fix stuff :when:
Dread
DreadOP9mo ago
do i leave the constructor blank?
Pobiega
Pobiega9mo ago
Yes.
Dread
DreadOP9mo ago
even if the total amount of inputs you want is 10?
Pobiega
Pobiega9mo ago
But read up on what both of those alternatives do
Jimmacle
Jimmacle9mo ago
lists work differently than arrays in terms of how they're sized and how you put new values in
Dread
DreadOP9mo ago
my instructor teaches more by like examples and following along. i struggle when i don’t understand concepts
Pobiega
Pobiega9mo ago
Your code looks like what I would expect for an array, not for a list.
Dread
DreadOP9mo ago
do i use foreach?
Pobiega
Pobiega9mo ago
No. If you want to use a list, read up on how lists work
Jimmacle
Jimmacle9mo 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
DreadOP9mo 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
Angius9mo ago
Well, the loop won't run The list has no elements in it The count is 0
Dread
DreadOP9mo ago
so the loop isn’t even relevant to the list? should i comment it out?
Jimmacle
Jimmacle9mo 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
DreadOP9mo ago
right and i want the user input to loop.
Dread
DreadOP9mo ago
No description
Angius
Angius9mo ago
No description
Angius
Angius9mo ago
Fix that
Jimmacle
Jimmacle9mo ago
we're getting to that
Pobiega
Pobiega9mo ago
so @Dread , how do we add an item to a list?
Dread
DreadOP9mo ago
would i put inputValues.Add
Pobiega
Pobiega9mo ago
Thats a good start so, if I wanted to add... 5 things?
Dread
DreadOP9mo 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
Angius9mo ago
And how would you do that without repeating the same line 5 times?
Dread
DreadOP9mo ago
loop?
Angius
Angius9mo ago
Yes
Dread
DreadOP9mo ago
No description
Dread
DreadOP9mo ago
what does that 5 in the parentheses mean? because there’s 7 things?
Angius
Angius9mo 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
daniel29mo 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
DreadOP9mo 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
Jimmacle9mo ago
there is a red squiggle under the most immediate issue how many times do you want the loop to run?
Christian Dale
Christian Dale9mo ago
Where is 'inputvalues' defined?
daniel2
daniel29mo 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
DreadOP9mo ago
@daniel2 how do i loop inputValues.Add?
daniel2
daniel29mo 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
DreadOP9mo ago
@daniel2 10 loops because i want 10 inputs right?
daniel2
daniel29mo ago
yeahhhhh
for (int i = 0; i < 10; i++)
{

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

}
Want results from more Discord servers?
Add your server