1,105,578 Community Members

array addressbook

Member Avatar
tom92united
Newbie Poster
1 post since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I've got a university assignment and just need to know how to remove an entry from the address book and use it as a method

Here's what I have so far, everything's fine apart from the removing, please make it quite simple

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Assignment2
{
    class Program
    {
        // strings for arrays
        public static string name;
        public static string name2;
        public static string address;
        public static string address2;

        public static bool addressbookloop = false;
        public static string selectioninput;
        public static char selection;


        static void addcontact(string[,] addressbook)
        {
            Console.WriteLine();


            Console.WriteLine("You want to add a new entry");
            Console.WriteLine("Press enter to continue");
            Console.ReadLine();

            Console.WriteLine("Please type in the contacts first name");
            name = Console.ReadLine();

            Console.WriteLine("And what is the contacts last name");
            name2 = Console.ReadLine();

            Console.WriteLine("Please type in the contacts house number and street name");
            address = Console.ReadLine();

            Console.WriteLine("Please type in the contacts town / city");
            address2 = Console.ReadLine();

            Console.WriteLine("CONTACT SAVED");

            Console.WriteLine();

            // Sending to Array
            int i = 0; bool Xs = false;
            do
            {
                if (addressbook[i, 0] == "XXXXXXX")
                {
                    addressbook[i, 0] = name;
                    addressbook[i, 1] = name2;
                    addressbook[i, 2] = address;
                    addressbook[i, 3] = address2;

                    Xs = true;
                }
                else
                {
                    i++;
                }

            } while (Xs == false);

        }

        static void removecontact(string[,] addressbook)
        {
            Console.WriteLine("Which contact do you want to delete");
            
                Console.WriteLine("You wish to delete {0} {1}, {2} {3}'s address.", name, name2, address, address2);
                Console.ReadLine();
   
        }


        static void showaddresses(string[,] addressbook)
        {
            Console.WriteLine("*** Address Book ***");
            Console.WriteLine();
            for (int i = 0; i < addressbook.GetLength(0); i++)
            {
                name = addressbook[i, 0];
                name2 = addressbook[i, 1];
                address = addressbook[i, 2];
                address2 = addressbook[i, 3];
                Console.WriteLine("{0} {1}, {2}, {3}", name, name2, address, address2);
            }
            Console.WriteLine();
            Console.WriteLine("Press enter to go back to the homepage");

        }

        static void Main()
        {
            // starting array
            string[,] addressbook = new string[10, 4];

            for (int i = 0; i < addressbook.GetLength(0); i++)
            {
                for (int j = 0; j < addressbook.GetLength(1); j++)
                {
                    addressbook[i, j] = "XXXXXXX";
                }
            }

            do
            {
                Console.WriteLine("*** My Address Book ***");
                Console.WriteLine();
                Console.WriteLine("Which option would you like to choose");
                Console.WriteLine();
                Console.WriteLine("1. Add new entry");
                Console.WriteLine("2. Remove entry");
                Console.WriteLine("3. Show address book");
                Console.WriteLine();
                Console.WriteLine("Please enter your selection below");
                selectioninput = Console.ReadLine();
                selection = Convert.ToChar(selectioninput);

                switch (selection)
                {
                    case '1':
                        // Add Contact
                        addcontact(addressbook);
                        break;
                    case '2':
                        // Remove contact

                        removecontact(addressbook);
                        break;
                    case '3':
                        // Show addresses
                        showaddresses(addressbook);
                        Console.ReadLine();
                        break;
                    default:
                        Console.WriteLine("This is an invalid selection, Please Try Again");
                        Console.WriteLine();
                        break;

                }
            }
            while (addressbookloop == false);
             


        }

    }
}
Member Avatar
thines01
Postaholic
2,420 posts since Oct 2009
Reputation Points: 389 [?]
Q&As Helped to Solve: 413 [?]
Skill Endorsements: 10 [?]
Team Colleague
Featured
 
0
 

There are a couple of other UK students working on the same thing.
Please check this post. Maybe you can work together.

Member Avatar
kamilacbe
Junior Poster
104 posts since Jun 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 11 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi,

You can try removing from array's "removeat(array index) " method to remove particular record..

thanx.

Member Avatar
thines01
Postaholic
2,420 posts since Oct 2009
Reputation Points: 389 [?]
Q&As Helped to Solve: 413 [?]
Skill Endorsements: 10 [?]
Team Colleague
Featured
 
0
 

You can try removing from array's "removeat(array index) " method to remove particular record.

From what I understand from looking at the other two sets of requirements, they are supposed to mark the records as available for overwrite by filling the first element in the array with six X "XXXXXX".

Member Avatar
skatamatic
Posting Shark
980 posts since Nov 2007
Reputation Points: 371 [?]
Q&As Helped to Solve: 133 [?]
Skill Endorsements: 5 [?]
 
0
 

Do you HAVE to use arrays? They aren't exactly tuned performance-wise for remove operations. A linked list or binary tree would be the ideal datastructures here.

Member Avatar
thines01
Postaholic
2,420 posts since Oct 2009
Reputation Points: 389 [?]
Q&As Helped to Solve: 413 [?]
Skill Endorsements: 10 [?]
Team Colleague
Featured
 
0
 

I'm assuming this is for academic purposes only.
Ive seen this a few places on this site and around the web.

Maybe the next lesson will be "Look how much easier this is with classes and collections". :D

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: