C
C#2y ago
Mekasu0124

❔ ✅ Issues With Type Conversion When Validating User Input

List<string> initialText = new()
{
"What Would You Like To Do?",
"--------------------------",
"** Submit Answer As 1, 2, or 3 **"
};

List<string> initialSelection = new()
{
"1) Play Game",
"2) View Previous Games",
"3) View Statistics",
};

foreach (string item in initialText)
{
Console.WriteLine(item);
};

foreach (string ite in initialSelection)
{
Console.WriteLine(ite);
};

int userSelection;

if (!int.TryParse(Console.ReadLine(), out userSelection))
{
tries--;
Console.WriteLine("Input Must Be An Integer Of 1, 2, or 3. Try Again! " + tries + " Left.");
SelectionScreen(tries);
}
else if (int.TryParse(Console.ReadLine(), out userSelection))
{
int selectedItem = userSelection;

if (initialSelection[selectedItem-1])
{

}
}
List<string> initialText = new()
{
"What Would You Like To Do?",
"--------------------------",
"** Submit Answer As 1, 2, or 3 **"
};

List<string> initialSelection = new()
{
"1) Play Game",
"2) View Previous Games",
"3) View Statistics",
};

foreach (string item in initialText)
{
Console.WriteLine(item);
};

foreach (string ite in initialSelection)
{
Console.WriteLine(ite);
};

int userSelection;

if (!int.TryParse(Console.ReadLine(), out userSelection))
{
tries--;
Console.WriteLine("Input Must Be An Integer Of 1, 2, or 3. Try Again! " + tries + " Left.");
SelectionScreen(tries);
}
else if (int.TryParse(Console.ReadLine(), out userSelection))
{
int selectedItem = userSelection;

if (initialSelection[selectedItem-1])
{

}
}
In this section of code, I print items to the screen and request user input. I'm trying to basically go if user input is not an integer, decrement tries and recall function. If user input is an integer but that integer does not match an index of List<string> initialSelection, then decrement tries and recall function. Otherwise, if user input is an integer and is a correct index of the list, then start next function but I'm having issues with the validation and type conversion. Thanks
20 Replies
JakenVeina
JakenVeina2y ago
I'm having issues with the validation and type conversion.
what issues?
Mekasu0124
Mekasu0124OP2y ago
it's not working? I don't really know how to explain it other than what I'm trying to do
If user input is an integer but that integer does not match an index of List<string> initialSelection, then decrement tries and recall function. Otherwise, if user input is an integer and is a correct index of the list, then start next function
JakenVeina
JakenVeina2y ago
what's not working? which part of that? checking whether the user input is an integer? checking whether it's an index of a List<string>? decrementing and retrying? starting the next function?
Mekasu0124
Mekasu0124OP2y ago
int userSelection;
if (!int.TryParse(Console.ReadLine(), out userSelection))
{
tries--;
Console.WriteLine("Input Must Be An Integer Of 1, 2, or 3. Try Again " + tries + " Left.");
SelectionScreen(tries);
}
int userSelection;
if (!int.TryParse(Console.ReadLine(), out userSelection))
{
tries--;
Console.WriteLine("Input Must Be An Integer Of 1, 2, or 3. Try Again " + tries + " Left.");
SelectionScreen(tries);
}
this works. However, when the user actually puts in an integer, and checking if it's an index of the list like
else if (int.TryParse(Console.ReadLine(), out userSelection))
{
int selectedItem = userSelection; // this works...well no errors in ide
// now checking if it's an index
if (initialSelection[selectedItem - 1])
{}
}
else if (int.TryParse(Console.ReadLine(), out userSelection))
{
int selectedItem = userSelection; // this works...well no errors in ide
// now checking if it's an index
if (initialSelection[selectedItem - 1])
{}
}
it tells me that I Cannot implicitly convert type 'string' to 'bool' but I'm not trying to do that. I'm trying to check if it's an index of the list. like my else if part is to check if the integer entered is an index of the list. Since the index is 0-2 and the user enters a number 1-3, I subtract one from the users input to check if it's an index, but it's not letting me just like if I write it like this
else if (int.TryParse(Console.ReadLine(), out userSelection))
{
if (initialSelection[userSelection-1])
{}
}
else if (int.TryParse(Console.ReadLine(), out userSelection))
{
if (initialSelection[userSelection-1])
{}
}
it tells me the same thing. So I'm having issues converting the users input to an integer. I have also tries to wrap Convert.ToInt32() around user selection and it won't let me do that either so my question is, how do I check if the users input is an integer, and if not -> show error, if so -> check if its an index and if not -> show error while decrementing the same tries variable
checking whether the user input is an integer?
- that part works
checking whether it's an index of a List<string>?
- can't get conversion right to take user input as an integer to then check the index
decrementing and retrying?
- it's not decrementing the same tries variable. Instead it'll decrement one version for when it's not an integer and then reset when it is an integer. It needs to be the same variable
starting the next function?
- no I got that part down like it keeps reading int.TryParse(Console.ReadLine(), out userSelection) as a string instead of an integer so I'm trying to convert it back to an integer so that I can use it to check if the index is correct. I just want to get the users input, check if it's an integer and if so then check if that integer is an index of the list. If it's not an integer then decrement tries and recall function. If it is an integer but not an index, then decrement the same tries count and recall the function. ^
JakenVeina
JakenVeina2y ago
alright so
if (initialSelection[selectedItem - 1])
if (initialSelection[selectedItem - 1])
yes, you are getting a type error here Cannot implicitly convert type 'string' to 'bool' that's becase you are passing a string value to a statement that expects a bool
Mekasu0124
Mekasu0124OP2y ago
ok so then if I Have
int userSelection;

