Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Just as my question stated, I am needing to ask the user to input and display their input back to them. Which would then have the menu populate again. I'm having an extremely difficult time with this. The code below will run and display the menu, and when an option is selected it will ask the user for their input but it never does anything with that information. I apologize for my newness and low level question but any help would be amazing and very much appreciated.
Thank you


C#
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~Menu~~~~~~~~~~~~~~~~~~~~~~");
            Console.WriteLine();
            Console.WriteLine("Please Choose one of the following options");
            Console.WriteLine();
            Console.WriteLine("1) Please Enter Scores ");
            Console.WriteLine("2) Display Scores ");
            Console.WriteLine("3) Calculate Statistics  ");
            Console.WriteLine("4) Exit Program ");
         
            
            int menuselect;
            int[] userScore;
           
            userScore = new int[0];
            menuselect = Convert.ToInt32(Console.ReadLine());
           
            while (menuselect != 4)
            {
               
                

                if (menuselect == 1)
                {
                     
                    // Enter Scores 
                    // int ScoresOpt1 = First set of user input scores 
                    Console.WriteLine("How many Scores would you like to Enter?   ");
                    int ScoresOpt1 = Convert.ToInt32(Console.ReadLine());
                  
                    for (int i = 0; i < userScore.Length; ++i)
                    {
                        userScore[0] = Convert.ToInt32(Console.ReadLine());
                        Console.WriteLine(userScore);
                    }

                    Console.ReadKey();
                }
                else if (menuselect == 2)
                {
                    //Display Scores 
                   
                  
                    Console.WriteLine("Your Scores are listed below:    ");
                    Console.WriteLine(userScore); 
                    Console.ReadKey();
                }
                else if (menuselect == 3)
                {
                    // Option 3 code goes here
                    Console.WriteLine("I will calculate your scores for you below:   ");
                    Console.ReadKey();
                }
                else if (menuselect == 4)
                {
                    //Option 4 code goes here
                    Console.WriteLine("Press any key to close the program");
                    Console.ReadKey();
                }
                else
                {
                    // Any other option besides one listed in the menu 
                    Console.WriteLine("Hey!!! You must not know how to follow directions!!!!!!! :|");
                    Console.WriteLine();
                }
               
                
                
                // Display new menu! woot! 
               
                Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~Menu~~~~~~~~~~~~~~~~~~~~~~");
                Console.WriteLine();
                Console.WriteLine("Please Choose one of the following options");
                Console.WriteLine();
                Console.WriteLine("1) Please Enter Scores ");
                Console.WriteLine("2) Display Scores ");
                Console.WriteLine("3) Calculate Statistics  ");
                Console.WriteLine("4) Exit Program ");

            }
            

            Console.ReadKey();

        }

    }
}
Posted
Updated 15-Sep-14 18:10pm
v2

First of all, interactive UI based on console-only application is a big overkill, to be practical. Nearly all practical, or good, console-only applications don't use interactive user input. Instead, they expect all input in one command line. If command line is too big, such option as the name of the file containing all required input is used.

But it this code is just the useful exercise, you can do it. First of all, hide the echo on Console.ReadKey:
C#
using System;

//...

ConsoleKeyInfo key = Console.ReadKey(true);
// Boolean argument is important here

Please see:
http://msdn.microsoft.com/en-us/library/x3h8xffw%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.consolekeyinfo(v=vs.110).aspx[^].

Now, the key is: don't hard-code keys and menu option texts, especially if you are going to re-use them: show to the user in a menu, and then show the user's selection. Here is what you can do: put all this information into a dictionary, which will give you the search with the complexity O(1). Here is how:
C#
using MenuItemDictionary = System.Collections.Generic.Dictionary<char, string>;

//... 

MenuItemDictionary menuItemDictionary = new MenuItemDictionary();

//...

// somewhere in a constructor, populate it:
menuItemDictionary.Add('1', "Please Enter Scores");
menuItemDictionary.Add('1', "..."); // and so on...
// ...

// ...

// Show all options:
foreach(var pair in menuItemDictionary)
    Console.WriteLine("{0}) {1}", pair.Key, pair.Value);

// ...

// This is how you chose the option:
string message;
if (menuItemDictionary.TryGetValue(key.KeyChar, out message)) {
   Console.WriteLine("You have chosen: {0}", message);
   // ...
} else {
   // show something to indicate that this is a wrong key
}

Please see:
http://msdn.microsoft.com/en-us/library/xfhwa508(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/5tbh8a42%28v=vs.110%29.aspx[^].

Now, the message (text of the menu option) is not all. You also need some action. What to do? Instead of using string type, create a type which composes string and action, which could be a delegate instance, with the delegate type, say, System.Action. For example:
C#
using MenuItemDictionary = System.Collections.Generic.Dictionary<char, MenuItem>;

// ...

class MenuItem {
    internal MenuItem(string name, System.Action action) {
        this.Name = name;
        this.Action = action;
    }   
    internal string Name { get; private set; }
    internal System.Action { get; private set; }
}


And then, populate dictionary with keys, and MenuItem instances, each representing name and action. Show the names with keys as shown above. The the key is recognized, show the choice to the user and call the action both taken from the dictionary in its value element of the type MenuItem.

—SA
 
Share this answer
 
