Reputation: 13
So, I am a beginner C# programmer, and I can't get my program to work. I want to be able to use the Main() method's user input, pass it to the PaintJobCalc() to calculate a paint job, and send the calculation back to the main method. I have been playing with it for an hour and I can't get anywhere.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
class PaintingEstimate
{
static void Main()
{
string[] input = {};
Write("\n Type a room length in feet >> ");
input[0] = ReadLine();
Write("\n Type a room width in feet >> ");
input[1] = ReadLine();
PaintJobCalc((string[])input.Clone());
}
public static void PaintJobCalc(string[] args)
{
int inputOne = Convert.ToInt32(input[0]);
int inputTwo = Convert.ToInt32(input[1]);
var wallCount = (inputOne + inputTwo) * 2;
var squareFootage = wallCount * 9;
var endEstimate = squareFootage * 6;
WriteLine("\n Your Paint Job total will be ${0}", endEstimate);
ReadLine();
}
}
Upvotes: 0
Views: 223
Reputation: 1601
You need to look into this for how to return values from function.
try below code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
class PaintingEstimate
{
static void Main()
{
string[] input = new string[2];
Write("\n Type a room length in feet >> ");
input[0] = ReadLine();
Write("\n Type a room width in feet >> ");
input[1] = ReadLine();
decimal endEstimate =PaintJobCalc((string[])input);
WriteLine("\n Your Paint Job total will be ${0}", endEstimate);
ReadLine();
}
public static double PaintJobCalc(string[] input)
{
int inputOne = Convert.ToInt32(input[0]);
int inputTwo = Convert.ToInt32(input[1]);
var wallCount = (inputOne + inputTwo) * 2;
var squareFootage = wallCount * 9;
var endEstimate = squareFootage * 6;
return endEstimate;
}
}
Upvotes: 0
Reputation: 56473
Firstly, you'll need to return endEstimate
.
public static int PaintJobCalc(string[] args)
{
int inputOne = Convert.ToInt32(args[0]);
int inputTwo = Convert.ToInt32(args[1]);
var wallCount = (inputOne + inputTwo) * 2;
var squareFootage = wallCount * 9;
var endEstimate = squareFootage * 6;
return endEstimate;
}
note - the exchange of return type from void
to int
.
unlike some programming languages C#
arrays are not dynamic you cannot have an empty array then add items onto them and expect them to grow.
string[] input = {}; // this is size 0 and won't grow in size
you should declare arrays like this:
string[] input = new string[2];
now, your main method becomes like this:
static void Main()
{
string[] input = new string[2];
Write("\n Type a room length in feet >> ");
input[0] = ReadLine();
Write("\n Type a room width in feet >> ");
input[1] = ReadLine();
int endEstimate = PaintJobCalc(input);
WriteLine("\n Your Paint Job total will be ${0}", endEstimate);
ReadLine();
}
Upvotes: 0
Reputation: 19179
You don't need array. your method can take two integer parameters and return integer as result. method signature is very important and it must describe input of the method clearly. don't make things ambiguous using wrong signature.
public static int PaintJobCalc(int length, int width)
{
var wallCount = (length + width) * 2;
var squareFootage = wallCount * 9;
var endEstimate = squareFootage * 6;
return endEstimate;
}
static void Main()
{
Write("\n Type a room length in feet >> ");
int length = Convert.ToInt32(ReadLine());
Write("\n Type a room width in feet >> ");
int width = Convert.ToInt32(ReadLine());
var value = PaintJobCalc(length, width);
WriteLine("\n Your Paint Job total will be ${0}", value);
}
Upvotes: 0
Reputation: 16
Since the return type of your method is void you can't return anything back. It's not C# specific, rather it is same in all programming language. Try changing the return type of your method PaintJobCalc to int/float(whichever is suitable for your requirement) and calling it on some int/float variable.
That will work. Good luck
Upvotes: 0