p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   C# (http://p2p.wrox.com/forumdisplay.php?f=22)
-   -   switch statement (http://p2p.wrox.com/showthread.php?t=53127)

seymour_glass January 23rd, 2007 04:27 PM

switch statement
 
ok so I'm a little confused about this. What chooses the cases? I am writing a simple console app that displays an output based on a entered variable (in this case, a letter grade i.e. A, B, C etc) so should it look like:

switch
{
    case A: code

    case B: code

    case C: code
}
??
Do A, B, and C need to be declared first??

dparsons January 23rd, 2007 04:38 PM

No, its just like a SELECT CASE in VB so you need to do this:

string value = "something";

switch(value)
{
   case "something":
      //code
      break;
   case "Something Else":
     //code
     break;
}

remember to put your break statements in there or the compiler will bark at you about code does not support fall through.

================================================== =========
I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
================================================== =========

seymour_glass January 23rd, 2007 06:35 PM

OK i dont understand what string i need. Here however is the code i have started.....

Seymour
Code:

{
            char charGrade;

            Console.WriteLine("Enter the letter grade to see your student status");
            Console.Write("Please enter your selection: ");
            Console.ReadLine(charGrade);
            char.Parse(charGrade);


            switch (charGrade)
            {
                case A: Console.WriteLine("Honor student");
                        break;

                case B: Console.WriteLine("Honor student");
                        break;

                case C: Console.WriteLine("Average student");
                        break;

                case D: Console.WriteLine("Below average student");
                    break;

                case F: Console.WriteLine("Below average student");
                    break;

                default:
                    Console.WriteLine("Invalid selection. Please select an A,B,C,D or F.");           
                    break;

Seymour

dparsons January 23rd, 2007 06:42 PM

Is this code compiling? I am going to say no and that is probably barking about variable A B C D and F arent defined. Enclose those letters in " "

================================================== =========
I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
================================================== =========

seymour_glass January 23rd, 2007 07:26 PM

Ya I was getting those build errors along with some others that i think i fixed. Anyways. In reading the input, what argument do i need? Also, Im still a little stuck on what my argument needs to be for my switch statement. The compiler doesnt like

switch (strGrade)

I would like my input to only read one character (i.e. A B or C )rather than a string, but when i try to parse the input into charGrade the compiler is bumping into problems converting strings to chars???

Sry for the trouble, its been about 3 semesters since i did the first part of this course and im trrying to get my head back in it.

dparsons January 24th, 2007 12:08 AM

Instead of this:
Console.ReadLine(charGrade);
char.Parse(charGrade);

You may want to try
charGrade = Console.ReadLine();

here is the problem I for see you having with your code. A char represents one character but, what happens if a user types in AB? Your code is going to throw an exception =\ I would suggest using a string unless you are going to write in error checking/handling so that if more then one char is passed in, you pass only one char to your variable.

================================================== =========
I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
================================================== =========


All times are GMT -4. The time now is 07:45 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.