Home > Net >  Why am I getting a wrong output in a console in C#?
Why am I getting a wrong output in a console in C#?

Time:01-17

Im a begginer in learning C# and I'm trying to write a console app where, if you say a certain food, you're a certain type of animal.

For some reason, I am constantly getting the same output, no matter which food I type (I'm getting the output with the bear). Only if I write Pizza, I will get an output for a sloth animal.

Can somebody please take a look at this and tell me where Im wrong? And even if I choose some random food that .IndexOf didnt find, I still get the bear animal!

Console.WriteLine("Select your favorite food and Ill tell you what kind of animal you are!");

string userInput = Console.ReadLine();
int userInput1 = userInput.IndexOf("Hamburger", 0);
int userInput2 = userInput.IndexOf("Pizza", 0);
int userInput3 = userInput.IndexOf("Tuna salad", 0);
bool hamUserInput1 = Convert.ToBoolean(userInput1);
bool pizzaUserInput2 = Convert.ToBoolean(userInput2);
bool tunaUserInput3 = Convert.ToBoolean(userInput3);

if (hamUserInput1 == true)
{
    Console.WriteLine($"Since you chose {userInput} as your food, youre a bear!");
}
else if (pizzaUserInput2 == true)
{
    Console.WriteLine($"Since you chose {userInput} as your food, youre a sloth!");
}
else if (tunaUserInput3 == true)
{
    Console.WriteLine($"Since you chose {userInput} as your food, youre a fish!");
}
else
{
    Console.WriteLine($"Since you chose {userInput} as your food, I can only say that youre a dinosaur!");
}

CodePudding user response:

IndexOf returns -1 when the string passed is not a substring.

Convert.ToBoolean is true when the passed int is not 0.

You can do userInput1 != -1 but String.Contains is what you want.

CodePudding user response:

You should use the method string.Contains For example userInput.Contains("Your Food");

CodePudding user response:

try this

    var foodAnimals = new string[,] { { "Hamburger", "bear" }, 
                        { "Piza", "sloth" }, { "Tuna Salad", "fish"} };

    Console.WriteLine("Select your favorite food and Ill tell you what kind of animal you are!");

    var userInput = Console.ReadLine();

    var animal = "dinosaur";
    for (var i = 0; i < foodAnimals.GetLength(0); i  )
    {
        if (userInput.ToLower().Contains(foodAnimals[i, 0].ToLower()))
        {
            animal = foodAnimals[i, 1];
            break;
        }
    }

    Console.WriteLine($"Since you chose {userInput} as your food, youre a {animal}!");

CodePudding user response:

For some reason, I am constantly getting the same output, no matter which food I type (I'm getting the output with the bear). Only if I write Pizza, I will get an output for a sloth animal.

Can somebody please take a look at this and tell me where Im wrong? And even if I choose some random food that .IndexOf didnt find, I still get the bear animal!

Well, let's try it and see what happens:

User types "Hamburger"

I'll replace the method calls with just straight values

string userInput = "Hamburger"; //Imagine they type Hamburger
int userInput1 = 0;             //Hamburger found at 0 - userInput.IndexOf("Hamburger", 0);
int userInput2 = -1;            //Pizza not found - userInput.IndexOf("Pizza", 0); 
int userInput3 = -1;            //Tuna salad not found - userInput.IndexOf("Tuna salad", 0);
bool hamUserInput1 = false;     //0 converted to boolean is False - Convert.ToBoolean(userInput1);
bool pizzaUserInput2 = true     //-1 converted to boolean is True -Convert.ToBoolean(userInput2);
bool tunaUserInput3 = true      //-1 converted to boolean is True -Convert.ToBoolean(userInput3);

if (false /*hamUserInput1*/ == true)       //NOT DONE, false is not equal to true
{
    Console.WriteLine($"Since you chose {userInput} as your food, youre a bear!");
}
else if (true /*pizzaUserInput2*/ == true) //DONE
{
    Console.WriteLine($"Since you chose {userInput} as your food, youre a sloth!");
}

We don't need to go any further; we found that when we type Hamburger, you're a sloth!

User types "Pizza"

Round 2, here we go!

string userInput = "Pizza";     //Imagine they type Pizza
int userInput1 = -1;            //Hamburger not found - userInput.IndexOf("Hamburger", 0);
int userInput2 = 0;             //Pizza found at 0 - userInput.IndexOf("Pizza", 0); 
int userInput3 = -1;            //Tuna salad not found - userInput.IndexOf("Tuna salad", 0);
bool hamUserInput1 = true;      //-1 converted to boolean is True - Convert.ToBoolean(userInput1);
bool pizzaUserInput2 = true     //0 converted to boolean is False -Convert.ToBoolean(userInput2);
bool tunaUserInput3 = true      //-1 converted to boolean is True -Convert.ToBoolean(userInput3);

if (true /*hamUserInput1*/ == true)       //DONE
{
    Console.WriteLine($"Since you chose {userInput} as your food, youre a bear!");
}

We don't need to go any further; we found that when we type Pizza, you're a bear!

User types "ASDF"

Round 3, here we go!

string userInput = "ASDF";      //Imagine they type ASDF
int userInput1 = -1;            //Hamburger not found - userInput.IndexOf("Hamburger", 0);
int userInput2 = -1;            //Pizza not found - userInput.IndexOf("Pizza", 0); 
int userInput3 = -1;            //Tuna salad not found - userInput.IndexOf("Tuna salad", 0);
bool hamUserInput1 = true;      //-1 converted to boolean is True - Convert.ToBoolean(userInput1);
bool pizzaUserInput2 = true     //-1 converted to boolean is True-Convert.ToBoolean(userInput2);
bool tunaUserInput3 = true      //-1 converted to boolean is True -Convert.ToBoolean(userInput3);

if (true /*hamUserInput1*/ == true)       //DONE
{
    Console.WriteLine($"Since you chose {userInput} as your food, youre a bear!");
}

We don't need to go any further; we found that when we type ASDF, you're a bear!


This is the kind of debugging you do with the VS Debugger. You click a line, you press F9, you press Play..

..then the code stops on that line with the red dot and you can press F10 or F11 to advance it (stepping over or into methods respectively - step over stil lruns a method it just doesn't step into it (can be very tedious if you step into all the time))

You can point to any in-scope variables in the code and see their values at any time

You would see this process; type something, watch the values


Straight up, Convert.ToBoolean of an IndexOf is a bad idea.

  • If the IndexOf returns -1 meaning "not found", the ToBoolean makes it true as if it "was found"
  • If IndexOf returns 0 meaning "found at the start", Convert makes this "not found"
  • If IndexOf returns 1 meaning "found somewhere in the middle", Convert makes this "was found"

So it works some times, but not others - it's terrible, unreliable code. Chuck it away

Look how nice this is:

string userInput = Console.ReadLine();

if (userInput.Contains("Hamburger")){
    Console.WriteLine($"Since you chose Hamburger as your food, you're a bear!");

} else if (userInput.Contains("Pizza")){{
    Console.WriteLine($"Since you chose Pizza as your food, you're a sloth!");
}
else if (userInput.Contains("Tuna salad")){
    Console.WriteLine($"Since you chose Tuna salad as your food, you're a fish!");
}
else{
    Console.WriteLine($"Since you chose {userInput} as your food, I can only say that you're a dinosaur!");
}
  •  Tags:  
  • Related