Development C# - If and Arrays problem

Discussion in 'Software' started by deathtaker27, 16 Nov 2011.

  1. deathtaker27

    deathtaker27 #noob

    Joined:
    17 Apr 2010
    Posts:
    2,196
    Likes Received:
    155
    Hello guys,

    I need some help with an assignment at uni, basically option 1 on my menu is ment to offer the user a single entry in the array, and then exit, so it must find an empty space (XXXXXX) and fill it up, but if it has none it has to inform the user.

    Part of the assignment is that it must be a 2D array, and empty blocks must be XXXXXX, and yes i know its horrible. :duh:

    Code is below,

    Thank you.

    DT.




    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ArraysAssessment
    {
        class Program
        {
                //Declare the Menu Selection 
            public static int Selection;
            public static int index;
                //String for an empty entry
            public static string empty = "XXXXXX";
                //Initialise the Array
            public static string[,] book = new string[20, 2];
    
            static void Main(string[] args)
            {
    
                defaultFill();
    
                do
                {
    
                    //Call the menu system
                    MainMenu();
    
    
                    if (Selection == 1)
                    {
                                                                            // NOT WORKING SELECTION = 1, LOOPS THEN DOES THE ELSE
                    
                        for (int i = 0; i < book.Length / 2; i++)
                  
                            //while (true)
                            if (book[i, 0] == empty)
                            {
    
                                    Console.WriteLine("Please enter the Name in the format: Lastname, ForeName");
                                    book[i, 0] = Convert.ToString(Console.ReadLine());
                                    Console.WriteLine("");
                                    Console.WriteLine("Please Enter the Address in the format: Number, Road Name, Town Name");
                                    book[i, 1] = Convert.ToString(Console.ReadLine());
                                    Console.WriteLine("");
                                    Console.WriteLine("The name is: " + book[i, 0]);
                                    Console.WriteLine("The Address is: " + book[i, 1]);
                                    Console.WriteLine("Thank You for inputting the data Press Return to go to the Main Menu");
                                    Console.ReadLine(); 
                                    i = book.Length / 2;
                                 }
                        
    
                            
                           else if(book[i, 0] != empty)
                            {
                                Console.WriteLine("Sorry there is no space");
                                Console.WriteLine("Press Return to go to the Main Menu");
                                Console.ReadLine();
    
    
                            }
    
                        //break;
                        Console.WriteLine("Choice 1 here");
    
                        //Fill in
    
                    }
    
                    else if (Selection == 2)
                    {
                        menuOption2();
                    }
    
    
                    //Display the book
                    else if (Selection == 3)
                    {
                        menuOption3();
                    }
    
                    else
                    {
                        Console.WriteLine("Program Ended, Press Return to Close");
    
    
                    }
    
                } while (Selection != 6);
                    Console.ReadLine();
            }
    
            private static void menuOption3()
            {
    
                for (int i = 0; i < book.Length / 2; i++)
                {
                    Console.WriteLine("____________________________________________");
                    Console.WriteLine((i + 1) + " | " + book[i, 0] + " | " + book[i, 1]);
    
                }
                Console.WriteLine("Press Return to go to the Main Menu");
                Console.ReadLine();
            }
    
            private static void menuOption2()
            {
                // Ask for the Address Index
                Console.WriteLine("Please Enter The Address Index");
                index = Convert.ToInt32(Console.ReadLine());
    
                // Index Starts at 1, but array at 0 so taking the difference 
                index = (index - 1);
    
                // Write to Address Book.
                book[index, 0] = empty;
                book[index, 1] = empty;
    
                Console.WriteLine("Choice 2 here");
            }
    
            private static void defaultFill()
            {
                //Fill the Address book with 'XXXXXX' for blank entrys
    
                for (int i = 0; i < book.Length / 2; i++)
                {
                    book[i, 0] = empty;
                    book[i, 1] = empty;
                }
            }
    
            public static void MainMenu()
            {
    
                //Clear Everything above
                Console.Clear();
                Console.WriteLine("Please Select One of the following options:");
                Console.WriteLine("_________________________________");
                Console.WriteLine("| 1. | Add an Entry             |");
                Console.WriteLine("| 2. | Delete an Entry          |");
                Console.WriteLine("| 3. | Print Book to Screen     |");
                Console.WriteLine("| 6. | Exit The Address Book    |");
                Console.WriteLine("|_______________________________|");
                //Store the selection
                Selection = Convert.ToInt32(Console.ReadLine());
                //Clear the menu
                Console.Clear();
    
            }
        }
    }
     
  2. glaeken

    glaeken Freeeeeeeze! I'm a cawp!

    Joined:
    1 Jan 2005
    Posts:
    2,041
    Likes Received:
    50
    What problem are you having exactly? Are you getting unexpected results or are you having difficulty implementing the logic?
     
  3. xMathiasD

    xMathiasD What's a Dremel?

    Joined:
    15 Mar 2009
    Posts:
    437
    Likes Received:
    17
    uhm yeah! working code - took me a half hour.. haven't programmed in a while now..

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ArraysAssessment
    {
        class Program
        {
            //Declare the Menu Selection 
            public static int Selection;
            public static int index;
            public static bool booksfull = false;
            //String for an empty entry
            public static string empty = "XXXXXX";
            //Initialise the Array
            public static string[,] book = new string[20, 2];
    
            static void Main(string[] args)
            {
    
                defaultFill();
    
                do
                {
    
                    //Call the menu system
                    MainMenu();
    
    
                    if (Selection == 1)
                    {
                        // NOT WORKING SELECTION = 1, LOOPS THEN DOES THE ELSE
    
                        for (int i = 0; i < book.Length / 2; i++)
                        {
                            //while (true)
                            if (book[i, 0] == empty)
                            {
                                Console.WriteLine("Please enter the Name in the format: Lastname, ForeName");
                                book[i, 0] = Convert.ToString(Console.ReadLine());
                                Console.WriteLine("");
                                Console.WriteLine("Please Enter the Address in the format: Number, Road Name, Town Name");
                                book[i, 1] = Convert.ToString(Console.ReadLine());
                                Console.WriteLine("");
                                Console.WriteLine("The name is: " + book[i, 0]);
                                Console.WriteLine("The Address is: " + book[i, 1]);
                                Console.WriteLine("Thank You for inputting the data Press Return to go to the Main Menu");
                                Console.ReadLine();
                                booksfull = false;
                                i = book.Length / 2;
                            }
                            else
                            {
                                booksfull = true;
                            }
    
                        }
    
                        if (booksfull == true)
                        {
                            Console.WriteLine("Sorry there is no space");
                            Console.WriteLine("Press Return to go to the Main Menu");
                            Console.ReadLine();
                        }
                        //break;
                        Console.WriteLine("Choice 1 here");
                        //Fill in
    
                    }
    
                    else if (Selection == 2)
                    {
                        menuOption2();
                    }
    
    
                    //Display the book
                    else if (Selection == 3)
                    {
                        menuOption3();
                    }
    
                    else
                    {
                        Console.WriteLine("Program Ended, Press Return to Close");
                    }
    
                } while (Selection != 6);
                Console.ReadLine();
            }
    
            private static void menuOption3()
            {
    
                for (int i = 0; i < book.Length / 2; i++)
                {
                    Console.WriteLine("____________________________________________");
                    Console.WriteLine((i + 1) + " | " + book[i, 0] + " | " + book[i, 1]);
    
                }
                Console.WriteLine("Press Return to go to the Main Menu");
                Console.ReadLine();
            }
    
            private static void menuOption2()
            {
                // Ask for the Address Index
                Console.WriteLine("Please Enter The Address Index");
                index = Convert.ToInt32(Console.ReadLine());
    
                // Index Starts at 1, but array at 0 so taking the difference 
                index = (index - 1);
    
                // Write to Address Book.
                book[index, 0] = empty;
                book[index, 1] = empty;
    
                Console.WriteLine("Choice 2 here");
            }
    
            private static void defaultFill()
            {
                //Fill the Address book with 'XXXXXX' for blank entrys
    
                for (int i = 0; i < book.Length / 2; i++)
                {
                    book[i, 0] = empty;
                    book[i, 1] = empty;
                }
            }
    
            public static void MainMenu()
            {
    
                //Clear Everything above
                Console.Clear();
                Console.WriteLine("Please Select One of the following options:");
                Console.WriteLine("_________________________________");
                Console.WriteLine("| 1. | Add an Entry             |");
                Console.WriteLine("| 2. | Delete an Entry          |");
                Console.WriteLine("| 3. | Print Book to Screen     |");
                Console.WriteLine("| 6. | Exit The Address Book    |");
                Console.WriteLine("|_______________________________|");
                //Store the selection
                Selection = Convert.ToInt32(Console.ReadLine());
                //Clear the menu
                Console.Clear();
    
            }
        }
    }
    If it adds a book the boolean would go false.. and then jump out without leaving a message..

    but if it dosen't add any books the boolean would become true and then write a message..
     
  4. deathtaker27

    deathtaker27 #noob

    Joined:
    17 Apr 2010
    Posts:
    2,196
    Likes Received:
    155
    + rep for helping dude, been working solid for my new job sorry i didnt reply before.
     
  5. xMathiasD

    xMathiasD What's a Dremel?

    Joined:
    15 Mar 2009
    Posts:
    437
    Likes Received:
    17
    that's ok! had fun doing it.. :D i should begin to program a little bit again.. just dunno what
     

Share This Page