Use of the unassigned local variable of two strings in C#code

Asked

Viewed 342 times

1

My code should be entered the gender of a person, height and age of a person in a repeat loop for 10 people.

The code must show the greater weight, lower weight, average height of 10 people, the amount of men and the gender of the heaviest person.

But there is an error in the use of the unassigned local variable Strings sexo, pessoamaispesada and wanted to know how to fix it

Following the lines of my code that appear the variables sexo, pessoamaispesada

// Declarando as strings
string sexo, pessoamaispesada;

// Parte que pergunta o gênero da pessoa
for (i = 0; i < 10; i++)
{
    Console.Write($"Digite o sexo da pessoa {i} (M - masculino | F - feminino): ");
    sexo = Console.ReadLine();
}

/// Capturar a pessoa de determinado genero com maior peso
for (i = 0; i < 10; i++)
{
    if (peso > maiorpeso)
    {
        maiorpeso = peso;
        sexo = pessoamaispesada; // Uso da variável local não atribuída "pessoamaispesada"
    }
}

//  Contabiliza a quantidade de homens
for (i = 0; i < 10; i++)
{
    if (sexo == "M" || sexo == "m") // Uso da variável local não atribuída "sexo"
    {
        qnthomens++;
    }
}

// Imprimindo a pessoa mais pesada de determinado gênero 
Console.Write($"A pessoa mais pesada é {pessoamaispesada}");
  • It was a bit confusing understanding. Can you post the full code? So you can better see the problem.

  • Your code could be posted in its entirety?

  • @Virgilionovic already posted.

  • Reversed: First you claimed in question the following statement: string sexo, pessoamaispesada; , in the edition you put string sexo = ""; var pessoamaispesada = ""; thus invalidating the answers.

  • 1

    Actually @Augustovasques reversed may even invalidate the answers, but the problem was obscure and it wasn’t because of that. The problem was more internal and in my opinion should have let the user put the whole of the code that yes we could point out the real problem by an answer. The question is bad, it’s a do for me because I lack user experience and I think users post question and think we have to guess I for example hope they put as much information before answering.

2 answers

3

The error is exactly what is written, you did not initialize the variable and tried to use without a value in it, this would potentially give error, so compiling already prevents. So what should you do? Give a value to the variable. Then this would solve:

var sexo = "";
var pesoMaisPesada = "";

I put in the Github for future reference.

The second error should not happen unless the code is being presented in a way completely out of its real state.

I can almost guarantee that it will not solve the total problem of the code that seems to make something meaningless there. I gave only to answer that part.

2


The error is occurring because the variables do not have initialization (initial value defined). Simply assign a value to them as an example below:

string sexo = "";
string pessoamaispesada = "";

The issue is a little confused, but from what I interpreted your running program would look like this (one of the ways to make the method to calculate the heaviest):

public static void VerificaMaisPesado()
{
    int pos = 2;
    string[] sexo = new string[pos];
    string[] nome = new string[pos];
    decimal[] peso = new decimal[pos];

    decimal pesoMax = 0;

    string pessoamaispesada = "";

    for (var i = 0; i < pos; i++)
    {
        Console.Write("Digite o nome da pessoa: ");
        nome[i] = Console.ReadLine();
        Console.Write("Digite o sexo da pessoa {i} (M - masculino | F - feminino): ");
        sexo[i] = Console.ReadLine();
        Console.Write("Digite o peso da pessoa: ");
        peso[i] = decimal.Parse(Console.ReadLine());
    }

    for (int i = 0; i < peso.Length; i++)
    {
        if (peso[i] > pesoMax)
        {
            pesoMax = peso[i];
            pessoamaispesada = nome[i];
        }
    }

    Console.Write("A pessoa mais pesada é {0}", pessoamaispesada);

}

Browser other questions tagged

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