|
Subject:
|
switch statement
|
|
Posted By:
|
seymour_glass
|
Post Date:
|
1/23/2007 3:27:37 PM
|
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??
|
|
Reply By:
|
dparsons
|
Reply Date:
|
1/23/2007 3:38:22 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^^ ===========================================================
|
|
Reply By:
|
seymour_glass
|
Reply Date:
|
1/23/2007 5:35:15 PM
|
OK i dont understand what string i need. Here however is the code i have started.....
Seymour
{
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
|
|
Reply By:
|
dparsons
|
Reply Date:
|
1/23/2007 5:42:25 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^^ ===========================================================
|
|
Reply By:
|
seymour_glass
|
Reply Date:
|
1/23/2007 6:26:01 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.
|
|
Reply By:
|
dparsons
|
Reply Date:
|
1/23/2007 11:08:52 PM
|
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^^ ===========================================================
|