11 Replies - 1958 Views - Last Post: 18 January 2011 - 11:56 AM Rate Topic: -----

#1 mendes272   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 16-January 11

Problem with setting menu interaction with switch statement

Posted 17 January 2011 - 08:59 PM

Hi all , i am new to this community but i have been surfin trough some old posts and it has already proven to be very useful.

however , i got a problem i can't solve , i'm trying to create a menu in console c# like "Press F1 get x , Press F2 get Y ...." you get the point. What it is supposed to do is that when i press F1 for example it will lead me to another class i created , F2 other , and so forth.

I will post the code i got done on this menu part and hope you guys find what is wrong! (I am still very new to programming so it may be obvious)

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

namespace Isegi_98
{
    class Menu
    {
        static void Main(string[] args)
        {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("*********************************");
                Console.WriteLine("           ISEGI98               ");
                Console.WriteLine("*********************************");
                Console.WriteLine(" F1 - Adição");
                Console.WriteLine(" F2 - Subtração");
                Console.WriteLine(" F3 - Multiplicação");
                Console.WriteLine(" F4 - Divisão");
                Console.WriteLine(" F5 - Factoriais");
                Console.WriteLine(" F6 - Raíz quadrada de um número");
                Console.WriteLine(" F7 - Número elevado ao quadrado");
                Console.WriteLine(" F8 - Seno , Coseno e Tangente de um ângulo");
                Console.WriteLine(" F9 - Sobre...");
                Console.WriteLine(" Esc - Sair da Aplicação");

                Console.ReadLine();
            
            Adição verAdição = new Adição();
            verAdição.getAdição();
            Subtração verSubtração = new Subtração();
            verSubtração.getSubtracção();
            Multiplicação verMultiplicação = new Multiplicação();
            verMultiplicação.getMultiplicação();
            Divisão verDivisão = new Divisão();
            verDivisão.getDivisão();
            Factoriais verFactoriais = new Factoriais();
            verFactoriais.getFactoriais();
            Raíz verRaíz = new Raíz();
            verRaíz.getRaíz();
            Quadrado verQuadrado = new Quadrado();
            verQuadrado.getQuadrado();
            Angulo verAngulo = new Angulo();
            verAngulo.getAngulos();
            Sobre verSobre = new Sobre();
            verSobre.getSobre();


            ConsoleKeyInfo tecla;
            tecla = Console.ReadKey();


            switch (tecla.Key)
            {
                case ConsoleKey.F1:
                    verAdição.getAdição();
                    break;
                case ConsoleKey.F2:
                    verSubtração.getSubtracção();
                    break;
                case ConsoleKey.F3:
                    verMultiplicação.getMultiplicação();
                    break;
                case ConsoleKey.F4:
                    verDivisão.getDivisão();
                    break;
                case ConsoleKey.F5:
                    verFactoriais.getFactoriais();
                    break;
                case ConsoleKey.F6:
                    verRaíz.getRaíz();
                    break;
                case ConsoleKey.F7:
                    verQuadrado.getQuadrado();
                    break;
                case ConsoleKey.F8:
                    verAngulo.getAngulos();
                    break;
                case ConsoleKey.F9:
                    verSobre.getSobre();
                    break;
                case ConsoleKey.Escape:
                    break;
                default:
                    Console.WriteLine("O comando que introduziu não é válido!");
                    break;
            }
            Console.ReadLine();

            }

        }
    }



Hope to get good answers!

Best regards to all

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with setting menu interaction with switch statement

#2 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: Problem with setting menu interaction with switch statement

Posted 18 January 2011 - 05:20 AM

Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:
  • Post the code you're having problems with
  • Post the exact error you're receiving, if you are receiving one
  • If no error explain what the code is doing versus what you want it to do
  • Post your question in the body of your post, not the description field

Was This Post Helpful? 0
  • +
  • -

#3 mendes272   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 16-January 11

Re: Problem with setting menu interaction with switch statement

Posted 18 January 2011 - 08:06 AM

