none
Very simple Boolean prompt in C# RRS feed

  • Question

  • Hi! I'm extremely new to coding, I just started today, and I am just messing around with the basics trying to teach myself C#. So far I am noodling with if statements and booleans and I decided to create a program that displayed the famous Epicurus logic about God and Evil. I figured it would be a good place to start as it's essentially just a series of simple if statements.

    So I did this:

     bool abletopreventevil = false;
     bool willingtopreventevil = true;
    
                if (willingtopreventevil && !abletopreventevil)
                {
                    Console.WriteLine("Then he is not omnipotent");
                } 
                else if (!willingtopreventevil && abletopreventevil)
                {
                    Console.WriteLine("Then he is Malovelent");
                }
                else if (willingtopreventevil && abletopreventevil)
                {
                    Console.WriteLine("Then Whence cometh evil?");
                } 
                else
                {
                    Console.WriteLine("Then why call him God?");
                }
                
                
                Console.ReadLine();

    Which I was really pleased with and which works. The problem is that I have to input the true or false values into the code and then run the program to have it spit out the answer, so I decided to try and learn how to make it so that the program would prompt the user with the questions 'is God able to prevent evil?' and 'is God willing to prevent evil?' and then spit out the correct text depending on the inputs. I have been trying to do this for 2 hours now while searching for the answer on Google but to no avail. I have been trying to do stuff like this:

     Console.WriteLine("Is God willing to prevent evil? [Y/N]: ");
                string answer1 = Console.ReadLine();
    
    
                if (answer1 == "Y")
                {
                 bool willingtopreventevil = true;
                }
                

    But it doesn't do anything. I know I can use that same thing to save some variables but it doesn't seem to work for boolean stuff. Basically I'm looking for a quick lesson in how to go about achieving this, or a tutorial or other resource that explains it. I know this is very simple and I apologise for that, I wouldn't have asked if I could find it elsewhere. I'm really enjoying learning though, so I appreciate any help.

    Thanks!

    Tuesday, March 26, 2019 4:08 PM

Answers

  • Perhaps this will help

    using System;
    using static System.Console;
    
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
                bool answer = false;
                WriteLine("Is God willing to prevent evil? [Y/N]: ");
    
                try
                {
                    answer = ReadLine().ToBool();
                    WriteLine(answer);
                }
                catch (Exception)
                {
                    WriteLine("Invalid input");
    
                }
                
                ReadLine();
            }
    
    
        }
    
        static class Extensions
        {
            public static bool ToBool(this string input)
            {
                if (input.Equals("y", StringComparison.OrdinalIgnoreCase))
                {
                    return true;
                }
                else if (input.Equals("n", StringComparison.OrdinalIgnoreCase))
                {
                    return false;
                }
                else
                {
                    throw new Exception("The data is not in the correct format.");
                }
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Lpoolboy Tuesday, March 26, 2019 5:27 PM
    Tuesday, March 26, 2019 5:02 PM
    Moderator

All replies

  • Perhaps this will help

    using System;
    using static System.Console;
    
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
                bool answer = false;
                WriteLine("Is God willing to prevent evil? [Y/N]: ");
    
                try
                {
                    answer = ReadLine().ToBool();
                    WriteLine(answer);
                }
                catch (Exception)
                {
                    WriteLine("Invalid input");
    
                }
                
                ReadLine();
            }
    
    
        }
    
        static class Extensions
        {
            public static bool ToBool(this string input)
            {
                if (input.Equals("y", StringComparison.OrdinalIgnoreCase))
                {
                    return true;
                }
                else if (input.Equals("n", StringComparison.OrdinalIgnoreCase))
                {
                    return false;
                }
                else
                {
                    throw new Exception("The data is not in the correct format.");
                }
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Lpoolboy Tuesday, March 26, 2019 5:27 PM
    Tuesday, March 26, 2019 5:02 PM
    Moderator
  • Thank you! I'm not sure how this works but I can go through and look up all the terms and figure out exactly how they work and what each one is useful for, which will help immensely. It gives me another avenue to go down tonight, in any case. Thanks again.
    Tuesday, March 26, 2019 5:30 PM
  • Thank you! I'm not sure how this works but I can go through and look up all the terms and figure out exactly how they work and what each one is useful for, which will help immensely. It gives me another avenue to go down tonight, in any case. Thanks again.

    Note 1

    using static System.Console; at the top of the class, this allows us to write WriteLine rather than Console.WriteLine, key work here is static.

    Note 2

    Since there are no method to convert Y/N to bool a method was needed in the form of a language extension.

    Note 3

    When moving away from console projects for bool you will be dealing with true/false rather than Y/N so to assert this here is a very simple example. See also

    var response = "true";
    if (bool.TryParse(response, out var result))
    {
        
    } 


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, March 26, 2019 5:59 PM
    Moderator
  • One caution:  your test is looking ONLY for an upper case "Y".  If you just typed "y", that would not match.  The match has to be exact.

    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Tuesday, March 26, 2019 9:33 PM
  • I know this post has been answered, but I feel Karen's answer was a bit over-complicated and didn't really explain what the original problem was. So I thought I'd put in a couple of cents worth.

    Lpoolboy, the original problem was to do with something called scope. That is, the lifetime of variables.

    You declared willingtopreventevil inside an 'if' statement. This means it will exist only inside that block, and anywhere else won't be able to use it. If you have also declared a variable with the same name outside of the 'if' block, that's the one the rest of your code will 'see'.

    Your code would probably have worked (assuming no other errors) with the following minimal change.

                // Declare the variable here, outside the 'if'.
                bool willingtopreventevil = false;
        
                if (answer1 == "Y")
                {
                   // Change the value of the variable here.
                   // Do NOT re-declare it. That is, don't put 'bool' in front of it.
                   // This will use the variable declared above, rather than creating a new one 
                   // with limited scope.
                   willingtopreventevil = true;
                }

    I'll leave you to find some articles explaining the concept of scope more fully. It's a tricky concept for beginners, but you will need to get it right for non-trivial "Hello World" programs.


    Wednesday, March 27, 2019 1:06 AM