Click here to Skip to main content
15,889,861 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello. I am trying to make a game like dungeons and dragons in c# using a console application. I am trying to use if statements with readlines and it won't let me. this is the code that i put in.

C#
if(Console.ReadLine == ("Attack"))
{
    //Take away health from the goblin.
}
Posted
Comments
PIEBALDconsult 25-Aug-15 10:56am    
Ah, that causes one of those less-helpful compile errors. But a small dose of RTFM should fix you right up.
https://msdn.microsoft.com/en-us/library/system.console.readline(v=vs.110).aspx
Andy Lanng 25-Aug-15 11:26am    
My boss once asked me what RTFM meant. Obviously I replies "Read the Manual". He paused looked confused, then started laughing his ass off :P

OP: Methods need braces '()'. Google Console.ReadLine and you'll find 1000's of answers
Maciej Los 25-Aug-15 13:34pm    
I thought that RTMF means: Read The F...ing Manual! :laugh:

1 solution

For starters, the problem is pretty simple: ReadLine is a method, and that means you need to call it as a method by appending brackets that you could put parameters in:
C#
if(Console.ReadLine() == "Attack")
{
    //Take away health from the goblin.
}
But I woudln't do that anyway.
The reason why not is pretty simple: what if it's "throw" or "runaway" instead? Becasue you don't save the user input, you can't test it for anything else afterwards.
So instead, read teh input, and then check it:
C#
string userResponse = Console.ReadLine();
if (userResponse == "Attack")
{
    //Take away health from the goblin.
}
else if (userResponse == "Runaway")
}
    ...

And I wouldn't do it like that either! :laugh:
C#
string userResponse = Console.ReadLine();
switch (userResponse.ToLower())
    {
    case "attack":
        //Take away health from the goblin.
        ...
        break;
    case "runaway":
        // Run aaaaaaway!
        ...
        break:
    default:
        Console.WriteLine("I'm sorry, I didn't understand that!");
        break;
    }
Try it - you should find it's easier to read.
 
Share this answer
 
Comments
Richard Deeming 25-Aug-15 12:55pm    
The recommendation is to normalize strings to uppercase:
CA1308: Normalize strings to uppercase[^]

Not a huge issue if everything's in English, but some cultures have multiple uppercase characters which map to a single lowercase character. :)
Maciej Los 25-Aug-15 13:35pm    
Agree!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900