Maddy
Maddy

Reputation: 23

Write Line error in C#

I have an assignment to create a record-keeping console application in C# for adding, viewing, and deleting records. I need to use an array, list, or dictionary (or some combination).

I have tried using arrays and lists, but so far dictionaries have proved the most successful (aka I know how to use them best in this program). In the photo is the code I have so far, but I am getting errors when I prompt the user to enter information. I have tried a few different fixes but none have worked.

My Code

    class Patient
    {
        public static string PatientDict;
            public Console.WriteLine("Enter the patient's name: "); 
        string PatientName = Console.ReadLine();
            public Console.WriteLine("Enter the patient's date of birth: "); 
        string PatientDOB = Console.ReadLine();
            public Console.WriteLine("Enter the patient's age: "); 
        string PatientAge = Console.ReadLine();
            public Console.WriteLine("Enter the patient's ailment(s): "); 
        string PatientAilments = Console.ReadLine();
            public Console.WriteLine("Enter the patient's medication(s): "); 
        string PatientMeds = Console.ReadLine();

        string DOB;
        string Age;
        string Ailments;
        string Meds;
    }

Upvotes: 1

Views: 3704

Answers (2)

FefeHern
FefeHern

Reputation: 81

You may want to rewrite your program. You have multiple cases of Main methods and Console.WriteLine(...) is not method-level code.

It looks like you want to do all of this in the main method, so consider just:

class Patient 
{
    static void main(string[] args) 
    {
        Console.WriteLine("Enter the patient's name: "); 
        string PatientName = Console.ReadLine();

        Console.WriteLine("Enter the patient's date of birth: "); 
        string PatientDOB = Console.ReadLine();

        Console.WriteLine("Enter the patient's age: "); 
        string PatientAge = Console.ReadLine();

        Console.WriteLine("Enter the patient's ailment(s): "); 
        string PatientAilments = Console.ReadLine();

        Console.WriteLine("Enter the patient's medication(s): "); 
        string PatientMeds = Console.ReadLine();
    }
}

As a starting point, and read more about C# & tutorials

Upvotes: 0

Joey040
Joey040

Reputation: 82

You could use a list of your objects (patients) like so:

class Program
{
    static void Main(string[] args)
    {
        // Create a new list of patients to store your values
        List<Patient> patients = new List<Patient>();

        // Get user input and store them as variables
        Console.WriteLine("Enter the patients name: ");
        string name = Console.ReadLine();

        Console.WriteLine("Enter the patients date of birth: ");
        string dob = Console.ReadLine();

        Console.WriteLine("Enter the patients age: ");
        int age = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("Enter the patients ailment(s): ");
        string ailments = Console.ReadLine();

        Console.WriteLine("Enter the patients medication(s): ");
        string meds = Console.ReadLine();

        Console.WriteLine(); // Empty line


        // Add the given input to the list of patients
        patients.Add(new Patient { patientName = name, patientDOB = dob, patientAilments = ailments, patientMeds = meds, patientAGE = age });


        // Print patients to console output
        foreach (var patient in patients)
        {
            Console.WriteLine("Patients name = {0}", patient.patientName);
            Console.WriteLine("Patients date of birth = {0}", patient.patientDOB);
            Console.WriteLine("Patients age = {0}", patient.patientAGE);
            Console.WriteLine("Patients ailment(s) = {0}", patient.patientAilments);
            Console.WriteLine("Patients medication(s) = {0}", patient.patientMeds);
        }
    }
}

class Patient
{
    public string patientName { get; set; }
    public string patientDOB { get; set; }
    public string patientAilments { get; set; }
    public string patientMeds { get; set; }
    public int patientAGE { get; set; }
}

This is just one of the many ways to do what you want. If you want to enter more patients simply prompt a new set of questions and add them to the list.

Upvotes: 2

Related Questions