Home > Mobile >  How to check and compare user input from console?
How to check and compare user input from console?

Time:12-02

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();


            

        }
        public 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.

CodePudding user response:

userResponse1 and userResponse2 are out of scope from the BirthdayCheck method. you need to either pass in references to the human/pet objects(like human.userResponse2 == pet.userResponse1) or pass the birthdays themselves in to compare.

CodePudding user response:

Is there a reason why you made this so complicated? You made 3 classes and 3 interfaces and how do you expect them to work together? You use Console.WriteLine(); without variables.

Does your code need to make sense? If its just for practice, you should slow down and try something more simple, like 2 classes and then compare, then 2 classes and 1 interface and then compare...

CodePudding user response:

you mean

    public string BirthdayCheck()
    { 

you were missing () from the method declaration

  • Related