Reputation: 191
I have an object called "operator" in C# with a method that takes two number inputs from a user and adds them together. However, I want to make the second parameter (2nd input) optional so that the default is "4" if the user doesn't enter a second number.
I know something's wrong because it just ends the program rather than using the default if the user enters just one number and hits enter when prompted for second input.
This solution is probably very obvious but it's eluding me. I'd appreciate if someone would take a look at my code and see what I'm missing.
Thank you so much!
program code:
class Program
{
static void Main(string[] args)
{
Operator operatorObject = new Operator();
Console.WriteLine("Pick a number:");
int userValue = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Pick another number--optional");
int userValue2 = Convert.ToInt32(Console.ReadLine());
int result = operatorObject.operate(userValue, userValue2);
Console.WriteLine(result);
Console.ReadLine();
}
}
class code:
public class Operator
{
public int operate(int data, int input=4)
{
return data + input;
}
}
UPDATE: Thank everyone for your answers! I think I've got it working now, due to a combination of suggestions. Your help is much appreciated!
Upvotes: 0
Views: 1913
Reputation: 185
The problem is that you are calling your method with both parameters. You should check whether to pass the second parameter. Something like follows:
public static void Main()
{
Operator operatorObject = new Operator();
Console.WriteLine("Pick a number:");
int userValue = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Pick another number--optional");
int userValue2;
int result;
if(int.TryParse(Console.ReadLine(), out userValue2))
{
result = operatorObject.operate(userValue,userValue2);
}
else
{
result = operatorObject.operate(userValue);
}
Console.WriteLine(result);
Console.ReadLine();
}
Upvotes: 1
Reputation: 37000
If you omit to enter a value the input Console.ReadLine
will return the empty string which surely can´t be converted to an integer.
So in order to enable the parameter to be omitted you need to indicate if the user entered anything at all:
int userValue2, userValue2;
int result;
Console.WriteLine("Pick a number:");
if(!int.TryParse(Console.ReadLine(), out userValue))
throw new ArgumentException("no valid number");
Console.WriteLine("Pick another number--optional");
if(int.TryParse(Console.ReadLine(), out userValue2)
result = operatorObject.operate(userValue, userValue2);
else
result = operator.operate(userValue);
int.TryParse
tries to parse the input provided by user and if parsing fails will return false
. So this also works if user types something completely different like "MyString"
.
Upvotes: 2
Reputation: 819
How about this:
class Program
{
static void Main(string[] args)
{
Operator operatorObject = new Operator();
Console.WriteLine("Pick a number:");
int result = 0;
int userValue;
if (int.TryParse(Console.ReadLine(), out userValue))
{
Console.WriteLine("Pick another number--optional");
int userValue2;
if (int.TryParse(Console.ReadLine(), out userValue2))
{
result = operatorObject.operate(userValue, userValue2);
}
else
{
result = operatorObject.operate(userValue);
}
}
else
{
Main(null);
}
Console.WriteLine(result);
Console.ReadLine();
}
...
}
Upvotes: -1
Reputation: 413
Something like:
static void Main(string[] args)
{
Operator operatorObject = new Operator();
Console.WriteLine("Pick a number:");
var val1 = Console.ReadLine();
int userValue = 0;
if (val1 != null && val1.Length > 0)
{
userValue = Convert.ToInt32(val1);
}
Console.WriteLine("Pick another number--optional");
var val2 = Console.ReadLine();
int userValue = 0;
int userValue2 = 0;
if (val2 != null && val2.Length > 0)
{
userValue2 = Convert.ToInt32(val2);
}
int result = operatorObject.operate(userValue, userValue2);
Console.WriteLine(result);
Console.ReadLine();
}
public class Operator
{
public int operate(int data, int input = 4)
{
return data + input;
}
}
Upvotes: 0
Reputation: 130
class Program
{
static void Main(string[] args)
{
Operator operatorObject = new Operator();
Console.WriteLine("Pick a number:");
int userValue = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Pick another number--optional");
var userValue2IsValid = int.TryParse(Console.ReadLine(), out int userValue2);
int result = 0;
if (userValue2IsValid) {
result = operatorObject.operate(userValue, userValue2);
}
else {
result = operatorObject.operate(userValue);
}
Console.WriteLine(result);
Console.ReadLine();
}
}
Upvotes: 0