v3
Comments
BillWoodruff 16-Sep-14 1:17am    
+5 This is a great lesson in .NET programming. I hope Jake is at a level where he can understand it. I believe that a newcomer to C# ... depending on their previous experience ... should be introduced to Dictionaries asap; they're so useful, and performant.
Sergey Alexandrovich Kryukov 16-Sep-14 1:49am    
Thank you very much, Bill.

Well, apparently, all developers should know a number of central concepts of programming technologies, including this delegates and dictionaries.
If OP fails to understand anything, I'll gladly respond to the follow-up questions. Ignoring those techniques just because other ones are more understandable would be a big mistake.

—SA
george4986 16-Sep-14 1:58am    
i think this solution is to be Placed above all other solutions.
My +5V ;-)
Sergey Alexandrovich Kryukov 16-Sep-14 2:18am    
Thank you very much, George.
—SA
Suvabrata Roy 16-Sep-14 3:08am    
Yes you are rite
Hi,

Here you go I have modified your code :)

C#
static void Main(string[] args)
        {
            Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~Menu~~~~~~~~~~~~~~~~~~~~~~");
            Console.WriteLine();
            Console.WriteLine("Please Choose one of the following options");
            Console.WriteLine();
            Console.WriteLine("1) Please Enter Scores ");
            Console.WriteLine("2) Display Scores ");
            Console.WriteLine("3) Calculate Statistics  ");
            Console.WriteLine("4) Exit Program ");


            int menuselect;
            int[] userScore;

            userScore = new int[0];
            menuselect = 0;

            while (menuselect != 4)
            {
                Console.Write(Environment.NewLine + " Please input your choice : ");
                 menuselect = Convert.ToInt32(Console.ReadLine());
                 Console.WriteLine();
                if (menuselect == 1)
                {

                    // Enter Scores
                    // int ScoresOpt1 = First set of user input scores
                    Console.WriteLine("How many Scores would you like to Enter?   ");
                    int ScoresOpt1 = Convert.ToInt32(Console.ReadLine());
                    userScore = new int[ScoresOpt1];
                    for (int i = 0; i < ScoresOpt1; ++i)
                    {
                        Console.Write(" Score[" + i.ToString() + "] = ");
                        userScore[i] = Convert.ToInt32(Console.ReadLine());
                        //Console.WriteLine(userScore);
                    }
                    Console.WriteLine("Scores are saved");
                    //Console.ReadKey();
                }
                else if (menuselect == 2)
                {
                    //Display Scores

                    if (userScore.Length == 0)
                        Console.WriteLine("Save some score first");
                    else
                    {
                        Console.WriteLine("Your Scores are listed below:    ");
                        int i = 0;
                        foreach (int scr in userScore)
                            Console.WriteLine(" Score[" + (i++).ToString() + "] = " + scr.ToString());
                    }

                    //Console.ReadKey();
                }
                else if (menuselect == 3)
                {
                    // Option 3 code goes here
                    Console.WriteLine("I will calculate your scores for you below:   ");
                    //write your logic
                    //Console.ReadKey();
                }
                else if (menuselect == 4)
                {
                    //Option 4 code goes here
                    Console.WriteLine("Press any key to close the program");
                    Console.ReadKey();

                }
                else
                {
                    // Any other option besides one listed in the menu
                    Console.WriteLine("Hey!!! You must not know how to follow directions!!!!!!! :|");
                    Console.WriteLine();
                }



                // Display new menu! woot!
                if (menuselect != 4)
                {
                    Console.WriteLine();
                    Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~Menu~~~~~~~~~~~~~~~~~~~~~~");
                    Console.WriteLine();
                    Console.WriteLine("Please Choose one of the following options");
                    Console.WriteLine();
                    Console.WriteLine("1) Please Enter Scores ");
                    Console.WriteLine("2) Display Scores ");
                    Console.WriteLine("3) Calculate Statistics  ");
                    Console.WriteLine("4) Exit Program ");
                }

            }


            //Console.ReadKey();

        }


and thanks Jake because of you I have worked on console after a long time I love to work on console :)
 
Share this answer
 
Comments
jake2809 16-Sep-14 1:07am    
Thank You all very much!!!!! This has been such a huge help! I have been racking my brain, reading c# books, and googling for the past few days now. And as far as my "Cheezy" (my words) interface goes, I have to display the menu as such as a requirement by my prof. Also to Suvabrata Ray, I'm glad I could allow you to work on console again! Thanks again Everyone! I think I have a much better understanding and I should be able to finish the rest of my console app!
Suvabrata Roy 16-Sep-14 1:09am    
Best of luck... and enjoy C#...

It could be more better in aspect of C# coding but as you novice so first stick to basic rules, then learn advance ones.

Few keywords will help you like :
Switch : http://msdn.microsoft.com/en-IN/library/06tc147t.aspx
Int.TryParse :http://msdn.microsoft.com/en-us/library/system.int32.tryparse(v=vs.110).aspx
Functional Programming : http://www.codeproject.com/Articles/375166/Functional-programming-in-Csharp
Linq to Object : http://msdn.microsoft.com/en-IN/library/bb397919.aspx
Suvabrata Roy 16-Sep-14 1:14am    
why peoples are down voting what happened ? Please tell me the reason of down vote ...
use do...while loop and switch case to make your program effective
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900