if (int.TryParse(Console.ReadLine(), out userSelection))
{
// do something if input is an integer
}
else
{
// show error message and decrement tries
}
int userSelection;

if (int.TryParse(Console.ReadLine(), out userSelection))
{
// do something if input is an integer
}
else
{
// show error message and decrement tries
}
how do I go inside of my if statement and check if it's a correct index of the list?
JakenVeina
JakenVeina2y ago
what counts as a correct index of the list?
Mekasu0124
Mekasu0124OP2y ago
List<string> initialSelection = new()
{
"1) Play Game",
"2) View Previous Game",
"3) View Statistics"
};
List<string> initialSelection = new()
{
"1) Play Game",
"2) View Previous Game",
"3) View Statistics"
};
if the user inputs a number 1, 2, or 3 then it would subtract 1 from the users input to check if the input matches index 0, 1, or 2. If it doesn't, that means they're out of list index range and the error message needs to show, decrement tries, and recall the function <- or allow the user to try again in other words
JakenVeina
JakenVeina2y ago
so what are the valid index values that you can accept?
Mekasu0124
Mekasu0124OP2y ago
..... 1, 2, or 3
JakenVeina
JakenVeina2y ago
great check for those
Mekasu0124
Mekasu0124OP2y ago
yep great idea
JakenVeina
JakenVeina2y ago
better yet check in a way that accounts for you chaning the list in the future I.E. don't assume the list has 3 items
Mekasu0124
Mekasu0124OP2y ago
int userSelection;

if (int.TryParse(Console.ReadLine(), out userSelection))
{
if (userSelection - 1 >= 0 && userSelection - 1 <= 3)
{
string selectedAction = initialSelection[userSelection - 1];
Console.WriteLine("Selected Action: " + selectedAction);
}
else
{
tries--;
Console.WriteLine("Input Must Be An Integer Of 1, 2, or 3. Try Again. " + tries + " Left.");
SelectionScreen(tries);
}
}
else
{
tries--;
Console.WriteLine("Input Must Be An Integer Of 1, 2, or 3. Try Again. " + tries + " Left.");
SelectionScreen(tries);
}
int userSelection;

if (int.TryParse(Console.ReadLine(), out userSelection))
{
if (userSelection - 1 >= 0 && userSelection - 1 <= 3)
{
string selectedAction = initialSelection[userSelection - 1];
Console.WriteLine("Selected Action: " + selectedAction);
}
else
{
tries--;
Console.WriteLine("Input Must Be An Integer Of 1, 2, or 3. Try Again. " + tries + " Left.");
SelectionScreen(tries);
}
}
else
{
tries--;
Console.WriteLine("Input Must Be An Integer Of 1, 2, or 3. Try Again. " + tries + " Left.");
SelectionScreen(tries);
}
so I did this, but when I put in 4 as a response, it background the console window and told me the list index was out of range inside of the IDE instead of displaying the error message
check in a way that accounts for you chaning the list in the future
I don't know what this means but if I put in 3 for my user input, it works just fine I changed my nested if statement to say if (userSelection >= 0 && userSelection <=4) and it seems to work ok thank you for your help ❤️
JakenVeina
JakenVeina2y ago
that would allow for values 0, 1, 2, 3, and 4 which is not 1, 2, and 3 ergo, exception
Mekasu0124
Mekasu0124OP2y ago
because the menu option shows 1, 2, 3, 4 but the list indexes 0, 1, 2, 3 the return subtracts 1 from the users input to get the correct index
JakenVeina
JakenVeina2y ago
okay so, why is 0 accepted?
Mekasu0124
Mekasu0124OP2y ago
i fixed it to say 1
JakenVeina
JakenVeina2y ago
okay so... then it's fixed
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server