Unassigned variable in c#

Asked

Viewed 2,252 times

4

The variable in the last console.writeline is giving that has not been assigned being that it was in ifs, I wonder why.

string c, nome;
double s, sn;

Console.WriteLine("Escreva o nome:");
nome = Console.ReadLine();
Console.WriteLine("Escreva o salário:");
s = double.Parse(Console.ReadLine());
Console.WriteLine("Escreva a categoria:");
c = Console.ReadLine();

if (c == "a" || c == "c" || c == "f" || c == "h")
{
    sn = s * 1.10;
}
else if (c == "b" || c == "d" || c == "e" || c == "i" || c == "j" || c == "t")
{
    sn = s * 1.15;
}
else if (c == "k" || c == "r")
{
    sn = s * 1.25;
}
else if (c == "l" || c == "m" || c == "n" || c == "o" || c == "p" || c == "q" || c == "s")
{
    sn = s * 1.35;
}
else if (c == "u" || c == "v" || c == "x" || c == "y" || c == "w" || c == "z")
{
    sn = s * 1.50;
}
else
{
    Console.WriteLine("Categoria inválida!");
}

Console.WriteLine(" Nome: {0}\n Categoria: {1}\n Novo salário: R$ {2:0.00}", nome, c, sn);

Console.ReadKey();
  • The reply was posted by LINQ, but you tagged the Pagotti comment

2 answers

3


Because if the code falls on else the variable will never receive a value.

Just initialize the variable with the default value.

double s, sn = 0;
// ...

Trying to read the code, it seems to me that it would be more reasonable to "cancel" the operation and return on else.

else
{
    Console.WriteLine("Categoria inválida!");
    return;
}
  • Thank you so much for your help, I’ll change that. As I’m still learning I don’t know much, so if you want to pass me the things you said.

  • 1

    I’ll take it off so I don’t compromise for now. Btw, if the answer was helpful you can mark it as correct using the on the left side of the post. And you can always vote positively on the posts you find interesting.

  • 1

    I’d like to suggest, if you’re using Visual Studio, using breakpoints. That way, you can see the path your code takes.

  • @Brunosoares would be better to comment on the question to be notified to AP. Posting in my reply only I receive notification.

  • Thanks for the remark. I’m new on the platform and passed beaten.

1

would like to know why

To answer your question, the reason this message occurs is that there is a path that the code can go through, which is the last else, where the variable sn will not have been assigned to any value and at the end of the program you are showing the value of that variable.

The solution @LINQ gave you to avoid the error is to start the variable with the value 0 at the beginning. Another way would be to put it in the last else an instruction sn = 0;

  • Your reply is actually a comment on the proposal that LINQ had already posted

  • 1

    When @LINQ responded the first time, he replied how to fix the problem, but had not explained why the message occurred. The idea of my answer was complementary to his because he had already given the solution and the person who asked had already thanked him. He edited the answer later and included the explanation, but I don’t know why the person who asked the question chose my answer instead of his.

Browser other questions tagged

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