user2980622
user2980622

Reputation: 123

c# How to display an array based on user input

I have a program where the user inputs an "Address book" where they must enter 5 people before they can search. For phone numbers it only does area codes to keep it quick.

My question is, at the end of the program, it asks the user to search for a name or email, and it returns the index it is at. I'd like it to display the actual information of that person.

        static void Main (string[] args)
    {
        int size = 5;
        string[] names = new string[size];
        int[] Age = new int[size];
        string[] address = new string[size];
        int[] phone = new int[size];
        string[] email = new string[size];
        bool stop = false; // stop to flag for while loops to end loops
        string temp;
        int counter = 0;
        while((stop == false) && (counter <5)) // counter can only get to 5 and both statements must be true
        {
            Console.WriteLine ("Enter a name or q/Q to quit");
            temp = Console.ReadLine ();
            if (temp == "q" || temp == "Q") {
                Console.WriteLine ("Quitting");
                stop = true; // Allowing user to quit
            } 
            else 
            {
                names [counter] = temp; // temp value stored into names array [counter]
            }
            Console.WriteLine ("Enter the age or q/Q to quit");
            temp = Console.ReadLine ();
            if (temp == "q" || temp == "Q") {
                Console.WriteLine ("Quitting");
                stop = true;
            } 
            else 
            {
                Age [counter] = Convert.ToInt16(temp); // converts a string to integer
            }
            Console.WriteLine ("Enter address or q/Q to quit");
            temp = Console.ReadLine ();
            if (temp == "q" || temp == "Q") {
                Console.WriteLine ("Quitting");
                stop = true; // Allowing user to quit
            } 
            else 
            {
                address [counter] = temp;
            }
            Console.WriteLine ("Enter phone number or q/Q to quit");
            temp = Console.ReadLine ();
            if (temp == "q" || temp == "Q") {
                Console.WriteLine ("Quitting");
                stop = true;
            } 
            else 
            {
                phone [counter] = Convert.ToInt32(temp); // converts a string to integer

            }
            Console.WriteLine ("Enter a email or q/Q to quit");
            temp = Console.ReadLine ();
            if (temp == "q" || temp == "Q") {
                Console.WriteLine ("Quitting");
                stop = true; // Allowing user to quit
            } 
            else 
            {
                email [counter] = temp;
                counter = counter + 1;
            }
        }       
        for (int i = 0; i <5; i++)
        {
            Console.WriteLine (names [i]);
            Console.WriteLine (Age [i]);
            Console.WriteLine (address [i]);
            Console.WriteLine (phone [i]);
            Console.WriteLine (email [i]);
        } 
        Console.WriteLine ("Enter a name or email to search or Q/q to quit");
        temp = Console.ReadLine ();
        stop = false; 
        while (stop == false) 
        {
            if (temp == "q" || temp == "Q") {
                Console.WriteLine ("Quitting");
                stop = true;
            } 
            else
            {
                for (int i = 0; i <5; i++)
                {
                    if (temp == names [i] || temp == email [i]) {
                        Console.WriteLine ("Name is found at index {0}", i);
                        stop = true;
                    } 
                    else
                    {
                        if (i == 5) 
                        {
                            Console.WriteLine ("Value not found");
                            stop = true;
                        }

                } 
            }
        }
        Console.ReadLine ();

Upvotes: 0

Views: 3882

Answers (3)

Bojan Komazec
Bojan Komazec

Reputation: 9536

Just replace

Console.WriteLine ("Name is found at index {0}", i);

with

Console.WriteLine ("Person is found at index {0}. Name: {1}, Age: {2}, Address: {3}, Phone: {4}, Email: {5}", i, names[i], Age[i], address[i], phone[i], email [i]);

You should break down this method above into shorter ones as at the moment it does many things (it has many responsibilities) and is difficult to read and understand. The first step could be separating populating data structures (entering data) from consuming them (printing filtered data).

Instead of having array for each attribute, you can create class named e.g. Person with members Name, Age, Address, Phone, Email and have a single array - an array of Person objects. That would simplify your code.

It would be better to keep Persons in list instead of in an array as you don't know in advance when will user hit Q)uit and stop adding new Persons. If size was 10000 and user quits after 10 entries, you have 9990 allocated but unused spaces for Person objects.

Also, think of how to handle the case when some fields of Person are not entered. (Do you want to use e.g. empty string or don't want to add half-populated instance to the list at all...).

Upvotes: 1

Shar1er80
Shar1er80

Reputation: 9041

Change

if (temp == names [i] || temp == email [i]) {
    Console.WriteLine ("Name is found at index {0}", i);
    stop = true;
}

To

if (temp == names [i] || temp == email [i]) {
    Console.WriteLine (names [i]);
    Console.WriteLine (Age [i]);
    Console.WriteLine (address [i]);
    Console.WriteLine (phone [i]);
    Console.WriteLine (email [i]);
}

I wouldn't bother stopping until the user inputs q or Q.

Upvotes: 0

DanielVorph
DanielVorph

Reputation: 131

You should use a adress book class with name, age, address, phone number properties, and you can use a generic List to stores the data that the user inputs.

Generic List already has finding methods.

Upvotes: 0

Related Questions