View PostJackOfAllTrades, on 18 January 2011 - 05:20 AM, said:

Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:
  • Post the code you're having problems with
  • Post the exact error you're receiving, if you are receiving one
  • If no error explain what the code is doing versus what you want it to do
  • Post your question in the body of your post, not the description field



The code is not giving any error. However it is not doing what i want it to do. What i want is that when a user presses F1 he goes to another class(Adição) , and so on.
However when i execute this code the commands simply don't work!

Hope someone can help me
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Problem with setting menu interaction with switch statement

Posted 18 January 2011 - 08:09 AM

This is basic debugging. Placing breakpoints, watching the variable values as you step through line by line and so on. This should help.

Debugging tutorial
Was This Post Helpful? 0
  • +
  • -

#5 mendes272   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 16-January 11

Re: Problem with setting menu interaction with switch statement

Posted 18 January 2011 - 08:12 AM

View PosttlhIn, on 18 January 2011 - 08:09 AM, said:

This is basic debugging. Placing breakpoints, watching the variable values as you step through line by line and so on. This should help.

Debugging tutorial



Sorry i don't really understand this. I am new to programming and maybe that is easy but i don't know how to do it.

Thanks for the quick answer
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Problem with setting menu interaction with switch statement

Posted 18 January 2011 - 08:17 AM

That is a link that will take you to a tutorial on how to debug. Follow the tutorial.

Click the link: Debugging tutorial

This post has been edited by tlhIn'toq: 18 January 2011 - 08:18 AM

Was This Post Helpful? 1
  • +
  • -

#7 Guest_mendes272*


Reputation:

Re: Problem with setting menu interaction with switch statement

Posted 18 January 2011 - 10:07 AM

i'm a bit desperate now. I did what you said and my code is not working as intended since it doesn't get to the switch statement.
I have no idea what is wrong and i am already past my deadline , really need help figuring out how to make this menu work
Was This Post Helpful? 0

#8 Curtis Rutland   User is offline

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 5104
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Problem with setting menu interaction with switch statement

Posted 18 January 2011 - 10:21 AM

He's trying to teach you how to solve your own problems. Posting "It doesn't get to the switch statement" isn't helpful unless you tell us what line it stops on.

I think one of the reasons you're having trouble getting answers is the language. I think it's Portuguese? Most of us don't speak or read that language.

But I think your problem is on line 27. You print the menu, then do a ReadLine. The execution stops there, and waits for you to push Enter. Only then will it continue. Remove that ReadLine, and your program should probably work.
Was This Post Helpful? 2
  • +
  • -

#9 mendes272   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 16-January 11

Re: Problem with setting menu interaction with switch statement

Posted 18 January 2011 - 10:38 AM

View PostinsertAlias, on 18 January 2011 - 10:21 AM, said:

He's trying to teach you how to solve your own problems. Posting "It doesn't get to the switch statement" isn't helpful unless you tell us what line it stops on.

I think one of the reasons you're having trouble getting answers is the language. I think it's Portuguese? Most of us don't speak or read that language.

But I think your problem is on line 27. You print the menu, then do a ReadLine. The execution stops there, and waits for you to push Enter. Only then will it continue. Remove that ReadLine, and your program should probably work.


yes it is portuguese =) .
I tried that but it didn't work.

I will try to be more specific :

