Beginner, please help (if statement)

Good morning,

I am trying to write the following program.

So the computer asks the user a question: 5 x 5. If the user is correct, this message should appear: Good answer!

If the user get pissed and insult the computer by saying: "Screw you don't tell me what to do!" this message should appear: "Wow buddy, gotta check that language!"

If the user input anything else, this message should appear: "Dude…what?"

This was my original code:

 { int numero1 = 5; //Declaring first variable int numero2 = 5; //Declaring second variable Console.WriteLine ("What is the answer of " + numero1 + " x " + numero2); //Asking question to the user int answer = Convert.ToInt32(Console.ReadLine()); //Converting the "answer" to integral variable string insult = Console.ReadLine(); //The user enter his insult if (answer == 25) //If the answer is 25 { Console.WriteLine("Good answer!"); //This message appears } else if (insult == "screw you, don't tell me what to do!") //If the user insult me { Console.WriteLine("Wow buddy, gotta check that language!"); //The user receives this message } else { Console.WriteLine("Dude...what?"); //If the user write anything else, he gets this message } Console.ReadKey(); } } } 

Now this code is giving me that error: An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll Additional information: Input string was not in a correct format.

So I went to stack overflow and a user (http://ift.tt/1GnlZnu) told me to use tryparse. I have no idea what it does and I'm trying to understand.

So I rewrote the code and I changed the beginning to this:

int numero1 = 5; //Declaring first variable int numero2 = 5; //Declaring second variable Console.WriteLine ("What is the answer of " + numero1 + " x " + numero2); //Asking question to the user string insult = Console.ReadLine(); int answer; while (!int.TryParse(insult, out answer)) { Console.WriteLine("Wow buddy, gotta check that language!"); insult = Console.ReadLine(); } 

The problem is that now, if I enter a number like 5, it gave me the "Dude what?" message but if I enter a message like "I'm a rebel!", I get the "Wow buddy, gotta check that language!" every times.

What am I doing wrong?

by VitaminNigga via /r/csharp

Leave a Reply