Search code examples
c#classinterfaceconsole-applicationuser-input

How to check and compare user input from console?


I have been trying to make a simple program to check a person's birthday and if their birthday is the same as their pet, for it to be printed out on the console, or if it's not the same to type out no valid input. I don't know why but the variables are not being taken in saying they aren't properly added or it just says they need a get/set. If anyone could show and explain how it should be done it would be like really awesome and cool and amazing. Here's the code:

using System;

namespace MyApplication
{
    class Program
    {


        static void Main(string[] args)
        {


            Human human = new Human();
            human.name();
            human.age();
            human.id();
            human.birthday();


            Robot robot = new Robot();
            robot.id();
            robot.model();


            Pet pet = new Pet();
            pet.name();
            pet.birthday();

            Program program = new Program();
            program.BirthdayCheck("","");
            

        }
        public static void BirthdayCheck(string userResponse1, string userResponse2)
        {
            if (userResponse1 == userResponse2)
            {
                Console.WriteLine("" + userResponse1);
            }
            else
            {
                Console.WriteLine("No matching birthday");
            }
            
        }



        interface IHuman
        {
            void name();
            void age();
            void id();
            void birthday();

        }

        interface IRobot
        {
            void model();
            void id();
        }

        interface IPet
        {
            void name();
            void birthday();
        }

        class Human : IHuman
        {
            public string userResponse2;

            public string Birthday
            {
                get { return userResponse2; }
                set { userResponse2 = value; }
            }
            public void name()
            {
                Console.WriteLine("Citizen name: ");
                Console.ReadLine();
            }
            public void age()
            {
                Console.WriteLine("Citizen's age: ");
                Console.ReadLine();
            }
            public void id()
            {
                Console.WriteLine("Citizen's id: ");
                Console.ReadLine();
            }
            public void birthday()
            {
                Console.WriteLine("Citizen's birthday: ");
                userResponse2 = Console.ReadLine();
            }

        }

        class Robot : IRobot
        {

            public void model()
            {
                Console.WriteLine("Enter Robot Model: ");
                Console.ReadLine();
            }
            public void id()
            {
                Console.WriteLine("Enter Robot Id: ");
                Console.ReadLine();
            }
        }
        class Pet : IPet
        {

            public string userResponse1;

            public string Birthday
            {
                get { return userResponse1; }
                set { userResponse1 = value; }
            }
            public void name()
            {
                Console.WriteLine("Enter pet name: ");
                Console.ReadLine();
            }

            public void birthday()

            {
                Console.WriteLine("Enter pet birthday: ");
                userResponse1 = Console.ReadLine();

            }
        }
    }
}
  

I had no issues with the interfaces themselves and it does that the information, it just doesn't want to compare the two between them. I don't know if it's just a logical or syntax error, but hopefully, it's at least partially correct. Finished up some syntax errors but the issues still remain. The error message that appears is "Member Program.BitrhdayCheck(string, string) cannot be accessed with an instance reference; qualify it with a type name instead."


Solution

  • As commented above, this looks like a really complicated program to compare something so simple. Assuming that's your choice, I think these two lines in the main method are not quite correct:

                Program program = new Program();
                program.BirthdayCheck("","");
    

    you really should be calling the BirthdayCheck method directly. Another issue is, no value is really being passed to this method. So not sure exactly what is it you're comparing.


    Here's a fix to your main method that can solve your issue:

        static void Main(string[] args)
        {
    
    
            Human human = new Human();
            human.name();
            human.age();
            human.id();
            human.birthday();
    
    
            Robot robot = new Robot();
            robot.id();
            robot.model();
    
    
            Pet pet = new Pet();
            pet.name();
            pet.birthday();
    
            BirthdayCheck(pet.Birthday, human.Birthday); // birthdaycheck is method of the program class, hence does not need to be invoked with a new program object. 
    // Additionally, you need to pass an actual birthday value to compare instead of blank strings. 
    
            Console.ReadKey(); // this step ensures the console does not close after the birthday check
        }
        public static void BirthdayCheck(string userResponse1, string userResponse2)
        {
            if (userResponse1 == userResponse2)
            {
    
                Console.WriteLine("" + userResponse1);
            }
            else
            {
                Console.WriteLine("No matching birthday");
            }
    
        }
    

    Here's an output sample of the program after the above mentioned changes:

    Citizen name:
    w
    Citizen's age:
    12
    Citizen's id:
    1
    Citizen's birthday:
    10/10/1990
    Enter Robot Id:
    2
    Enter Robot Model:
    r2d2
    Enter pet name:
    p2v2
    Enter pet birthday:
    11/11/1991
    No matching birthday