Hello everyone I am trying to code an address book in C#, the program should be a console application and should stores names address etc in a string.

The menu should loop and continue wish I have done using a do loop, at the moment I am stuck on the menu as dont how to use the number keys for the following functions in my program

1 Add Entry to the address book
2 Delete Entry to the address book
3 Print Book to Screen
4 Edit a Record
5 Search for a record using either Surname, Firstname or Road Name

Please can you proivde solutions using example thanks.

Here is my code so far:

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


class AddressProgram
{
    static void Main()
    {
        ArrayList address = new ArrayList { };
        Boolean menu = true;
        Double keychar;
        do
        {
            
            Console.WriteLine("Welcome to my address book program");
            Console.WriteLine("*****************************");
            Console.WriteLine("Press 1 to Add an entry to the address book");
            Console.WriteLine("Press 2 to Remove any entry in the address book");
            Console.WriteLine("Press 3 to Print the book to the screen");
            Console.WriteLine("Press 4 to Edit a record");
            Console.WriteLine("Press 5 to Search for a record");
            Console.ReadLine();


            Console.WriteLine("Please Enter your Lastname");
            string str2 = "Lastname";
            str2 = Console.ReadLine();
            string valueString2 = str2;

            Console.WriteLine("Please Enter your Firstname");
            string str1 = "Firstname";
            str1 = Console.ReadLine();
            string valueString1 = str1;

            Console.WriteLine("Please Enter your Address, house number and town");
            string str3 = "Address";
            str3 = Console.ReadLine();
            string valueString3 = str3;

            Console.WriteLine("Please Enter your County");
            string str5 = "County";
            str5 = Console.ReadLine();
            string valueString5 = str5;

            Console.WriteLine("Please Enter your Postcode");
            string str6 = "Postcode";
            str6 = Console.ReadLine();
            string valueString6 = str6;



            Console.WriteLine("Here is a list of the stored names and addresees that you have entered so far");
            Console.WriteLine("******************************************************************************");
            Console.ReadLine();


           

            
            Console.WriteLine("Lastname you entered: " + valueString2);
            Console.WriteLine("Firstname you entered: " + valueString1);
            Console.WriteLine("Address and house number and town you entered: " + valueString3);
            Console.WriteLine("County you entered: " + valueString5);
            Console.WriteLine("Postcode you entered: " + valueString6);
            Console.ReadLine();

        }
        while(menu == true);
           
        
        }

            }

Recommended Answers

All 7 Replies

Hi,
Use a switch statement to move to the correct option based on the user input (1-5)

switch (input)
{
    case 1: 
        // add address
        break;
    case 2:
        // remove address
        break;
    case 3:
    etc...
}

Thanks for your help, where exactly do I place this code in my program? And how do I Print any book on the address book?, Edit a record and Search for any record?

Thank you.

Thanks for your help, where exactly do I place this code in my program? And how do I Print any book on the address book?, Edit a record and Search for any record?

Thank you.

Here you go:

do
            {
                Console.WriteLine("Welcome to my address book program");
                Console.WriteLine("*****************************");
                Console.WriteLine("Press 1 to Add an entry to the address book");
                Console.WriteLine("Press 2 to Remove any entry in the address book");
                Console.WriteLine("Press 3 to Print the book to the screen");
                Console.WriteLine("Press 4 to Edit a record");
                Console.WriteLine("Press 5 to Search for a record");

                switch(Console.ReadLine())
                {
                    case "1": // Check if user entered 1.
                        Console.Clear(); // Clear the console screen.
                        Console.WriteLine("Add!"); // Write you add "program" here.
                        break; // Stop the switch loop.
                    case "2":
                        Console.WriteLine("Remove");
                        break;
                    default: // If user didn't enter 1 or 2 his choice isn't valid. Show a message.
                        Console.WriteLine("The choice you made isn't valid, please try again.");
                        break;
                }
            }

If you want to clear your console screen, just use Console.Clear(); If you want to save adresses and such. U could use a database like MySQL or use a text file. I suggest you to first search the internet for some tutorials on these subjects. (Shouldn't be hard to find).

Hope that helps. :)

Yeah thanks it would be useful to save the strings to .txt files.

Any idea how to do this??

Also how would I search for a string??

Tried Looking on the internet no luck sorry

Thanks

Here you go:

Console.Clear(); // Clear the console screen.
                        Console.WriteLine("Here you can add a entry to your address book.");
                        Console.Write("Name: ");
                        string name = Console.ReadLine();
                        Console.Write("Phone number: ");
                        string phonenumber = Console.ReadLine();

                        StreamWriter writer = new StreamWriter("addresses.txt"); // Create a new file.
                        writer.Write(name + ";" + phonenumber); // Write name and phonenumber in it.
                        writer.Close(); // Close the writer.

                        break; // Stop the switch loop.

For searching and better programming (Object Orientated) first go read some books and study some more. Hope this helps a bit.

Bye.

Great thanks

How would I print the .txt files that is created using the strings?

And Search for a record by Firstname Lastname etc in my program.

Thanks

I'll speak to you on Monday, Lee.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.