C
C#2y ago
Mekasu0124

✅ Need Help Passing Code Off To Helper Function

this is my current code https://pastebin.com/RFRQshGX. I'm trying to split it off into 1 or 2 helper functions to clean things up a bit. The first helper function needs to check if the users input is an integer and the second function needs to check if the integer is an index of the list, but I can't figure out how to separate it and get the input correct. The helper functions also need to return the correct values too please. Any errors that I have encountered are commented next to the line throwing the error. Thanks.
33 Replies
Mekasu0124
Mekasu0124OP2y ago
function CheckInputIsInteger needs to return an integer and function CheckIfCorrectIndex needs to return a string
pppoe252110
pppoe2521102y ago
public bool CheckInputIsInteger(int tries, string userInput, out int value) public bool CheckIfCorrectIndex(int tries, int userSelection, List<string> correctItems, out string correctItem)
pppoe252110
pppoe2521102y ago
it works like the picture
Mekasu0124
Mekasu0124OP2y ago
ok maybe I need help re-writing my stuff.
public void SelectionScreen(int tries)
{
List<string> initialText = new() {};
List<string> initialSelection = new() {};
foreach (string text in initialText) {};
foreach (string txt in initialSelection) {};

// get user input
// check if input is integer
// check if input is an acceptable index
LaunchGameScreen(tries, selectedAction); //end result
public void SelectionScreen(int tries)
{
List<string> initialText = new() {};
List<string> initialSelection = new() {};
foreach (string text in initialText) {};
foreach (string txt in initialSelection) {};

// get user input
// check if input is integer
// check if input is an acceptable index
LaunchGameScreen(tries, selectedAction); //end result
This is the "brain" function that gathers all of the input and then launches the wanted game screen.
public bool CheckInputIsInteger(int tries, string userInput, out int value)
{
// if userInput is an integer -> return the integer
// else -> decrement tries and allow user to try again
}
public bool CheckInputIsInteger(int tries, string userInput, out int value)
{
// if userInput is an integer -> return the integer
// else -> decrement tries and allow user to try again
}
changing the function to what you suggested, is still telling me that not all code returns a value so how would I write this helper function?
public bool CheckInputIsInteger(int tries, string userInput, out int value)
{
if (int.TryParse(userInput, out value))
{
return value;
}
}
public bool CheckInputIsInteger(int tries, string userInput, out int value)
{
if (int.TryParse(userInput, out value))
{
return value;
}
}
taking it one step down, return value; tells me that I cannot convert int to bool
pppoe252110
pppoe2521102y ago
(sorry if i wrote something wrong, i'm not use google translate now) so, you made a little mistake, you just need to return something in every if else condition. just imagine situation if your function needs to return something where in "if" you return true, but what happens if there is no return in else? yes, this code won't compile, cus your function can't return you what you need
public bool CheckInputIsInteger(int tries, string userInput, out int value)
{
if (int.TryParse(userInput, out value))
{
//value is value
return true;
}
}
public bool CheckInputIsInteger(int tries, string userInput, out int value)
{
if (int.TryParse(userInput, out value))
{
//value is value
return true;
}
}
Mekasu0124
Mekasu0124OP2y ago
right. I get that part.
pppoe252110
pppoe2521102y ago
do not forget to add
return false
return false
after if(){} maybe you should take out tries as a variable? you use it in every method
Mekasu0124
Mekasu0124OP2y ago
This is my original code https://pastebin.com/WXJhPwBE. It works just how it is, but I'm trying to lighten the load of this function and clean the code up a bit. Specifically this part
int userSelection;
if (int.TryParse(Console.ReadLine(), out userSelection))
{
if (userSelection >= 0 && userSelection <= 3)
{
string selectedAction = initialSelection[userSelection - 1].ToLower().Split(")")[1].Trim();
LaunchGameScreen(tries, selectedAction);
}
else
{
tries--;
if (tries > 0)
{
Console.WriteLine("\nInput Must Be An Integer Of 1, 2, or 3. Try Again. " + tries + " Left.");
SelectionScreen(tries);
}
else
{
Console.WriteLine("\nInvalid Input 3 Times. Going Back To Welcome Screen.");
Thread.Sleep(2000);
Console.Clear();
tries = 3;
Program program = new();
program.WelcomeScreen();
}
}
}
else
{
tries--;
if (tries > 0)
{
Console.WriteLine("\nInput Must Be An Integer Of 1, 2, or 3. Try Again. " + tries + " Left.");
SelectionScreen(tries);
}
else
{
Console.WriteLine("\nInvalid Input 3 Times. Going Back To Welcome Screen.");
Thread.Sleep(2000);
Console.Clear();
tries = 3;
Program program = new();
program.WelcomeScreen();
}
}
int userSelection;
if (int.TryParse(Console.ReadLine(), out userSelection))
{
if (userSelection >= 0 && userSelection <= 3)
{
string selectedAction = initialSelection[userSelection - 1].ToLower().Split(")")[1].Trim();
LaunchGameScreen(tries, selectedAction);
}
else
{
tries--;
if (tries > 0)
{
Console.WriteLine("\nInput Must Be An Integer Of 1, 2, or 3. Try Again. " + tries + " Left.");
SelectionScreen(tries);
}
else
{
Console.WriteLine("\nInvalid Input 3 Times. Going Back To Welcome Screen.");
Thread.Sleep(2000);
Console.Clear();
tries = 3;
Program program = new();
program.WelcomeScreen();
}
}
}
else
{
tries--;
if (tries > 0)
{
Console.WriteLine("\nInput Must Be An Integer Of 1, 2, or 3. Try Again. " + tries + " Left.");
SelectionScreen(tries);
}
else
{
Console.WriteLine("\nInvalid Input 3 Times. Going Back To Welcome Screen.");
Thread.Sleep(2000);
Console.Clear();
tries = 3;
Program program = new();
program.WelcomeScreen();
}
}
This is 2 separate actions. The outer if statement is checking if the user entered an integer and if not, then it's allowing them to try again up to 3 times and the nested if statement is checking if the integer the user entered is of an allowable index. I'm wanting to put the outer if statement into one function and the nested if statement into a second function. The only difference is, the functions need to return the correct values.
pppoe252110
pppoe2521102y ago
can i get the whole script and do the refactoring?
Mekasu0124
Mekasu0124OP2y ago
so like
public int CheckIfInputIsInteger(int tries, string userInput)
{
if the input is an integer then return the integer
if the input is not an integer, then decrement tries and allow user to try again
}
public int CheckIfInputIsInteger(int tries, string userInput)
{
if the input is an integer then return the integer
if the input is not an integer, then decrement tries and allow user to try again
}
this function would match
if (int.TryParse(Console.ReadLine(), out userSelection))
{
// rest of code
}
else
{
tries--;
if (tries > 0)
{
Console.WriteLine("\nInput Must Be An Integer. Try Again. " + tries + " Left.");
SelectionScreen(tries);
}
else
{
Console.WriteLine("\nInvalid Input 3 Times. Returning To Welcome Screen");
Thread.Sleep(2000);
Console.Clear();
Program program = new();
program.WelcomeScreen();
}
}
if (int.TryParse(Console.ReadLine(), out userSelection))
{
// rest of code
}
else
{
tries--;
if (tries > 0)
{
Console.WriteLine("\nInput Must Be An Integer. Try Again. " + tries + " Left.");
SelectionScreen(tries);
}
else
{
Console.WriteLine("\nInvalid Input 3 Times. Returning To Welcome Screen");
Thread.Sleep(2000);
Console.Clear();
Program program = new();
program.WelcomeScreen();
}
}
and then the second function
public string CheckCorrectIndex(int tries, int userInput, List<string> list)
{
if the number the user gave minus 1 is a correct index, then return the string value of the list -> return list[userInput-1];
if the number the user gave minux 1 is not a correct index, then decrement tries and allow user to try again
}
public string CheckCorrectIndex(int tries, int userInput, List<string> list)
{
if the number the user gave minus 1 is a correct index, then return the string value of the list -> return list[userInput-1];
if the number the user gave minux 1 is not a correct index, then decrement tries and allow user to try again
}
this function would match
if (userSelection >= 0 && userSelection <= 3)
{
string selectedAction = initialSelection[userSelection-1].ToLower().Split(")")[1].Trim();
}
else
{
tries--;
if (tries > 0)
{
Console.WriteLine("\nInput Must Be An Integer 1, 2, or 3. Try Again. " + tries + " Left.");
SelectionScreen(tries);
}
else
{
Console.WriteLine("\nInvalid Input 3 Times Returning To Welcome Screen.");
Thread.Sleep(2000);
Console.Clear();
Program program = new();
program.WeclomeScreen();
}
if (userSelection >= 0 && userSelection <= 3)
{
string selectedAction = initialSelection[userSelection-1].ToLower().Split(")")[1].Trim();
}
else
{
tries--;
if (tries > 0)
{
Console.WriteLine("\nInput Must Be An Integer 1, 2, or 3. Try Again. " + tries + " Left.");
SelectionScreen(tries);
}
else
{
Console.WriteLine("\nInvalid Input 3 Times Returning To Welcome Screen.");
Thread.Sleep(2000);
Console.Clear();
Program program = new();
program.WeclomeScreen();
}
so I'm trying to break that giant if statement up into 2 functions to clean the code up a bit
public int CheckInputIsInteger(int tries, string userInput, out int value)
{
if (int.TryParse(userInput, out value))
{
return value;
}
else
{
tries--;

if (tries > 0)
{
Console.WriteLine("\nInput Must Be An Integer. Try Again. " + tries + " Left.");
SelectionScreen(tries);
}
else
{
Console.WriteLine("\nInvalid Input 3 Times. Returning To Welcome Screen.");
Thread.Sleep(2000);
Console.Clear();

Program program = new();
program.WelcomeScreen();
}
}
}
public int CheckInputIsInteger(int tries, string userInput, out int value)
{
if (int.TryParse(userInput, out value))
{
return value;
}
else
{
tries--;

if (tries > 0)
{
Console.WriteLine("\nInput Must Be An Integer. Try Again. " + tries + " Left.");
SelectionScreen(tries);
}
else
{
Console.WriteLine("\nInvalid Input 3 Times. Returning To Welcome Screen.");
Thread.Sleep(2000);
Console.Clear();

Program program = new();
program.WelcomeScreen();
}
}
}
so something like this, but this doesn't have a return value for all code lines so do I just return a random number? ok I guess so. I answered my own question lol
pppoe252110
pppoe2521102y ago
just return 0; and you didn't get how to use out
Mekasu0124
Mekasu0124OP2y ago
what do you mean? ?
pppoe252110
pppoe2521102y ago
public bool CheckInputIsInteger(out int value)
{
if (int.TryParse(userInput, out value))
{
return true;
}
else
{
tries--;

if (tries > 0)
{
Console.WriteLine("\nInput Must Be An Integer. Try Again. " + tries + " Left.");
SelectionScreen(tries);
}
else
{
Console.WriteLine("\nInvalid Input 3 Times. Returning To Welcome Screen.");
Thread.Sleep(2000);
Console.Clear();

Program program = new();
program.WelcomeScreen();
}
}
return false;
}
public bool CheckInputIsInteger(out int value)
{
if (int.TryParse(userInput, out value))
{
return true;
}
else
{
tries--;

if (tries > 0)
{
Console.WriteLine("\nInput Must Be An Integer. Try Again. " + tries + " Left.");
SelectionScreen(tries);
}
else
{
Console.WriteLine("\nInvalid Input 3 Times. Returning To Welcome Screen.");
Thread.Sleep(2000);
Console.Clear();

Program program = new();
program.WelcomeScreen();
}
}
return false;
}
public void SelectionScreen()
{
Console.WriteLine(
@$"What Would You Like To Do?{"\n"}
--------------------------{"\n"}
** Submit Answer As 1, 2, or 3 **");

Console.WriteLine(
@$"1) Play Game{"\n"}
2) View Previous Games{"\n"}
3) View Statistics");

}
public void SelectionScreen()
{
Console.WriteLine(
@$"What Would You Like To Do?{"\n"}
--------------------------{"\n"}
** Submit Answer As 1, 2, or 3 **");

Console.WriteLine(
@$"1) Play Game{"\n"}
2) View Previous Games{"\n"}
3) View Statistics");

}
Mekasu0124
Mekasu0124OP2y ago
I need the function to return the integer, not true/false
pppoe252110
pppoe2521102y ago
pppoe252110
pppoe2521102y ago
it returns value
if (CheckInputIsInteger(userInput, out var value))
{
//do stuff with value
}
else
{
//not int
}
if (CheckInputIsInteger(userInput, out var value))
{
//do stuff with value
}
else
{
//not int
}
Mekasu0124
Mekasu0124OP2y ago
public bool CheckInputIsInteger(int tries, string userInput, out int value)
{
if (int.TryParse(userInput, out value))
{
return true;
}
else
{
tries--;

if (tries > 0)
{
Console.WriteLine("\nInput Must Be An Integer. Try Again. " + tries + " Left.");
SelectionScreen(tries);
}
else
{
Console.WriteLine("\nInvalid Input 3 Times. Returning To Welcome Screen.");
Thread.Sleep(2000);
Console.Clear();

Program program = new();
program.WelcomeScreen();
}
}
return false;
}

public void SelectionScreen(int tries)
{
int userSelection = CheckInputIsInteger(tries, Console.ReadLine(), out userSelection); // cannot convert type bool to int
}
public bool CheckInputIsInteger(int tries, string userInput, out int value)
{
if (int.TryParse(userInput, out value))
{
return true;
}
else
{
tries--;

if (tries > 0)
{
Console.WriteLine("\nInput Must Be An Integer. Try Again. " + tries + " Left.");
SelectionScreen(tries);
}
else
{
Console.WriteLine("\nInvalid Input 3 Times. Returning To Welcome Screen.");
Thread.Sleep(2000);
Console.Clear();

Program program = new();
program.WelcomeScreen();
}
}
return false;
}

public void SelectionScreen(int tries)
{
int userSelection = CheckInputIsInteger(tries, Console.ReadLine(), out userSelection); // cannot convert type bool to int
}
that's why I need it to return the integer value, not true/false
pppoe252110
pppoe2521102y ago
........ dude out userSelection is your value
bool valueIsInt = CheckInputIsInteger(tries, Console.ReadLine(), out userSelection);
bool valueIsInt = CheckInputIsInteger(tries, Console.ReadLine(), out userSelection);
or just use global variables so you don't even need to return anything
Mekasu0124
Mekasu0124OP2y ago
ok so the one thing that i was missing was changing my variable cast. ok so onto the next part
pppoe252110
pppoe2521102y ago
int tries;

public void SelectionScreen()
{
if (int.TryParse(userInput, out var input))
{

}
else
{
tries--;
InputInvalid();
}
}

public void InputInvalid()
{
if (tries > 0)
{
Console.WriteLine($"\nInput Must Be An Integer. Try Again. {tries} Left.");
SelectionScreen(tries);
}
else
{
Console.WriteLine("\nInvalid Input 3 Times. Returning To Welcome Screen.");
Thread.Sleep(2000);
Console.Clear();

Program program = new();
program.WelcomeScreen();
}
}
int tries;

public void SelectionScreen()
{
if (int.TryParse(userInput, out var input))
{

}
else
{
tries--;
InputInvalid();
}
}

public void InputInvalid()
{
if (tries > 0)
{
Console.WriteLine($"\nInput Must Be An Integer. Try Again. {tries} Left.");
SelectionScreen(tries);
}
else
{
Console.WriteLine("\nInvalid Input 3 Times. Returning To Welcome Screen.");
Thread.Sleep(2000);
Console.Clear();

Program program = new();
program.WelcomeScreen();
}
}
Mekasu0124
Mekasu0124OP2y ago
I implemented that
Mekasu0124
Mekasu0124OP2y ago
so now to get the second function to work right https://pastebin.com/FMauagFy CheckCorrectIndex
Pastebin
public void InputInvalid(int tries){ if (tries > 0) { ...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
pppoe252110
pppoe2521102y ago
so add some code there cus i don't understand what to do
Mekasu0124
Mekasu0124OP2y ago
public bool CheckCorrectIndex(int tries, int userInput, List<string> listObject, out string value)
{
if (userInput >= 0 && userInput <= 3)
{
value = listObject[userInput - 1];
return value;
}
else
{
tries--;
InputInvalid(tries);
}
return false;
}
public bool CheckCorrectIndex(int tries, int userInput, List<string> listObject, out string value)
{
if (userInput >= 0 && userInput <= 3)
{
value = listObject[userInput - 1];
return value;
}
else
{
tries--;
InputInvalid(tries);
}
return false;
}
this is what I have so far, but I'm not sure how to write it so that I can get it to return correctly
pppoe252110
pppoe2521102y ago
what kind of stuff you need to return
public bool CheckCorrectIndex(int tries, int userInput, List<string> listObject, out string value)
{
if (userInput >= 0 && userInput <= 3)
{
value = listObject[userInput - 1];
return true;
}
else
{
tries--;
InputInvalid(tries);
}
return false;
}
public bool CheckCorrectIndex(int tries, int userInput, List<string> listObject, out string value)
{
if (userInput >= 0 && userInput <= 3)
{
value = listObject[userInput - 1];
return true;
}
else
{
tries--;
InputInvalid(tries);
}
return false;
}
pppoe252110
pppoe2521102y ago
stop doing this pls
pppoe252110
pppoe2521102y ago
Mekasu0124
Mekasu0124OP2y ago
tries is instantiated in my first program file. it's passed around from the primary function throughout the entire program. It's not going to be global
pppoe252110
pppoe2521102y ago
than put in your class not in every method. sorry, but it's stupid
Mekasu0124
Mekasu0124OP2y ago
that's your opinion this keeps telling me The out parameter 'value' must be assigned to before control leaves the current method so should I just put value=null; above the return false; line?
pppoe252110
pppoe2521102y ago
anyway you can do what you want
public bool CheckCorrectIndex(int tries, int userInput, List<string> listObject, out string value)
{
if (userInput >= 0 && userInput <= 3)
{
value = listObject[userInput - 1];
return true;
}
else
{
tries--;
InputInvalid(tries);
}
value = null;
return false;
}
public bool CheckCorrectIndex(int tries, int userInput, List<string> listObject, out string value)
{
if (userInput >= 0 && userInput <= 3)
{
value = listObject[userInput - 1];
return true;
}
else
{
tries--;
InputInvalid(tries);
}
value = null;
return false;
}
Mekasu0124
Mekasu0124OP2y ago
ty for your help ❤️
public void WelcomeScreen()
{
List<string> welcomeText = new()
{
"Welcome To Mek's Math Game!",
"---------------------------",
"Rules: ",
"1) All Answers Are In The Form Of An Integer, or Whole Number.",
"2) If Your Division Game Gives You A Decimal Answer Like: 0.86, Then Your Answer Is 0.",
"3) This Game Has 3 Difficulties: Easy, Medium, and Hard.",
"4) None Of The Numbers You Encounter Go Above 100.",
"5) If You Consistently Receive An Error Even When Input Is Correct, Then Contact",
"Support At: https://github.com/mekasu0124/MeksMathGame/issues"
};

foreach (string text in welcomeText)
{
Console.WriteLine(text);
};

int tries = 3;
GetUserReady(tries);
}

static void Main(string[] args)
{
Program program = new();
program.WelcomeScreen();
}
public void WelcomeScreen()
{
List<string> welcomeText = new()
{
"Welcome To Mek's Math Game!",
"---------------------------",
"Rules: ",
"1) All Answers Are In The Form Of An Integer, or Whole Number.",
"2) If Your Division Game Gives You A Decimal Answer Like: 0.86, Then Your Answer Is 0.",
"3) This Game Has 3 Difficulties: Easy, Medium, and Hard.",
"4) None Of The Numbers You Encounter Go Above 100.",
"5) If You Consistently Receive An Error Even When Input Is Correct, Then Contact",
"Support At: https://github.com/mekasu0124/MeksMathGame/issues"
};

foreach (string text in welcomeText)
{
Console.WriteLine(text);
};

int tries = 3;
GetUserReady(tries);
}

static void Main(string[] args)
{
Program program = new();
program.WelcomeScreen();
}
to show what I mean about the tries variable is it's instantiated here and passed around. It's written once and re-used every time
pppoe252110
pppoe2521102y ago
Want results from more Discord servers?
Add your server