C
C#7d ago
MegaMix

Need help on returning values

Hello, I've made a simple function that changes a value to whatever number has been entered by the user, as you can see in the image attached to this thread. It seems to work just fine. What I would like to do though is to have it check to see that if someone didn't input number, then it would repeat the input request for a number until the user gets it right. Or in clearer words, if int.TryParse(userEntered, out int parsedNumber); fails, then the user would be sent right back to the line var userEntered = Console.ReadLine();. Now, my idea would be to do something like this:
c#
int readInput() //Runs functions with whatever variable is entered.
{
Console.WriteLine("Enter number...");
var userEntered = Console.ReadLine(); // User enters in console the number.
while (!int.TryParse(userEntered, out int parsedNumber)); //Keeps looping the next code bock below if the TryParse fails.
Console.WriteLine("Please use numbers only, not words or anything else..."); //Tells the user what went wrong.
userEntered = Console.ReadLine(); //Asks the user to enter the varaible again.
return parsedNumber; //Returns the value of the parsedNumber, so that is now the valuable of the variable entered in the function.
}


var thisWillChange = readInput(); //thisWillChange variable now has the value of parsedNumber that was entered by the user in the readInput function.
Console.WriteLine($"The number entered is {thisWillChange}."); // Console writes what number has been entered.
c#
int readInput() //Runs functions with whatever variable is entered.
{
Console.WriteLine("Enter number...");
var userEntered = Console.ReadLine(); // User enters in console the number.
while (!int.TryParse(userEntered, out int parsedNumber)); //Keeps looping the next code bock below if the TryParse fails.
Console.WriteLine("Please use numbers only, not words or anything else..."); //Tells the user what went wrong.
userEntered = Console.ReadLine(); //Asks the user to enter the varaible again.
return parsedNumber; //Returns the value of the parsedNumber, so that is now the valuable of the variable entered in the function.
}


