Reputation: 3
I am new to programming and I am trying to code my very first OOP program in C# but it does not give the right results.
-The program has to calculate two numbers according to user input.
I tried to code it without repeating myself so probably that is why i don't understand why it does not work.
Program class :
static void Main(string[] args)
{
CalcOperations calcOperations = new CalcOperations();
Console.WriteLine("Welcome to basic OOP calculator! Enter a number. {0}1 - Addition{0}2 - Substraction{0}3 - Multiplication{0}4 - Division", Environment.NewLine);
var getOperation = new double[] {
calcOperations.Counting(),
calcOperations.Subtraction(),
calcOperations.Multiplication(),
calcOperations.Division()
};
var userInput = int.Parse(Console.ReadKey().KeyChar.ToString());
Console.WriteLine();
GetUserInput();
Console.WriteLine(calcOperations.result);
Console.ReadKey();
}
static void GetUserInput()
{
CalcOperations calcOperations = new CalcOperations();
Console.WriteLine("Enter the 1. number: ");
calcOperations.firstNum = double.Parse(Console.ReadLine());
Console.WriteLine("Enter the 2. number: ");
calcOperations.secondNum = double.Parse(Console.ReadLine());
}
-Class for calculating operations
class CalcOperations
{
public double firstNum;
public double secondNum;
public double result;
public double Counting()
{
result = firstNum + secondNum;
return result;
}
public double Subtraction()
{
result = firstNum - secondNum;
return result;
}
public double Multiplication()
{
result = firstNum * secondNum;
return result;
}
public double Division()
{
result = firstNum / secondNum;
return result;
}
}
After giving it two numbers it only says NaN as a result.
Upvotes: 0
Views: 924
Reputation: 3202
For a more root cause of what Diezerk just mentioned, you are making an array of function call results, not the functions themselves. You would want to either use Func delegate and no parentheses (thus forming an array of references to calcOperations' functions), or use a perfectly regular switch
statement to decide on operation to perform.
static void Main(string[] args) {
CalcOperations calcOperations = new CalcOperations();
Console.WriteLine("Welcome to basic OOP calculator! Enter a number. {0}1 - Addition{0}2 - Substraction{0}3 - Multiplication{0}4 - Division", Environment.NewLine);
var getOperation = new Func<double>[]{
calcOperations.Counting,
calcOperations.Subtraction,
calcOperations.Multiplication,
calcOperations.Division
};
var userInput = int.Parse(Console.ReadKey().KeyChar.ToString());
Console.WriteLine();
GetUserInput(calcOperations);
getOperation[userInput - 1]();
Console.WriteLine(calcOperations.result);
Console.ReadKey();
}
static void GetUserInput(CalcOperations calcOperations) {
Console.WriteLine("Enter the 1. number: ");
calcOperations.firstNum = double.Parse(Console.ReadLine());
Console.WriteLine("Enter the 2. number: ");
calcOperations.secondNum = double.Parse(Console.ReadLine());
}
Upvotes: 0
Reputation: 26
Three problems with your code:
You are creating two different object of the class CalcOperations one in your main function and one in your GetUserInput function that gets thrown away as soon as the function has finished executing. Instead send your class object to the function.
You need to gather the user input before calling the functions using the user input data. Simply move the GetUserInput call to before populating your array.
The result variable is populated by all function calls so division being the last call will populate the result variable. Instead print your opposing value in the array.
The following is the corrected code:
static void Main(string[] args)
{
CalcOperations calcOperations = new CalcOperations();
Console.WriteLine("Welcome to basic OOP calculator! Enter a number. {0}1 - Addition{0}2 - Substraction{0}3 - Multiplication{0}4 - Division", Environment.NewLine);
var userInput = int.Parse(Console.ReadKey().KeyChar.ToString());
Console.WriteLine();
GetUserInput(calcOperations);
var getOperation = new double[]{
calcOperations.Counting(),
calcOperations.Subtraction(),
calcOperations.Multiplication(),
calcOperations.Division()
};
Console.WriteLine(getOperation[userInput-1]);
Console.ReadKey();
}
static void GetUserInput(CalcOperations calcOperations)
{
Console.WriteLine("Enter the 1. number: ");
calcOperations.firstNum = double.Parse(Console.ReadLine());
Console.WriteLine("Enter the 2. number: ");
calcOperations.secondNum = double.Parse(Console.ReadLine());
}
edit: remember to verify user inputs as they might be invalid.
Upvotes: 1