Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

Switch Case issue.

So this is the code

Code
int caseSwitch = 1;
            switch (caseSwitch)
            {
                case 1:
                    Console.WriteLine("1) Book Tickets");
                    break;

                case 2:
                    Console.WriteLine("2) View Timetable");
                    break;

                case 3:
                    Console.WriteLine("3) Exit");
                    break;

            }

I want all cases to display and then the user choses one. But currently, depending on the "int caseSwitch" number thats the only switch that is displayed.

Thanks for the help :)

EDIT: This is what i mean Spoiler (Click to View)

I want all of the options to display not just the first
Can you re-phrase what you said. It doesn't make any sense.
Move your WriteLine statements above the entire switch block, so they'll be printed regardless. Remember sequencing and selection.
You cant do that with a switch statement.
You could do something like this.

Code
Dictionary<string, Tuple<string, Action>> options = new Dictionary<string, Tuple<string, Action>>
{
    {"1", new Tuple<string, Action>("Book Tickets", () => {})},
    {"2", new Tuple<string, Action>("View Timetable", () => {})}
};

foreach(var o in options)
    Console.WriteLine("{0}) {1}", o.Key, o.Value.Item1);

string i = Console.ReadLine();
if(options.ContainsKey(i))
    options[i].Item2.Invoke();
Lotta blood to let, peace to make, give a fuk about a check
(Nov 17, 2015 01:53 PM)BahNahNah Wrote:  You cant do that with a switch statement.
You could do something like this.

Code
Dictionary&lt;string, Tuple&lt;string, Action&gt;&gt; options = new Dictionary&lt;string, Tuple&lt;string, Action&gt;&gt;
{
    {"1", new Tuple&lt;string, Action&gt;("Book Tickets", () =&gt; {})},
    {"2", new Tuple&lt;string, Action&gt;("View Timetable", () =&gt; {})}
};

foreach(var o in options)
    Console.WriteLine("{0}) {1}", o.Key, o.Value.Item1);

string i = Console.ReadLine();
if(options.ContainsKey(i))
    options[i].Item2.Invoke();
Overly complicated way of doing exactly what Odus said, which is the answer to what the OP actually needs. Just move the output ...
Ive updated the post guys
(Nov 17, 2015 01:59 PM)MrJamieMcC Wrote:  Ive updated the post guys
Broken image.
(Nov 17, 2015 01:59 PM)MrJamieMcC Wrote:  Ive updated the post guys

Why are you even using a switch statement then 0_o.
Lotta blood to let, peace to make, give a fuk about a check
The reason he's using a switch statement is because he wants a person to be able to enter a value, and then the program react, which is correct, but he for some reason can't read the response that this person posted:
(Nov 17, 2015 01:53 PM)Odus Wrote:  Move your WriteLine statements above the entire switch block, so they'll be printed regardless. Remember sequencing and selection.
The answer's right there.
(Nov 17, 2015 02:03 PM)Mouse❤️ Wrote:  The reason he's using a switch statement is because he wants a person to be able to enter a value, and then the program react, which is correct, but he for some reason can't read the response that this person posted:
The answer's right there.

You mean like this? Spoiler (Click to View)

I still got that issue. I apologise for being idiotic, ive just started learning
(Nov 17, 2015 02:07 PM)MrJamieMcC Wrote:  You mean like this? Spoiler (Click to View)

I still got that issue. I apologise for being idiotic, ive just started learning

Remove the conosole writelines from the switch statement.
Lotta blood to let, peace to make, give a fuk about a check
(Nov 17, 2015 02:07 PM)MrJamieMcC Wrote:  You mean like this? Spoiler (Click to View)

I still got that issue. I apologise for being idiotic, ive just started learning

Almost. You can remove them from the cases. Mouse saved me the hard work.

P.S You can just call WriteLine without any args to terminate the line. Or append "\n" to your string literal.
Thank you all very much. I got it to work :)


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by datguy.devFeb 23, 2019 05:19 PM 0 125 Feb 23, 2019 05:19 PM
Last Post: datguy.dev
[C#] by video_errorDec 1, 2016 10:05 PM 14 404 Dec 1, 2016 10:05 PM
Last Post: video_error
[C#] by OG GangstaApr 13, 2014 05:10 PM 25 631 Apr 13, 2014 05:10 PM
Last Post: idb.