var thisWillChange = readInput(); //thisWillChange variable now has the value of parsedNumber that was entered by the user in the readInput function.
Console.WriteLine($"The number entered is {thisWillChange}."); // Console writes what number has been entered.
However, there are is one error code it gives when running it:
CS0103 The name 'parsedNumber' does not exist in the current context
CS0103 The name 'parsedNumber' does not exist in the current context
I'm a bit confused, shouldn't there be a parsedNumber variable when it runs while (!int.TryParse(userEntered, out int parsedNumber));? Any help would be greatly appreciated.
No description
6 Replies
Jimmacle
Jimmacle7d ago
it's because you're declaring it inside the while loop condition, which puts it in the scope of the while loop and it doesn't exist outside of it you can declare it before the while loop then assign to it with out parsedNumber instead of out int parsedNumber your loop syntax is also incorrect, C# uses braces and not indentation so your loop is technically empty and the indented code always runs after the loop exits if a specific example helps, this is how it should look:
int readInput() //Runs functions with whatever variable is entered.
{
Console.WriteLine("Enter number...");
var userEntered = Console.ReadLine(); // User enters in console the number.
int parsedNumber;
while (!int.TryParse(userEntered, out parsedNumber)) //Keeps looping the next code bock below if the TryParse fails.
{
Console.WriteLine("Please use numbers only, not words or anything else..."); //Tells the user what went wrong.
userEntered = Console.ReadLine(); //Asks the user to enter the varaible again.
}

return parsedNumber; //Returns the value of the parsedNumber, so that is now the valuable of the variable entered in the function.
}
int readInput() //Runs functions with whatever variable is entered.
{
Console.WriteLine("Enter number...");
var userEntered = Console.ReadLine(); // User enters in console the number.
int parsedNumber;
while (!int.TryParse(userEntered, out parsedNumber)) //Keeps looping the next code bock below if the TryParse fails.
{
Console.WriteLine("Please use numbers only, not words or anything else..."); //Tells the user what went wrong.
userEntered = Console.ReadLine(); //Asks the user to enter the varaible again.
}

return parsedNumber; //Returns the value of the parsedNumber, so that is now the valuable of the variable entered in the function.
}
Angius
Angius7d ago
int readInput()
{
Console.WriteLine("Enter number...");
var userEntered = Console.ReadLine();
while (!int.TryParse(userEntered, out int parsedNumber));
Console.WriteLine("Please use numbers only, not words or anything else...");
userEntered = Console.ReadLine();
return parsedNumber;
}
int readInput()
{
Console.WriteLine("Enter number...");
var userEntered = Console.ReadLine();
while (!int.TryParse(userEntered, out int parsedNumber));
Console.WriteLine("Please use numbers only, not words or anything else...");
userEntered = Console.ReadLine();
return parsedNumber;
}
Firstly, C# is not Python, indentation doesn't matter whatsoever. If you want both lines to run inside of the loop, both need to be within the braces of the loop Secondly, you have a semicolon after the while (); which terminates it
while (true);
Console.WriteLine("hello");
while (true);
Console.WriteLine("hello");
is not equal to
while (true)
{
Console.WriteLine("hello");
}
while (true)
{
Console.WriteLine("hello");
}
but rather
while (true)
{
}
Console.WriteLine("hello");
while (true)
{
}
Console.WriteLine("hello");
Similarly,
while (true)
Console.WriteLine("one");
Console.WriteLine("two");
while (true)
Console.WriteLine("one");
Console.WriteLine("two");
is not the same as
while (true)
{
Console.WriteLine("one");
Console.WriteLine("two");
}
while (true)
{
Console.WriteLine("one");
Console.WriteLine("two");
}
but rather, the same as
while (true)
{
Console.WriteLine("one");
}
Console.WriteLine("two");
while (true)
{
Console.WriteLine("one");
}
Console.WriteLine("two");
MegaMix
MegaMixOP6d ago
Thank you both! Very helpful. It's true, I do come from Python and need to learn C#. It's had some challenges so far. So I added the code as such:
c#
int readInput() //Runs functions with whatever variable is entered.
{
Console.WriteLine("Enter number...");
var userEntered = Console.ReadLine(); // User enters in console the number.
int parsedNumber;
while (!int.TryParse(userEntered, out parsedNumber)) //Keeps looping the next code bock below if the TryParse fails.
{
Console.WriteLine("Please use numbers only, not words or anything else..."); //Tells the user what went wrong.
userEntered = Console.ReadLine(); //Asks the user to enter the varaible again.
}

return parsedNumber; //Returns the value of the parsedNumber, so that is now the valuable of the variable entered in the function.
}

var a = readInput();
Console.WriteLine(a);
c#
int readInput() //Runs functions with whatever variable is entered.
{
Console.WriteLine("Enter number...");
var userEntered = Console.ReadLine(); // User enters in console the number.
int parsedNumber;
while (!int.TryParse(userEntered, out parsedNumber)) //Keeps looping the next code bock below if the TryParse fails.
{
Console.WriteLine("Please use numbers only, not words or anything else..."); //Tells the user what went wrong.
userEntered = Console.ReadLine(); //Asks the user to enter the varaible again.
}

return parsedNumber; //Returns the value of the parsedNumber, so that is now the valuable of the variable entered in the function.
}

var a = readInput();
Console.WriteLine(a);
It turns out that while when I type an integer it works, but when I type a word for a number like say "three" it returns the value 0. At no point does it ask me to re-enter an input if I don't enter an integer.
Jimmacle
Jimmacle6d ago
are you sure that's the code you're running, like the build is successful and everything? because when i copy and paste it into my IDE it works fine:
No description
Jimmacle
Jimmacle6d ago
int.TryParse doesn't parse the word form of numbers, only actual digits
MegaMix
MegaMixOP6d ago
The code actually does work, I messed up. Thank you so much! @Jimmacle !

Did you find this page helpful?