-1

We have assignment where we are supposed to code console application, which counts two values, perimeter and volume of triangle and regular hexagon. User should input values accordingly. We should use menu, where user selects which shape he wants to count. As menu i used switch case. I defined functions to count the volume and perimeter outside of main function. Now the problem i have is, that i dont know how to call the functions out of my cases. Hope i described it correctly.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp7
{
  class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Daniel Nosek");
        Console.WriteLine("Výpočet obvodu a obsahu - trojúhelník, pravidelný šestiúhelník");
        Console.WriteLine("Zvolte si obrazec:");
        Console.WriteLine("1 - trojúhelník");
        Console.WriteLine("2 - pravidelný šestiúhelník");

        int VolbaObrazce = int.Parse(Console.ReadLine());
        double obvod = 0;
        double obsah = 0;
        switch (VolbaObrazce)
        {
            case 1:
                Console.WriteLine("Zadejte délku strany a:");
                float a = float.Parse(Console.ReadLine());
                Console.WriteLine("Zadejte délku strany b:");
                float b = float.Parse(Console.ReadLine());
                Console.WriteLine("Zadejte délku strany c:");
                float c = float.Parse(Console.ReadLine());

                obvod = ObvodTrojuhelniku(a, b, c);
                obsah = ObsahTrojuhelniku(a, b, c, s);
                break;

            case 2:
                Console.WriteLine("Zadejte délku strany d:");
                float d = float.Parse(Console.ReadLine());

                obvod = ObvodSestiuhelniku(d);
                obsah = ObsahSestiuhelniku(d);
                break;
        }


    }

    static int ObvodTrojuhelniku(int a, int b, int c) // vypocet obvodu pomoci souctu stran
    {
        return a + b + c;
    }

    static double ObsahTrojuhelniku(int a, int b, int c, int s) // obsah pomoci heronova vzorce
    {
        s = (a + b + c) / 2;
        return (double)Math.Sqrt(s * (s - a) * (s - b) * (s - c));
    }

    static int ObvodSestiuhelniku(int d) // obvod sestiuhelniku
    {
        return 6 * d;
    }
    static double ObsahSestiuhelniku(int d) // obsah sestiuhelniku
    {
        return ((3 * Math.Sqrt(3) * Math.Pow(d, 2))) / 2;
    }

}

}

  • 5
    You're trying to pass floats to methods taking integers as parameters. – Tobias Tengler Nov 30 '18 at 20:43
  • 2
    What do you mean by *"call the functions out of my cases"*? What is wrong with the code you posted? – UnholySheep Nov 30 '18 at 20:43
  • @UnholySheep case 1 should call method ObvodTrojuhelniku and ObsahTrojuhelniku with values input from user, then write the result with console.writeline, and case 2 should do the same with ObvodSestiuhelniku and ObsahSestiuhelniku – Daniel Nosek Nov 30 '18 at 20:45
  • So your code does that - where is the issue? Perhaps your forgot to write the results? – NetMage Nov 30 '18 at 20:46
  • @mjwills it has errors. Not sure bout the translation, since im Czech, "unable to transfer arguments from float to int. – Daniel Nosek Nov 30 '18 at 20:50
  • Possible duplicate of [Convert Float to Int](https://stackoverflow.com/questions/21896580/convert-float-to-int) – mjwills Nov 30 '18 at 20:52
  • @mjwills kinda is, but i think we are supposed to do the code without needing to convert. – Daniel Nosek Nov 30 '18 at 20:57
  • @DanielNosek That isn't possible if you have a `float` involved. But what if a `float` **wasn't** involved? `float.Parse` got you a `float`. How might you get an `int` instead? – mjwills Nov 30 '18 at 21:01
  • Also, `ObsahTrojuhelniku` looks like it is a triangle area calculation. Note it has some oddities. Firstly, there is no point passing in `s`. Secondly, be sure to test it with `a`, `b` and `c` values of 3 (all of them, `3`) - it will not work correctly for that input due to https://stackoverflow.com/questions/10851273/why-does-integer-division-in-c-sharp-return-an-integer-and-not-a-float . – mjwills Nov 30 '18 at 21:03
  • @mjwills exactly, its triangle area. How else i would do it without S when knowing only lenght of the three sides? – Daniel Nosek Nov 30 '18 at 21:14
  • @DanielNosek Your currently implementation will produce the same result whether you pass in `s` of 100 or 1001. As such, `s` should be a variable declared **inside** the function. – mjwills Nov 30 '18 at 21:18

1 Answers1

0

Here, i fixed your parameter issues and i also took the freedom to switch up some of your Types:

static void Main(string[] args)
{
    Console.WriteLine("Daniel Nosek");
    Console.WriteLine("Výpočet obvodu a obsahu - trojúhelník, pravidelný šestiúhelník");
    Console.WriteLine("Zvolte si obrazec:");
    Console.WriteLine("1 - trojúhelník");
    Console.WriteLine("2 - pravidelný šestiúhelník");

    int VolbaObrazce = int.Parse(Console.ReadLine());
    double obvod = 0;
    double obsah = 0;

    bool recalculate = false;

    do
    {
        Console.Clear();

        switch (VolbaObrazce)
        {
            case 1:
                double a = ReadInVariable("Zadejte délku strany a:");
                double b = ReadInVariable("Zadejte délku strany b:");
                double c = ReadInVariable("Zadejte délku strany c:");

                obvod = ObvodTrojuhelniku(a, b, c);
                obsah = ObsahTrojuhelniku(a, b, c);
                break;

            case 2:
                double d = ReadInVariable("Zadejte délku strany d:");

                obvod = ObvodSestiuhelniku(d);
                obsah = ObsahSestiuhelniku(d);
                break;
            default:
                return;
        }

        // Output your results
        Console.WriteLine("obvod: " + Math.Round(obvod, 2, MidpointRounding.AwayFromZero)); // this will round to 2 digits after the .
        Console.WriteLine("obsah: " + Math.Round(obsah, 2, MidpointRounding.AwayFromZero));

        // check if the read in variable is equal to 1 and write that (true or false)
        // into the variable 'recalculate'
        recalculate = ReadInVariable("If you want to recalculate, enter 1:") == 1;
    }
    while(recalculate);
}

static double ReadInVariable(string text)
{
    Console.Write(text);
    return double.Parse(Console.ReadLine());
}

static double ObvodTrojuhelniku(double a, double b, double c) // vypocet obvodu pomoci souctu stran
{
    return a + b + c;
}

static double ObsahTrojuhelniku(double a, double b, double c) // obsah pomoci heronova vzorce
{
    double s = (a + b + c) / 2;
    return Math.Sqrt(s * (s - a) * (s - b) * (s - c));
}

static double ObvodSestiuhelniku(double d) // obvod sestiuhelniku
{
    return 6 * d;
}

static double ObsahSestiuhelniku(double d) // obsah sestiuhelniku
{
    return ((3 * Math.Sqrt(3) * Math.Pow(d, 2))) / 2;
}

I decided to switch the return type of the methods ObvodTrojuhelniku and ObvodSestiuhelniku from int to double, since you store them in a double anyways and it would just produce unnecessary casting.

I also changed your read-in variables from float to double since it makes everything a little easier and i don't see a point why you should be using float over double unless it's an requirement.

EDIT: I improved the code a little more by extracting the "reading in a variable"-functionality.

EDIT 2: Added Math.Round and the ability to recalculate values.

If you have any questions feel free to ask.

Tobias Tengler
  • 6,848
  • 4
  • 20
  • 34
  • Well thanks for the help, i see where i was wrong now. Few questions. Since every case has different output, should not i write outputs separately in each case? – Daniel Nosek Nov 30 '18 at 21:12
  • @DanielNosek no you don't have to do that. Since you declared `obvod` and `obsah` outside of the `switch` the values are updated by each case respectively. So you can just print them out after the switch, saving you from writing duplicate code. – Tobias Tengler Nov 30 '18 at 21:16
  • Thanks so much. Had no idea. I had just two courses on c sharp. Was making it unnecessary harder. Got another question, for what exactly you added this function static double ReadInVariable(string text) ? – Daniel Nosek Nov 30 '18 at 21:25
  • @DanielNosek that's simple. If you look at your old code from the question, you will see that you are repeating yourself very often by doing `Console.WriteLine()` and reading in a variable. And since the only thing that really changes in these repetitions is the text in `Console.WriteLine` you can extract these repetitions into a single method and pass it the changing variable of `text`. It might not matter now, but think about when you want to read in say 10 variables, it would likely get messy really quick. – Tobias Tengler Nov 30 '18 at 21:30
  • Well, had to just stare for a while at the code to understand but i get it now.. Also i was wondering about rounding, i know that there is math.round, actually im not even sure if we need to use it or no, and also i was wondering where exactly i would use it, in the functions return? ..And last question, or atleast an pointer, at the end, user should decide if he wants to quit program or count again. I was not looking into this yet, some if,else stuff would be okay, or? – Daniel Nosek Nov 30 '18 at 21:40
  • @DanielNosek I updated my answer with the answers to your follow up questions. But i don't think this is the place to explain Loops and such to you, i would say continue your studies and if you have a hard problem you can make a new question on StackOverflow. But for now, if my original answer helped you out, mark it as the answer to close your question, because this is getting into topics unrelated to your original questions. Have a good day and greetings from Germany to the Czech Republic :) – Tobias Tengler Nov 30 '18 at 21:52