Call a function that returns an array C#

Asked

Viewed 204 times

0

I need the user to enter a number that will be the amount of numbers to be calculated, for example: if I type 4, there will be 4 different numbers to do a given action (add, subtract, multiply or divide). For this I tried to create a function that returns me a array:

public int quantNum(int quantidadeNumeros)
    {
        //Cria a array que vai armazenar os valores a serem calculados.
        int[] numerosCalcular = new int[quantidadeNumeros];

        return numerosCalcular[quantidadeNumeros];
    }

I don’t know if this function is right, why can’t I call it in the main scope of my program that it’s like this:

string mensagemIinicio = "Bem vindo a calculadora do console 0.1 Alpha.";

        Console.WriteLine(mensagemIinicio);
        Console.WriteLine("Por favor insira quantos números você quer calcular.");
        int quantNumCalc = Convert.ToInt32(Console.ReadLine());

        //Verifica se o usuário digitou mais de um número e retorna uma mensagem
        while (quantNumCalc < 2)
        {
            Console.WriteLine("Operação inválida. Digite mais de um número(ERRO 0001)");
            Console.WriteLine("pressione qualquer tecla e aperte enter para tentar novamente.");
            Console.ReadLine();
            Console.Clear();
            Console.WriteLine(mensagemIinicio);
            Console.WriteLine("Por favor insira quantos números você quer calcular.");
            int x = Convert.ToInt32(Console.ReadLine());
            quantNum(x);
        }

1 answer

3

There are several problems in this code, so I recommend trying to learn in a more structured way, learning to do wrong is not learning.

If typing is not an integer your code breaks, I did so in a way that does not let the person type any beyond validating that the number is within the desired range.

Note that I took advantage and eliminated code redundancy, do not do twice what can be done only.

It makes no sense to create a function that does just that, but if you are going to create it anyway then at least give a name that says what it does. Then just create the array without further complications. The error that happens is that the return type is an integer and you are returning a array of integers, therefore completely different objects, so make the return be what is returning.

I also saved the array in a variable, otherwise n~]ao can do nothing with the return.

I would not ask the person to press a key and clear the screen, but I left it so. I would also change some things in the text, but did not move.

It gets better:

using static System.Console;

public class Program {
    public static void Main() {
        WriteLine("Bem vindo a calculadora do console 0.1 Alpha.");
        int quantNumCalc;
        while (true) {
            WriteLine("Por favor insira quantos números você quer calcular.");
            if (!int.TryParse(ReadLine(), out quantNumCalc) || quantNumCalc < 2) {
                WriteLine("Operação inválida. Digite mais de um número(ERRO 0001)");
                WriteLine("pressione qualquer tecla e aperte enter para tentar novamente.");
                ReadLine();
                Clear();
                continue;
            }
            break;
        }
        var array = CriaArray(quantNumCalc);
    }
    
    public static int[] CriaArray(int quantidadeNumeros) => new int[quantidadeNumeros];
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

  • Thanks for the reply my friend! So dude I’m at the beginning of the walk so there’s A LOT;

  • @Brkihel see on [tour] the best way to say thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.