This is how the code is looking right now:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Isegi_98
{
    class Menu
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Pressione <Enter> para iniciar a aplicação");

            ConsoleKeyInfo tecla;
            
            while (Console.ReadKey().Key != ConsoleKey.Escape)
            {
                Console.Clear();
                
                // Campos do Menu

                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("*********************************");
                Console.WriteLine("           ISEGI98               ");
                Console.WriteLine("*********************************");
                Console.WriteLine(" F1 - Adição");
                Console.WriteLine(" F2 - Subtração");
                Console.WriteLine(" F3 - Multiplicação");
                Console.WriteLine(" F4 - Divisão");
                Console.WriteLine(" F5 - Factoriais");
                Console.WriteLine(" F6 - Raíz quadrada de um número");
                Console.WriteLine(" F7 - Número elevado ao quadrado");
                Console.WriteLine(" F8 - Seno , Coseno e Tangente de um ângulo");
                Console.WriteLine(" F9 - Sobre...");
                Console.WriteLine(" Esc - Sair da Aplicação");

                tecla = Console.ReadKey();

                //Tentativa de criar atalhos para as teclas do menu
                   switch (tecla.Key)
                {
                    case ConsoleKey.F1:
                        Adição verAdição = new Adição();
                        verAdição.getAdição();
                        break;
                    case ConsoleKey.F2:
                         Subtração verSubtração = new Subtração();
                         verSubtração.getSubtracção();
                        break;
                    case ConsoleKey.F3:
                        Multiplicação verMultiplicação = new Multiplicação();
                        verMultiplicação.getMultiplicação();
                        break;
                    case ConsoleKey.F4:
                        Divisão verDivisão = new Divisão();
                        verDivisão.getDivisão();
                        break;
                    case ConsoleKey.F5:
                        Factoriais verFactoriais = new Factoriais();
                        verFactoriais.getFactoriais();
                        break;
                    case ConsoleKey.F6:
                        Raíz verRaíz = new Raíz();
                        verRaíz.getRaíz();
                        break;
                    case ConsoleKey.F7:
                        Quadrado verQuadrado = new Quadrado();
                        verQuadrado.getQuadrado();
                        break;
                    case ConsoleKey.F8:
                        Angulo verAngulo = new Angulo();
                        verAngulo.getAngulos();
                        break;
                    case ConsoleKey.F9:
                        Sobre verSobre = new Sobre();
                        verSobre.getSobre();
                        break;
                    case ConsoleKey.Escape:
                        break;
                    default:
                        Console.WriteLine("O comando que introduziu não é válido!");
                        break;
                }
                   
                

            }

        }
    }


When i use that method that was linked prior to debug what i see is that my program goes ok till line 40 and then it just skips to line 81. And even when it stops on 40 if i try to press F1 or whatever it will not go there!
Hope this helps
Was This Post Helpful? 0
  • +
  • -

#10 Curtis Rutland   User is offline

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 5104
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Problem with setting menu interaction with switch statement

Posted 18 January 2011 - 10:45 AM

Ok, I tried a simplified version of this and it worked for me. Perhaps it's your keyboard layout that is causing problems. Try this: instead of printing an error statement at line 81, try doing this:

default:
    Console.WriteLine("Default: " + tecla.Key.ToString());
    break;


Then it'll print out what it thinks you pressed. And you can handle it from there.
Was This Post Helpful? 2
  • +
  • -

#11 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Problem with setting menu interaction with switch statement

Posted 18 January 2011 - 10:52 AM

While debugging, when stopped and viewing line by line (using the F-10) key, you can hover your mouse over any variable to see it's value.

Debug again and stop at line 40.
Hover over 'key' in switch (tecla.Key) and you will see it's value.

The reason it jumps from line 40 to line 81 is because none of your case statements are being matched.
tecla.Key does not match any of your case conditions.

You are very likely running into a globalization issue. The key you are pressing on your Porteugese keyboard and O.S. does not match the Console.F7 key you are comparing to because that is probably an English character code.

You will probably have to map out the value you are pressing to the response you are expecting.
Press the F1 and get it's value. Then alter your case for F1 to match the value you just confirmed.

Instead of checking for Console.F1 you might try System.Windows.Forms.Keys.F1
I don't know for sure that this is more internationally compatible, but trial and error is free.
Was This Post Helpful? 1
  • +
  • -

#12 mendes272   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 16-January 11

Re: Problem with setting menu interaction with switch statement

Posted 18 January 2011 - 11:56 AM

Thanks a ton guys , the problem has to do with the keyboard. Meanwhile i found a way to get around this.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1