0

I would like to write a program where the user will be asked for numbers which will be used to calculate the surface of a square. But I don't know how to "use" the entered numbers in my methods.

Thank you

Here is what I have done so far

static void Main(string[] args)
{
    calculate();
}

public static void Height()
{
    Console.WriteLine("Enter height of the square");
    string enter1 = Console.ReadLine();
    double height = Convert.ToDouble(enter1);
    // height of the square 
}

public static void Length()
{
    Console.WriteLine("Enter length of the square");
    string enter2 = Console.ReadLine();
    double length = Convert.ToDouble(enter2);
    //length of the square 
}

static double calculate(double a, double b)
{
    double summary = a * b;
    Console.WriteLine(summary);
    return summary;
}
Fildor
  • 14,510
  • 4
  • 35
  • 67

2 Answers2

1

Let's return and use the input you have first, to make sure you have some success:

static void Main(string[] args)
{

    calculate(Height(), Length());

}

public double void Height()
{
    Console.WriteLine("Enter height of the square");


   string enter1 = Console.ReadLine();


   return Convert.ToDouble(enter1);


    // height of the square 
}
public static double Length()
{
    Console.WriteLine("Enter length of the square");


    string enter2 = Console.ReadLine();


    return Convert.ToDouble(enter2);

    //length of the square 



}

static double calculate(double a, double b)

{


    double summary = a * b;

    Console.WriteLine(summary);
    return summary;



    }

}

If there are no typos on my part, then this should work for you, but you still have a LOT to learn. You should separate your I/O operations from the business logic that you compute and should only have a thin communication between the two for the sake of reusability and portability. I didn't do it in the code above, it's left for you as a homework.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
1

First I suggest extracting a method (reading double value):

  public static double ReadDouble(string title) { 
    // while (true) - keep on asking until valid value provided
    while (true) {
      if (!string.IsNullOrEmpty(title))
        Console.WriteLine(title);

      // TryParse: user can enter any input here, say, "bla-bla-bla"
      // we don't want any exceptions here
      if (double.TryParse(Console.ReadLine(), out double result))
        return result;

      Console.WriteLine("Sorry, not a valid floating point value; please, try again");
    }  
  }

Then we can implement the main routine:

static void Main(string[] args)
{
    double height = ReadDouble("Enter height of the rectangle");
    double width = ReadDouble("Enter width of the rectangle");
    double square = height * width;  

    Console.WriteLine($"The square of the {width} x {height} rectangle is {square}");

    // Pause (wait for a keypress) for user to read the answer
    Console.ReadKey();
}
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • As you already stated in your comment on the question: If not always width == height, then it's not necessarily a square (referring to naming of vars and prompt messages - I know, I am being nitpicky ;) ). After edit: I suggest using "area" instead of "square". – Fildor May 20 '20 at 15:30
  • _"Sorry, invalid syntax; please, try again"_ - Side note for OP: The information, that something is wrong is important but not always _helpful_. You may want to tell the user, what a _valid_ input should look like. – Fildor May 20 '20 at 15:40